繁体   English   中英

asp.net listbox_index更改的回发在每个按钮上触发

[英]asp.net listbox_index changed postback is firing on every button

我有一个在buttonclick上填充的列表框,然后当用户选择或更改列表框上的索引时,它会下载与之相关的文件。

我的问题是当他们按下按钮来搜索新记录时,它再次下载了文件,但再次触发了下面的代码。 如何阻止它在其他按钮上调用回发?

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string splitval = ListBox1.SelectedValue.ToString();
    string[] newvar = splitval.Split(',');
    GlobalVariables.attachcrq = newvar[0];
    GlobalVariables.num = UInt32.Parse(newvar[1]);
    string filename = ListBox1.SelectedItem.ToString();
    GlobalVariables.ARSServer.GetEntryBLOB("CHG:WorkLog", GlobalVariables.attachcrq, GlobalVariables.num, Server.MapPath("~/TEMP/") + filename);

    FileInfo file = new FileInfo(Server.MapPath("~/TEMP/" + filename));
    Response.Clear();
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
    Response.AppendHeader("Content-Length", file.Length.ToString());
    Response.ContentType = ReturnExtension(file.Extension.ToLower());
    Response.TransmitFile(file.FullName);
    Response.Flush();
    Response.End();
}

IsPostback属性应在此处使用。

在条件if(!Page.Ispostback)您的代码

可以采用以下方法:

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) {

       if(!Page.IsPostback)
      {
        string splitval = ListBox1.SelectedValue.ToString();
        string[] newvar = splitval.Split(',');
        GlobalVariables.attachcrq = newvar[0];
        GlobalVariables.num = UInt32.Parse(newvar[1]);
        string filename = ListBox1.SelectedItem.ToString();
        GlobalVariables.ARSServer.GetEntryBLOB("CHG:WorkLog", GlobalVariables.attachcrq, GlobalVariables.num, Server.MapPath("~/TEMP/") + filename);

        FileInfo file = new FileInfo(Server.MapPath("~/TEMP/" + filename));
        Response.Clear();
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.AppendHeader("Content-Length", file.Length.ToString());
        Response.ContentType = ReturnExtension(file.Extension.ToLower());
        Response.TransmitFile(file.FullName);
        Response.Flush();
        Response.End();
      }
    }

IsPostBack的MSDN参考:

http://msdn.microsoft.com/zh-CN/library/system.web.ui.page.ispostback.aspx

用法示例:

http://www.geekinterview.com/question_details/60291

希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM