0

在页面索引上,我无法在 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;
        }
    }
4

1 回答 1

0

使用ViewState对象在页面回发中保存文本框值,这是由于 gridview 中的分页而发生的。

于 2013-09-12T10:53:04.060 回答