使用 Response.Write 生成供下载的文件并更改页面上的元素

发布于 2024-09-15 15:53:21 字数 236 浏览 5 评论 0原文

我正在尝试更改 asp:textbox 的文本并折叠页面上某些 ascx 控件中的某些 ajaxToolkit:CollapsiblePanelExtenders 以及输出动态生成的文件。我可以毫无问题地折叠 CollapsiblePanelExtenders 并从代码隐藏中更改文本框的文本或输出文件。当我希望这两个事件在同一个回发中发生时,问题就出现了。不幸的是,使用 Response.Write 会取消对页面的所有其他更改。

提前致谢

I am trying to change the text of a asp:textbox and collapse some ajaxToolkit:CollapsiblePanelExtenders within some ascx controls on my page as well as output a dynamically generated file. I have no problem collapsing the CollapsiblePanelExtenders and changing the text of the textbox from the codebehind or outputting a file. The problem arises when I want BOTH of these events to happen on the same postback. Unfortunately using Response.Write negates all of the other changes to the page.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

惯饮孤独 2024-09-22 15:53:21

下面是一个快速概念示例,说明如何使用 UpdatePanel 通过 AJAX 回发来更新屏幕上的文本并同时下载文件。

ASPX 代码:

<asp:UpdatePanel id="update1" runat="server">
  <ContentTemplate>
    <asp:TextBox id="textbox1" runat="server" /><br />
    <asp:Button id="button1" onclick="button1_Click" runat="server" />
  </ContentTemplate>
</asp:UpdatePanel>

C# 代码:

private string GenerateDownloadLink(string fileContent, string fileName) {
  // worker process will need write access to this folder
  string downloadFolder = "./download";

  TextWriter file = new StreamWriter(Server.MapPath(downloadFolder) + @"\" + fileName);
  file.WriteLine(fileContent);
  file.Close();

  return downloadFolder + "/" + fileName;
}

private void button1_Click(object sender, EventArgs e) {
  textbox1.Text = "the file download will begin shortly...";

  string fileContent = "here is the content for a new dynamically generated file";

  string fileUrl = GenerateDownloadLink(fileContent, "hello.txt");

  ScriptManager.RegisterStartupScript(this, this.GetType(), "StartDownload", "window.location = '" + fileUrl + "';", true);
}

另请查看此 MSDN 示例

我还想补充一点,UpdatePanels 会吞噬你的灵魂,你应该摆脱它们,转而使用诸如通过 AJAX 调用 WebMethod 之类的方法(如果可能的话):)

Here is a quick concept example of how you could update text on the screen and download a file at the same time through an AJAX postback with an UpdatePanel.

ASPX code:

<asp:UpdatePanel id="update1" runat="server">
  <ContentTemplate>
    <asp:TextBox id="textbox1" runat="server" /><br />
    <asp:Button id="button1" onclick="button1_Click" runat="server" />
  </ContentTemplate>
</asp:UpdatePanel>

C# code:

private string GenerateDownloadLink(string fileContent, string fileName) {
  // worker process will need write access to this folder
  string downloadFolder = "./download";

  TextWriter file = new StreamWriter(Server.MapPath(downloadFolder) + @"\" + fileName);
  file.WriteLine(fileContent);
  file.Close();

  return downloadFolder + "/" + fileName;
}

private void button1_Click(object sender, EventArgs e) {
  textbox1.Text = "the file download will begin shortly...";

  string fileContent = "here is the content for a new dynamically generated file";

  string fileUrl = GenerateDownloadLink(fileContent, "hello.txt");

  ScriptManager.RegisterStartupScript(this, this.GetType(), "StartDownload", "window.location = '" + fileUrl + "';", true);
}

Also check out this MSDN example.

I would also like to add that UpdatePanels will eat your soul and you should get rid of them in favor of something like calling a WebMethod via AJAX if at all possible :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文