2013-05-02 19 views
2

我有一個在buttonclick填充一個列表框,然後當用戶選擇或更改就可以下載與其相關的文件列表框的索引。asp.net listbox_index改回發射擊上的每個按鈕

我的問題是,當他們去按一個按鈕再次搜索它下載文件的新紀錄,但低於再次發射的代碼。如何阻止它在其他按鈕上調用回發?

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(); 
} 

回答

-1

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(); 
     } 
    } 

MSDN全球化志願服務青年爲的IsPostBack:

http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx

用途使用實施例:

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

望其有用的。

+1

這將如何代碼執行過呢?列表框是一個按鈕,點擊後填充,在這一點併爲未來的回傳!Page.IsPostback永遠不會爲真 – CRice 2013-05-02 04:54:46

+0

它現在沒有運行的代碼都當我改變了指數,它只是閃爍 – stefan 2013-05-02 04:55:41

+0

嘗試設置的AutoPostBack =「真正的」到你的列表框 – CRice 2013-05-02 04:56:22