2012-01-04 86 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控件的值复制到标签。