gpt4 book ai didi

c# - asp.net listbox_index 更改后的回传在每个按钮上触发

转载 作者:太空宇宙 更新时间:2023-11-03 17:03:12 27 4
gpt4 key购买 nike

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

我遇到的问题是,当他们按下按钮搜索新记录时,它会再次下载文件,但会再次触发下面的代码。我怎样才能阻止它在其他按钮上调用回发?

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/en-us/library/system.web.ui.page.ispostback.aspx

示例用法:

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

希望对你有帮助。

关于c# - asp.net listbox_index 更改后的回传在每个按钮上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16330491/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com