我知道之前已被问过,但我仍然无法弄清楚我做错了什么。
aspx(没有母版页,这是页面的一部分):
<asp:Label ID="Label1" runat="server" Text="Username: "></asp:Label>
<asp:TextBox ID="txtUser" runat="server"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Password: "></asp:Label>
<asp:TextBox ID="txtPass" runat="server" TextMode="Password"></asp:TextBox>
<br />
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<AjaxControlToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"> </AjaxControlToolkit:ToolkitScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" Visible="False">
<ContentTemplate>
<div id="Article">
<br />
<asp:Label ID="lblInfo" runat="server" Text=""></asp:Label>
<br />
<asp:Button ID="btnExcel" runat="server" Text="Download Excel" OnClick="btnExcel_Click" />
<br />
<br />
<asp:Button ID="btnReset" runat="server" Text="RESET" OnClick="btnReset_Click" />
</div>
</ContentTemplate>
<triggers>
<asp:AsyncPostBackTrigger ControlID="btnExcel" EventName="Click"/>
</triggers>
</asp:UpdatePanel>
上面的html代码:
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="AjaxControlToolkit" %>
btnExcel正在响应,但行为与UpdatePanel外部的行为不同。 btnReset会做出响应并做它应该做的事情
aspx.cs:
protected void btnExcel_Click(object sender, EventArgs e)
{
DataTable Project1Table = (DataTable)ViewState["ProjectTable"];
ExportToExcel(Project1Table, "Print2.xls");
}
问题可能与ExportToExcel()
有关。此功能必须下载Excel文件。
private void ExportToExcel(DataTable dt, string FileName)
{
string filename = FileName + "";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();
//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
正如你所看到的,我已经尝试添加AsyncPostBackTrigger(它不在我的工具箱中?)但是这并没有解决问题。
UpdatePanel visible = false,因为当用户使用正确的用户名和密码时,它会发生变化。
ChildrenAsTriggers设置为True
BtnReset成功更新了lblInfo,
当我替换更新面板之外的btnExcel
时,它会执行它应该执行的操作。
感谢阅读,祝你好运解决:)