【问题标题】:clear the textbox after redirect to a download page重定向到下载页面后清除文本框
【发布时间】:2013-05-15 08:55:52
【问题描述】:

我有 2 页。 main.aspx 和下载.aspx download.aspx 处的 page_load 是下载文件 main.aspx 中的文本框和下载按钮 -page_load 处没有功能 -button 重定向到 download.aspx -文本框不能留空。

在我填写文本框并按下按钮后,页面仍保留在 main.aspx,文件从 download.aspx 下载。 现在的问题是,按下下载按钮后如何清除文本框?

我试过了:

  1. this.Textbox1.Text = ""; 重定向前后。
  2. ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "document.getElementById('Textbox1').value =''", true); 重定向前后
  3. Response.Redirect("main.aspx);Response.Redirect("download.aspx"); 之后
  4. onclientclick = "validation()"

函数验证(){ document.getElementById('TextBox1').value=""; }

记住我有验证,所以 4. 不能工作。

5. 

    OnClientClick ="document.forms[0].target = '_blank';"/>

          Response.Redirect(”download.aspx”,false);
        Textbox1.Text="";

6. Textbox1.EnableViewState = false;

7.button 点击​​,重定向到 main2.aspx,在 main2.aspx 的页面加载重定向到 download.aspx。但是在我点击 main.aspx 的按钮后,文件被下载,但页面仍然保留在 main.aspx。

以上方法都不起作用,我还能尝试什么?问题是什么?为什么文本框不能设置为空白?

main.aspx

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
   this.TextBox1.Text = "";
   Response.Redirect("download.aspx");
   this.TextBox1.Text = "";
}

下载.aspx

protected void Page_Load(object sender, EventArgs e)
{
   string reportPath = "C:\\form.pdf";

   Response.ContentType = "appplication/pdf";
   Response.AppendHeader("Content-Disposition", "attachment; filename=form.pdf");
   Response.TransmitFile(reportPath);
   Response.End();
}

【问题讨论】:

  • 您拥有的任何清算事件都只会在回发时起作用。所以只有在页面加载/重新加载之后。您需要使用 Javascript 清除客户端

标签: c# asp.net download


【解决方案1】:

尝试使用javascript清除文本框值

【讨论】:

  • 请改进您的回答,了解您将如何使用 javascript 进行操作!
【解决方案2】:

我想,你可以使用get方法下载。 按钮的onClientClick代码:

function doGet(){
var txt=document.getElementById('<%=this.TextBox1.ClientID %>').value;
window.open ('download.aspx?file='+txt);
document.getElementById('<%=this.TextBox1.ClientID %>').value='';
}

并在download.aspx.cs中

可以从main.aspx中获取值,比如

string file=this.Request.QueryString["file"];

不要使用重定向方法请求下载页面。

【讨论】:

  • 问题依然存在,但是我已经尝试了另一种方法,看看方法7。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-25
  • 1970-01-01
  • 2019-09-28
  • 1970-01-01
相关资源
最近更新 更多