| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2070 人关注过本帖
标题:如何在 *.aspx.cs 页面中编写 JavaScript 脚本???
只看楼主 加入收藏
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
结帖率:100%
收藏
 问题点数:0 回复次数:16 
如何在 *.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
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 

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);
response.Write("<script>alert('File uploaded!')</script>");
}
catch (Exception ex)
{
response.Write("<script>alert('File could not be uploaded.')</script>");
}
}
else
{
response.Write("<script>alert('Cannot accept files of this type.')</script>");
}
}
}


本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2007-03-07 16:00
summoner
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1622
专家分:0
注 册:2005-3-3
收藏
得分:0 

还有个注册客户端脚本方式,不过忘了

[URL=javascript:window.close();e=new Enumerator(window.opener.document.images);for(;!e.atEnd();e.moveNext()){e.item().src=\'http://blog./UploadFiles/2007-1/117175967.gif\';}]其疾如風、其徐如林、侵掠如火、不動如山、難知如陰、動如雷震[/URL]
2007-03-07 16:02
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
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
为什么可以省略 Windows. ?
因为IE浏览器默认的类就是window,所以可以省略,记住,不要加“s”


Response.Write("<script>aa(" + FileUpload1.FileName + ")</script>");
这么写,不要写错了

[此贴子已经被作者于2007-3-7 17:03:11编辑过]


本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2007-03-07 17:02
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
冰镇柠檬汁儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:北京
等 级:版主
威 望:120
帖 子:8078
专家分:6657
注 册:2005-11-7
收藏
得分:0 
Response.Write("<script>return aa(" + FileUpload1.FileName + ");</script>");
自己也改改啊,我写的不是百分之百的都对,你也自己想想

本来无一物,何处惹尘埃
It is empty at all here, Why pm 2.5 is so TMD high!
2007-03-07 17:24
foshan
Rank: 1
等 级:新手上路
威 望:2
帖 子:605
专家分:0
注 册:2006-3-1
收藏
得分:0 
对 JavaScript 还未有什么认识,但试过上楼的代码后,依然没反映

我是2.0超级菜鸟,请多多教导!
2007-03-07 17:34
cyyu_ryh
Rank: 8Rank: 8
等 级:贵宾
威 望:45
帖 子:1899
专家分:176
注 册:2006-10-21
收藏
得分:0 
Response.Write("<script language='javascript'>alert('" + FileUpload1.FileName + "');</script>");

有事无事都密我. MSN: cyyu_ryh@hotmail.co.jp E-mail: cyyu_ryh@
2007-03-07 17:37
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
快速回复:如何在 *.aspx.cs 页面中编写 JavaScript 脚本???
数据加载中...
 
   



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

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