Gridview Paging时保留文本框值

时间:2013-09-12 10:50:27

标签: asp.net gridview

在页面索引上我无法保持TextBox中的给定值,我尝试了不同的逻辑但没有给出正确的结果

这里是代码

    protected void gvViolationCodes_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        RememberOldValues();
        //to rebind the data based on changed page index
        violationCodePresenter.GetViolationCodesList(Convert.ToInt32(ddlStatus.SelectedValue), Convert.ToInt32(Session["LanguageID"]));
        gvViolationCodes.PageIndex = e.NewPageIndex;
        gvViolationCodes.DataBind();
        RePopulateValues();
    }

以及存储先前值的方法如下

     private void RememberOldValues()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("row_index");
        dt.Columns.Add("edited_value");
        foreach (GridViewRow gvr in gvViolationCodes.Rows)
        {
            TextBox tb = (TextBox)gvr.FindControl("txtSeqNo");
            HiddenField hf = (HiddenField)gvr.FindControl("HiddenField1");
            if (tb.Text != hf.Value)
            {
                DataRow dr = dt.NewRow();
                dr["row_index"] = gvr.RowIndex;
                dr["edited_value"] = tb.Text;
                dt.Rows.Add(dr);
            }
        }
        if (dt.Rows.Count == 0 && Session["retain"] == null && Session["page-index"] == null)
        {
            Session["retain"] = null;
            Session["page-index"] = null;
        }
        else if (dt.Rows.Count > 0 && Session["retain"] == null && Session["page-index"] == null)
        {
            Session["retain"] = dt;
            Session["page_index"] = gvViolationCodes.PageIndex;
        }
        else if (Session["retain"] == null && Session["page-index"] == null)
        {
            Session["retain"] = dt;
            Session["page_index"] = gvViolationCodes.PageIndex;
        }
    }

1 个答案:

答案 0 :(得分:0)

使用ViewState对象来保存因网格视图中的分页而发生的页面回发中的文本框值。