| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2070 人关注过本帖
标题:如何在 *.aspx.cs 页面中编写 JavaScript 脚本???
取消只看楼主 加入收藏
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
结帖率:100%
收藏
 问题点数:0 回复次数:6 
如何在 *.aspx.cs 页面中编写 JavaScript 脚本???

以下代码是实现上传文件的,我不想用标签控件来显示上传的结果(红色代码部分),而是想用弹出一个小窗口来显示上传的结果。如何在 *.aspx.cs 页面中编写 JavaScript 脚本???

protected void Button1_Click(object sender, EventArgs e)
{
if (IsPostBack)
{
Boolean fileOK = false;
String path = Server.MapPath("~/file/");
if (FileUpload1.HasFile)
{
String fileExtension =
System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions =
{ ".xls", ".doc"}; //过滤Word和Excel文件
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}

if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(path
+ FileUpload1.FileName);
Label1.Text = "File uploaded!";
}
catch (Exception ex)
{
Label1.Text = "File could not be uploaded.";
}
}
else
{
Label1.Text = "Cannot accept files of this type.";
}
}
}

搜索更多相关主题的帖子: aspx 脚本 JavaScript 页面 编写 
2007-03-07 15:51
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 

想不到是 Response.Write()来实现,谢谢 老大!

还有一个问题,为什么可以省略 Windows. ?
另外还有一个问题:如何在 *.aspx.cs 中执行JavaScript的函数,并传递一个变量给JavaScript脚本??如下不行的:
-------------
*.aspx页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function aa(yy)
{
alert("+ yy +");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="用户名" Width="94px"></asp:Label>&nbsp;
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /></div>
</form>
</body>
</html>

--------------
*.aspx.cs 页面:
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>aa(FileUpload1.FileName)</script>");//想将上传文件控件中的文件名传递给JavaScript
}
}

[此贴子已经被作者于2007-3-8 8:50:13编辑过]


我是2.0超级菜鸟,请多多教导!
2007-03-07 16:10
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 
回复:(冰镇柠檬汁儿)为什么可以省略 Windows. ?因为...
怎么没反映的,哪里还有错啊

[此贴子已经被作者于2007-3-8 8:30:00编辑过]




o91KWaYG.rar (1.38 KB) 如何在 *.aspx.cs 页面中编写 JavaScript 脚本???


我是2.0超级菜鸟,请多多教导!
2007-03-07 17:22
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 
对 JavaScript 还未有什么认识,但试过上楼的代码后,依然没反映

我是2.0超级菜鸟,请多多教导!
2007-03-07 17:34
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 
cyyu_ryh ,这个我已会了。但现在我是想知道如何在 *.aspx.cs 页面中写个代码向 function aa(yy) 传递变量的 JavaScript 脚本。

我是2.0超级菜鸟,请多多教导!
2007-03-07 17:40
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="index" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language="javascript" type="text/javascript">
function aa(yy)
{
alert("+ yy +");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="用户名" Width="94px"></asp:Label>&nbsp;
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /></div>
</form>
</body>
</html>

***********************************************************************

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("<script>return aa(" + FileUpload1.FileName + ");</script>");
}


图片附件: 游客没有浏览图片的权限,请 登录注册

[此贴子已经被作者于2007-3-8 8:53:19编辑过]


我是2.0超级菜鸟,请多多教导!
2007-03-08 08:51
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 
以下是引用chenlong在2007-3-8 17:00:04的发言:
上面的代码我试了没问题你在找找是不是别的地方出现问题了

请上传你测试的代码的文件夹的内容,让我测试和对比一下,谢谢!


我是2.0超级菜鸟,请多多教导!
2007-03-08 17:12
快速回复:如何在 *.aspx.cs 页面中编写 JavaScript 脚本???
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023531 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved