Asp:UpdatePanel with FileUpload Control inside.
Posted on 2006-12-10 19:57 小小兜 阅读(1614) 评论(10) 收藏 举报
搜索了不知道多少网页,包括中文的英文的中国的美国的
就没有找到一个切实的可行的方案来很好的解决如何在Atlas UpdatePanel里面使用FileUpload的控件
最后的解决还是依靠下载了最新版本的Asp.Net AJax,也就是Atals改名的升级版本
编辑界面更加漂亮,而且提供的UpdatePanel里面Trigger的支持种类也多了很多
如需要,请到http://ajax.asp.net/下载最新版本的Asp.Net Ajax.
如何在Asp:UpdatePanel里面使用FileUpload控件的解决方案如下:
1.下载最新版本的Asp.Net Ajax: http://ajax.asp.net/
2.修改Web.Config文件,请从http://www.codeproject.com/Ajax/HelloAtlas.asp下载,参照修改:
3. 参照下面的代码使用FileUpload空间。特别注意<Trigger>Attribute的内容。下面内容可以从http://ajax.asp.net/docs/ViewSample.aspx?sref=System.Web.UI.PostBackTrigger%23PostBackTriggerCS.aspx 得到。
<%@ Page Language="C#" %>

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

<script runat="server">

private string saveDir = @"Uploads\";
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile && FileUpload1.FileBytes.Length < 10000 &&
!CheckForFileName())
{
string savePath = Request.PhysicalApplicationPath + saveDir +
Server.HtmlEncode(FileName.Text);
FileUpload1.SaveAs(savePath);
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
UploadStatusLabel.Text = "You did not specify a file to upload, or a file name, or the file was too large. Please try again.";
}
}

protected void CheckButton_Click(object sender, EventArgs e)
{
if (FileName.Text.Length > 0)
{
string s = CheckForFileName() ? "exists already." : "does not exist.";
UploadStatusLabel.Text = "The file name choosen " + s;
}
else
{
UploadStatusLabel.Text = "Specify a file name to check.";
}
}
private Boolean CheckForFileName()
{
System.IO.FileInfo fi = new System.IO.FileInfo(Request.PhysicalApplicationPath +
saveDir + Server.HtmlEncode(FileName.Text));
return fi.Exists;
}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>PostBackTrigger Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<fieldset>
<legend>FileUpload in an UpdatePanel</legend>
First, enter a file name to upload your file to:
<asp:TextBox ID="FileName" runat="server" />
<asp:Button ID="CheckButton" Text="Check" runat="server" OnClick="CheckButton_Click" />
<br />
Then, browse and find the file to upload:
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<br />
<asp:Label id="UploadStatusLabel"
runat="server" style="color:red;">
</asp:Label>
</fieldset>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="UploadButton" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>
此次最愤慨的找了这么多网页,很多作者都自称绝对可以解决,实际上不知道他们是否真正实验过了
害我浪费了那么多的时间帮他们实验,结果到最后才发现仍然不能解决,真是气人。
这个解决方案经过验证是可行的。希望能帮到那些一直在寻找解决方案却仍然没有结果的人们。
就没有找到一个切实的可行的方案来很好的解决如何在Atlas UpdatePanel里面使用FileUpload的控件
最后的解决还是依靠下载了最新版本的Asp.Net AJax,也就是Atals改名的升级版本
编辑界面更加漂亮,而且提供的UpdatePanel里面Trigger的支持种类也多了很多
如需要,请到http://ajax.asp.net/下载最新版本的Asp.Net Ajax.
如何在Asp:UpdatePanel里面使用FileUpload控件的解决方案如下:
1.下载最新版本的Asp.Net Ajax: http://ajax.asp.net/
2.修改Web.Config文件,请从http://www.codeproject.com/Ajax/HelloAtlas.asp下载,参照修改:
3. 参照下面的代码使用FileUpload空间。特别注意<Trigger>Attribute的内容。下面内容可以从http://ajax.asp.net/docs/ViewSample.aspx?sref=System.Web.UI.PostBackTrigger%23PostBackTriggerCS.aspx 得到。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
private string saveDir = @"Uploads\";
protected void UploadButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile && FileUpload1.FileBytes.Length < 10000 &&
!CheckForFileName())
{
string savePath = Request.PhysicalApplicationPath + saveDir +
Server.HtmlEncode(FileName.Text);
FileUpload1.SaveAs(savePath);
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
else
{
UploadStatusLabel.Text = "You did not specify a file to upload, or a file name, or the file was too large. Please try again.";
}
}
protected void CheckButton_Click(object sender, EventArgs e)
{
if (FileName.Text.Length > 0)
{
string s = CheckForFileName() ? "exists already." : "does not exist.";
UploadStatusLabel.Text = "The file name choosen " + s;
}
else
{
UploadStatusLabel.Text = "Specify a file name to check.";
}
}
private Boolean CheckForFileName()
{
System.IO.FileInfo fi = new System.IO.FileInfo(Request.PhysicalApplicationPath +
saveDir + Server.HtmlEncode(FileName.Text));
return fi.Exists;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>PostBackTrigger Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<fieldset>
<legend>FileUpload in an UpdatePanel</legend>
First, enter a file name to upload your file to:
<asp:TextBox ID="FileName" runat="server" />
<asp:Button ID="CheckButton" Text="Check" runat="server" OnClick="CheckButton_Click" />
<br />
Then, browse and find the file to upload:
<asp:FileUpload id="FileUpload1"
runat="server">
</asp:FileUpload>
<br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server">
</asp:Button>
<br />
<asp:Label id="UploadStatusLabel"
runat="server" style="color:red;">
</asp:Label>
</fieldset>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="UploadButton" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>此次最愤慨的找了这么多网页,很多作者都自称绝对可以解决,实际上不知道他们是否真正实验过了
害我浪费了那么多的时间帮他们实验,结果到最后才发现仍然不能解决,真是气人。
这个解决方案经过验证是可行的。希望能帮到那些一直在寻找解决方案却仍然没有结果的人们。


浙公网安备 33010602011771号