无法在GridView中保留数据

时间:2015-09-16 11:06:37

标签: c# asp.net .net gridview

有一个GridView但无法检索回发后的任何数据。我在Page_InitPage_Load的开头设置了一个断点,在触发的任何回发时GridView.Rows.Count属性始终为0. GridView的定义如下所示:< / p>

        <asp:GridView ID="TestGrid" runat="server" AllowSorting="False" DataKeyNames="ID,stock,percentage"
            OnRowDataBound="GridViewTest_RowDataBound" EnableViewState="True" 
            OnRowUpdating="GridViewTest_OnRowUpdating" OnRowEditing="GridViewTest_OnRowEditing" OnRowCancelingEdit="GridViewTest_OnRowCancelingEdit"
            AutoGenerateColumns="False">
            <Columns>
                <asp:CommandField ShowEditButton="True" CausesValidation="False"/>
                <asp:BoundField DataField="Client" HeaderText="Client" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="Stock" HeaderText="Stock" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="DutyStatus" HeaderText="DutyStatus" InsertVisible="False" ReadOnly="True" />
                <asp:TemplateField HeaderText="Percentage" InsertVisible="False">
                    <ItemTemplate>
                        <asp:Label ID="enhLbl" runat="server"><%#Eval("Percentage")%></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TbPercentage" runat="server" Text="<%#Bind("Percentage")%>" class="edit-field"></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

我在IsPostBack中检查Page_Load,如果它不是回发版,则只将GridView绑定到其DataSource。 它重新绑定的唯一时间是GridViewTest_OnRowEditingGridViewTest_OnRowUpdatingGridViewTest_OnRowCancelingEdit事件处理程序。

我通过以下方式将GridView绑定到DataTable

    AGridView.DataSource = DataTableSplitParcels;
    AGridView.DataBind();
    AGridView.EnableViewState = true;

我不知道它是否有任何区别,但DataTable是在代码中创建的,而行是在循环中添加的。

在回复的Page_Load中,我可以看到GridView的{​​{1}}属性填充了当前值,但是如果没有行,我将无法检索任何更新的值。

1 个答案:

答案 0 :(得分:0)

问题出现在Page_Load事件中,即每次调用都会刷新GridView(无论回发),因此您在更新期间丢失了用户输入的内容。

所以在你的Page_LoadBindData事件上放一个调试器,看看它是否被Page_Load调用了,你需要停止它...我尝试了一个示例应用程序,它在我的结局..见下面我试过的代码。

<强> ASPX

<asp:GridView ID="TaskGridView" runat="server" 
        AutoGenerateEditButton="True" 
        AllowPaging="true"
        OnRowEditing="TaskGridView_RowEditing"         
        OnRowCancelingEdit="TaskGridView_RowCancelingEdit" 
        OnRowUpdating="TaskGridView_RowUpdating"
        OnPageIndexChanging="TaskGridView_PageIndexChanging">
      </asp:GridView>

<强> CS

protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        // Create a new table.
        DataTable taskTable = new DataTable("TaskList");

        // Create the columns.
        taskTable.Columns.Add("Id", typeof(int));
        taskTable.Columns.Add("Description", typeof(string));
        taskTable.Columns.Add("IsComplete", typeof(bool));

        //Add data to the new table.
        for (int i = 0; i < 20; i++)
        {
            DataRow tableRow = taskTable.NewRow();
            tableRow["Id"] = i;
            tableRow["Description"] = "Task " + i.ToString();
            tableRow["IsComplete"] = false;
            taskTable.Rows.Add(tableRow);
        }

        //Persist the table in the Session object.
        Session["TaskTable"] = taskTable;

        //Bind data to the GridView control.
        BindData();
    }
}

protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //Retrieve the table from the session object.
    DataTable dt = (DataTable)Session["TaskTable"];

    //Update the values.
    GridViewRow row = TaskGridView.Rows[e.RowIndex];
    dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;

    //Reset the edit index.
    TaskGridView.EditIndex = -1;

    //Bind data to the GridView control.
    BindData();
}

private void BindData()
{
    TaskGridView.DataSource = Session["TaskTable"];
    TaskGridView.DataBind();
}

Source available here