文件上传后更新页面的问题
我遇到一个与 AsyncFileUpload 控件相关的奇怪问题。上传后,我通过从 ClientUploadComplete 事件处理程序调用 __doPostBack 函数来更新页面。第一次工作正常,但下次我尝试上传文件时,它会在上传之前先刷新页面,然后再次上传并刷新页面。不知道为什么刷新页面在上传前和上传后被调用两次。我有此代码的简化版本,其中存在此错误。有什么线索请问为什么会发生这种情况吗?
标记:
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="AsyncFileUpload1_ClientUploadComplete"
OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Refresh Data" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" EnableViewState="false"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Javascript:
<script type="text/javascript">
function AsyncFileUpload1_ClientUploadComplete() {
var btnRefreshData = $get("<%=Button1.ClientID%>").name;
__doPostBack(btnRefreshData, "");
}
</script>
代码隐藏:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Upload complete";
}
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
System.Threading.Thread.Sleep(3000);
}
I am having a strange issue associated with AsyncFileUpload control. after the upload, I am updating the page by calling__doPostBack function from ClientUploadComplete event handler. it works fine first time, but next time I try to upload the file, it refreshes the page first before uploading, then does the upload and refreshes the page again. not sure why refresh page is being called twice once before the upload and once after the upload. I have a simplified version of this code which has this bug. any clues please why it is happening?
Markup:
<form id="form1" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:AsyncFileUpload ID="AsyncFileUpload1" runat="server" OnClientUploadComplete="AsyncFileUpload1_ClientUploadComplete"
OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Refresh Data" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" EnableViewState="false"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</form>
Javascript:
<script type="text/javascript">
function AsyncFileUpload1_ClientUploadComplete() {
var btnRefreshData = $get("<%=Button1.ClientID%>").name;
__doPostBack(btnRefreshData, "");
}
</script>
Code-Behind:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Upload complete";
}
protected void AsyncFileUpload1_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
{
System.Threading.Thread.Sleep(3000);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
貌似没有人能回答这个问题。我仍然没有弄清楚为什么会发生这种情况,但我采取了一种解决方法,方法是在上传完成时在会话中添加一个标志,并在刷新页面上的数据之前检查该标志。这样在上传之前就不会发生数据刷新。谢谢。
looks like no one can answer this question. I still haven't figured out why this happens, but I put a workaround by adding a flag in session when the upload is complete and check that flag before refreshing the data on the page. this way the data refresh won't happen before the upload. thanks.