单击按钮时在 gridview 中保留文本框值

Preserve textbox values inside a gridview on button click

我有一个可编辑的 GridView,它有一个 AddRow 按钮,单击该按钮会添加一个由 text boxes 组成的空行。

每当我在这些 textboxes 中插入值并单击 AddRowDelete 按钮时,文本框就会丢失它们的值。

我敢肯定,这不会因为 postback 而发生,因为还有一个 Save 按钮,单击该按钮通常会保留值。

下面是标记:

<asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="false"
            OnRowDeleting="GridView1_RowDeleting">
            <Columns>
                <asp:TemplateField HeaderText="Col1">
                    <ItemTemplate>
                        <asp:TextBox runat="server" ID="txt1"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Col2">
                    <ItemTemplate>
                        <asp:TextBox ID="txt2" runat="server"></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Col3">
                    <ItemTemplate>
                        <asp:TextBox ID="txt3" runat="server"></asp:TextBox>
                    </ItemTemplate>
                    <FooterStyle HorizontalAlign="Right" />
                    <FooterTemplate>
                        <asp:Button ID="btnAddNewRow" runat="server" Text="AddRow" OnClick="Add" />
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:CommandField ButtonType="Button" ShowDeleteButton="true" />
            </Columns>
        </asp:GridView>
        <asp:Button ID="btnSavetoDB" runat="server" Text="save" OnClick="btnSavetoDB_Click" />

隐藏代码:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetInitialRow();
            }
        }
//Sets the first empty row to the grid view    
private void SetInitialRow()
            {
                DataTable dt = new DataTable();
                DataRow dr = null;
                dt.Columns.Add(new DataColumn("Column1", typeof(string)));
                dt.Columns.Add(new DataColumn("Column2", typeof(string)));
                dt.Columns.Add(new DataColumn("Column3", typeof(string)));
                dr = dt.NewRow();
                dr["Column1"] = string.Empty;
                dr["Column2"] = string.Empty;
                dr["Column3"] = string.Empty;
                dt.Rows.Add(dr);
                //Store the DataTable in ViewState
                ViewState["CurrentTable"] = dt;
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
//Adds a new empty row to the grid view    
protected void Add(object sender, EventArgs e)
            {
                int rowIndex = 0;
                if (ViewState["CurrentTable"] != null)
                {
                    DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                    DataRow drCurrentRow = null;
                    if (dtCurrentTable.Rows.Count > 0)
                    {
                        for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
                        {
                            //extract the TextBox values
                            TextBox box1 = (TextBox)GridView1.Rows[rowIndex].Cells[0].FindControl("txt1");
                            TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txt2");
                            TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("txt3");
                            drCurrentRow = dtCurrentTable.NewRow();
                            //drCurrentRow["RowNumber"] = i + 1;
                            drCurrentRow["Column1"] = box1.Text;
                            drCurrentRow["Column2"] = box2.Text;
                            drCurrentRow["Column3"] = box3.Text;
                            rowIndex++;
                        }
                        //add new row to DataTable
                        dtCurrentTable.Rows.Add(drCurrentRow);
                        //Store the current data to ViewState
                        ViewState["CurrentTable"] = dtCurrentTable;
                        //Rebind the Grid with the current data
                        GridView1.DataSource = dtCurrentTable;
                        GridView1.DataBind();
                    }
                }
                else
                {
                    Response.Write("ViewState is null");
                }
            }
//Called on Delete Button click, which is inside a CommanField
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int num = GridView1.Rows.Count;
            int index = Convert.ToInt32(e.RowIndex);
            if (num > 1)
            {
                DataTable dt = ViewState["CurrentTable"] as DataTable;
                dt.Rows[index].Delete();
                ViewState["CurrentTable"] = dt;
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
        }

初始快照:

点击添加行按钮后:

我哪里错了?

请专家帮忙

此致

问题是您没有在标记中使用 Eval

<asp:TextBox runat="server" ID="txt1" Text='<%# Eval("Column1") %>'></asp:TextBox>

优化代码隐藏。不需要 ViewState

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var table = CreateDataTable();
        table.Rows.Add("", "", "");
        BindGridView(table);
    }
}

//Sets the first empty row to the grid view
private DataTable CreateDataTable()
{
    var dt = new DataTable
    {
        Columns = { "Column1", "Column2", "Column3" }
    };
    return dt;
}

private void BindGridView(DataTable table)
{
    GridView1.DataSource = table;
    GridView1.DataBind();
}
private DataTable PopulateTableFromGridView()
{
    var table = CreateDataTable();
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        //extract the TextBox values
        TextBox box1 = (TextBox)GridView1.Rows[i].FindControl("txt1");
        TextBox box2 = (TextBox)GridView1.Rows[i].FindControl("txt2");
        TextBox box3 = (TextBox)GridView1.Rows[i].FindControl("txt3");
        table.Rows.Add(box1.Text, box2.Text, box3.Text);
    }
    return table;
}
//Adds a new empty row to the grid view
protected void Add(object sender, EventArgs e)
{
    var newTable = PopulateTableFromGridView();
    newTable.Rows.Add("", "", "");
    BindGridView(newTable);
}

//Called on Delete Button click, which is inside a CommanField
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    var dt = PopulateTableFromGridView();
    dt.Rows[e.RowIndex].Delete();
    if (dt.Rows.Count == 0)
    {
        dt.Rows.Add("", "", "");
    }
    BindGridView(dt);
}

内联表达式的链接

  1. http://support.microsoft.com/kb/976112
  2. http://weblogs.asp.net/ahmedmoosa/embedded-code-and-inline-server-tags