这是标记:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="well well-large">
<form class="navbar-form pull-left">
<asp:FileUpload ID="test" runat="server" CssClass="input-small" />
<br />
<asp:Button ID="btnUpload" runat="server" Text="Upload" CssClass="btn" OnClick="btnUpload_Click" />
</form>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnUpload" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Panel runat="server" ID="panAlert" Visible="false">
<div class="alert alert-success" id="divAlert" runat="server">
<button id="Button1" runat="server" type="button" class="close" data-dismiss="alert">×</button>
You shouldn't see this message!
</div>
<asp:Panel runat="server" ID="panMarquee" Visible="true">
<div id="Div1" runat="server" class="progress progress-success progress-striped">
<div id="ProgressBar" runat="server" class="bar" style="width: 100%"></div>
</div>
</asp:Panel>
</asp:Panel>
单击btnUpload按钮时,服务器代码应确定FileUpload控件是否有文件。如果是,则会将Panel控件的可见性更改为true。它在UpdatePanel之外工作正常。
这是服务器代码:
protected void btnUpload_Click(object sender, EventArgs e)
{
this.SetMessage(Message.Success);
try
{
if (this.test.HasFile)
{
string filename = Path.GetFileName(GetUB04Doc.FileName);
//test.SaveAs(Server.MapPath("~/") + filename);
this.SetMessage(Message.Success);
}
}
catch (Exception ex)
{
//TODO: Do something with th exception
this.SetMessage(Message.Fail);
}
finally
{
//this.GetUB04Doc.Dispose();
}
}
private enum Message { Success, Fail }
private void SetMessage(Message msg)
{
if (msg == Message.Success)
{
this.divAlert.InnerText = "Well done! The document appears to have uploaded successfully. Please wait...";
this.divAlert.Attributes.Add("class", "alert alert-success");
}
else
{
this.divAlert.InnerText = "Oh snap! Something broke. Please contact IT right away.";
this.divAlert.Attributes.Add("class", "alert alert-error");
}
this.panAlert.Visible = true;
}
我也尝试将Panel放在ContentTemplate部分,但结果是一样的。
关于我在这里做错了什么想法?
答案 0 :(得分:3)
您应该将另一个UpdatePanel
中的异步回发环绕在您要修改的区域,UpdateMode
设置为Conditional
。然后从codebehind手动更新另一个:
<asp:UpdatePanel ID="panAlertUpdatePanel" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Panel runat="server" ID="panAlert" Visible="false">
<!-- ....
代码隐藏:
// ...
this.panAlert.Visible = true;
panAlertUpdatePanel.Update()
旁注:@Belogix已经评论过,您应该在UpdatePanel
中使用AsyncFileUpload
控件,因为常规FileUpload
控件不支持异步回发。
MSDN:
与UpdatePanel控件不兼容的控件:
FileUpload
和HtmlInputFile
控制何时用于上传文件,作为异步回发的一部分。在UpdatePanel中使用FileUpload或HtmlInputFile控件 控制,设置提交文件的回发控制为 面板的PostBackTrigger控件。 FileUpload和 HtmlInputFile控件只能在回发场景中使用。
答案 1 :(得分:0)
你需要像这样放PostBackTrigger
而不是AsyncPostBackTrigger
。 <asp:PostBackTrigger ControlID="btnUpload" />