2012-01-04 93 views
0

在我的asp.net應用程序中,我有一個gridview控件,其中我添加了一個帶有fileupload控件的模板列。 而在頁面的gridview外,我有一個按鈕控件,它執行一些任務。 我的問題是,當我點擊按鈕,我通過gridview中的文件上傳控件選擇的文件得到刷新,文件路徑消失。 當我點擊按鈕時,如何停止刷新gridiew。 按鈕不在網格內。在gridview中的Asp.net回發

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      DataTable dt = new DataTable(); 

      DataColumn dc1 = new DataColumn("id", typeof(string)); 
      dt.Columns.Add(dc1); 
      dr = dt.NewRow(); 
      dr[0] = "abcd"; 
      dt.Rows.Add(dr); 
      DataSet ds = new DataSet(); 
      ds.Tables.Add(dt); 
      GridView1.DataSource = ds; 
      GridView1.DataBind(); 
     } 
    } 
+0

點擊gridview外部的按鈕時發生的任何回發? – Pavan 2012-01-04 05:45:49

+0

如果您正在使用ASP.NET Ajax,請將您的GridView放置在單獨的更新面板中。 – Pavan 2012-01-04 05:53:50

回答

0

文件上傳控制的目的不是爲了維護poskback上的文件路徑..但​​你可以有一個解決方法..嘗試在會話變量中存儲文件路徑..我知道這有點笨拙,但似乎是唯一的方法來做到這一點。 。一個你可以做更多的事情,以減輕你的努力是創建一個用戶控件將管理此你...

//If first time page is submitted and we have file in FileUpload control but not in session 
     // Store the values to SEssion Object 
     if (Session["FileUpload1"] == null && FileUpload1.HasFile) 
{ 
Session["FileUpload1"] = FileUpload1; 
Label1.Text = FileUpload1.FileName; 
} 
// Next time submit and Session has values but FileUpload is Blank 
     // Return the values from session to FileUpload 
     else if (Session["FileUpload1"] != null && (! FileUpload1.HasFile)) 
{ 
FileUpload1 = (FileUpload) Session["FileUpload1"]; 
Label1.Text = FileUpload1.FileName; 
} 
// Now there could be another sictution when Session has File but user want to change the file 
     // In this case we have to change the file in session object 
     else if (FileUpload1.HasFile) 
{ 
Session["FileUpload1"] = FileUpload1; 
Label1.Text = FileUpload1.FileName; 
} 

更多信息

http://www.codeproject.com/Tips/101834/How-to-Maintain-FileUpload-Control-s-State-after-P.aspx

問候

0

文件上傳控件永遠不會在回發之間保留其值。 您可以在GridView中維護一個Label字段,該字段通過FileUpload控件保存文件的路徑。當你點擊GridView外部的按鈕時,將FileUpload控件的值複製到標籤。