gridview中的Asp.net回发

gridview中的Asp.net回发,asp.net,Asp.net,在我的asp.net应用程序中,我有一个gridview控件,其中添加了一个带有fileupload控件的模板列。 在页面的gridview之外,我有一个按钮控件,它执行一些任务。 我的问题是,当我单击按钮时,我通过gridview中的文件上载控件选择的文件已刷新,文件路径消失。 单击按钮时,如何停止刷新GridView。 按钮不在网格内 protected void Page_Load(object sender, EventArgs e) { if (!IsPostB

在我的asp.net应用程序中,我有一个gridview控件,其中添加了一个带有fileupload控件的模板列。 在页面的gridview之外,我有一个按钮控件,它执行一些任务。 我的问题是,当我单击按钮时,我通过gridview中的文件上载控件选择的文件已刷新,文件路径消失。 单击按钮时,如何停止刷新GridView。 按钮不在网格内

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

文件上载控件永远不会在回发之间保持其值。
您可以在GridView中维护一个标签字段,该字段保存通过FileUpload控件上传的文件的路径。单击GridView外部的按钮时,将值从FileUpload控件复制到lable。

文件上载控件的设计目的不是在poskback上维护文件路径。。但是你可以有一个解决办法。。尝试将文件路径存储在会话变量中。。我知道这有点笨拙,但似乎是唯一的方法。。您可以做的另一件事是创建一个UserControl来为您管理它,以减少您的工作量

//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;
}
更多信息


有关单击gridview外部的按钮时发生的任何回发吗?如果您使用的是ASP.NET Ajax,请将gridview放置在单独的更新面板中。