在网上搜索发现可以使用PostBackTrigger ,通过设置Trigger来实现共处。
PS:不要直接Copy网页内的代码到自己的程序内,这样会有一定机率出现无法实现其代码功能的现象,请手动输入代码
以下内容转自其他人的博客,转载地址:http://blog.csdn.net/linkinwhite/article/details/5692256,在此谢谢此博客的主人。
今天往一个页面中添加图片上传功能,弄了半天发现不成功,FileUpload始终找不到文件,HasFile属性始终为false;经过多次检查后,没发现和原来图片上传有什么区别,都是在UpdatePanel下设置提交按钮的Triggers,感觉很奇怪,最终在网上找到了这么一段话:
解决办法就是UpdatePanel中设置PostBackTrigger:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>
而如果你又想在这个UpdatePanel上做点花样,比如加了一个asp:Panel, 可以通过按钮事件触发隐藏或显示的,你会发现FileUpload1并不能找到文件。。。
其实道理很简单,UpdatePanel中的内容是通过XmlHttp实时填充的,在你让他显示之前,查看页面源代码里面是空的。一个动态控件更新 普通数据没问题,但上传文件就不行了,我的解决办法是用普通div代替asp:Panel,并写了2个函数来动态发送控制脚本,按钮事件中只要调用该函数 即可:
<div id="Panel1"></div>
private void ShowPanel()
{
string script = "document.getElementById('Panel1').style.display='';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ShowPanel", script, true);
}
private void ClosePanel()
{
string script = "document.getElementById('Panel1').style.display='none';";
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "ClosePanel", script, true);
}
原来我在开始设置了两个Panel的Visible为false,通过RadioButtonList来切换,导致FileUpload找不到文件的原因,我还在奇怪为什么在生成的HTML中查看不了代码呢,呵呵。UpdatePanel只显示初始页面的HTML代码
---------------声明:经过一些测试之后,发现上面的代码并不能很好的解决问题,所以现在使用另一种方式 ,但会回一次全页面刷新的事件。
1.首先在页面内创建DIV与Iframe,使它们独立与现在有的DIV或TABLE之外,如下面代码 :
</table>
</div>
<div id="divSelectFile" style=" background-color: #FFFFCC; z-index:10000; position:absolute; width: 450px; height: 100px; left: 25%; top: 30%; border:1px solid #CCC;display:none; " >
<asp:Label ID="Label10" runat="server" Text="请选择要读取的EXCEL文件....." ></asp:Label><br/><br/>
<asp:FileUpload ID="FileUploadSelect" runat="server" Width="350px" CssClass="button_height" />
<asp:Button ID="btnConfirmSelectRead" runat="server" Text="确定" CssClass ="button_height" οnclick="btnConfirmSelectRead_Click" />
</div>
<iframe id="iframeBlock" src="" scrolling="no" frameborder="0" style="position:absolute; display:none; width: 450px; height:100px;">
</iframe>
</form>
</body>
</html>
在Updatepanel内创建一个调用一段JavaScript代码来控制DIV与Iframe的显示与隐藏:
<asp:TextBox ID="txtFilePath" runat="server" Width="300" ReadOnly="True" ></asp:TextBox>
<input id="btnBrowseExcel" type="button" value="浏览" οnclick="DisplaySelectExcel()" />
下面是Javascript代码:
function DisplaySelectExcel() {
var iFrameShow = document.getElementById("iframeBlock");
var SelectFile = document.getElementById("divSelectFile");
if (iFrameShow.style.display == "block") {
iFrameShow.style.display = "none";
SelectFile.style.display = "none";
}
else {
iFrameShow.style.display = "block";
SelectFile.style.display = "block";
iFrameShow.style.left = SelectFile.style.left;
iFrameShow.style.top = SelectFile.style.top;
iFrameShow.style.width = SelectFile.offsetWidth;
iFrameShow.style.height = SelectFile.offsetHeight;
iFrameShow.style.zIndex = SelectFile.style.zIndex - 1;
}
}
当上面的全部创建完成后,就可以编写刚刚创建DIV的确定按钮CS代码了。
protected void btnConfirmSelectRead_Click(object sender, EventArgs e)
{
//上传文件至临时目录
try
{
if (FileUploadSelect.HasFile)
{
string sFileExt = System.IO.Path.GetExtension(FileUploadSelect.FileName).ToUpper();
if (sFileExt == ".XLS" || sFileExt == ".XLSX")
{
FileUploadSelect.SaveAs(Server.MapPath("temp") + "\\" + FileUploadSelect.FileName);
//sFilePath = Server.MapPath("temp") + "\\" + FileUploadSelect.FileName;
}
else
{
ShowOpenFileNotes.Text = "上传的文件格式不正确,请重新选择文件!";
return;
}
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "Return FileName", "<script language='javascript'>alert('请选择目标文件!');</script>");
return;
}
}
catch
{
ShowOpenFileNotes.Text = "上传文件出错,请确认系统上传功能!";
return;
}
...............读取文件内容............................................................................
try
{
System.IO.FileInfo sTempFile = new System.IO.FileInfo(sFilePath);
sTempFile.Delete();
}
catch
{
ShowOpenFileNotes.Text = "文件资源回收失败!";
return;
}