有一个GridView
但无法检索回发后的任何数据。我在Page_Init
和Page_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_OnRowEditing
,GridViewTest_OnRowUpdating
和GridViewTest_OnRowCancelingEdit
事件处理程序。
我通过以下方式将GridView
绑定到DataTable
:
AGridView.DataSource = DataTableSplitParcels;
AGridView.DataBind();
AGridView.EnableViewState = true;
我不知道它是否有任何区别,但DataTable
是在代码中创建的,而行是在循环中添加的。
在回复的Page_Load中,我可以看到GridView
的{{1}}属性填充了当前值,但是如果没有行,我将无法检索任何更新的值。
答案 0 :(得分:0)
问题出现在Page_Load
事件中,即每次调用都会刷新GridView
(无论回发),因此您在更新期间丢失了用户输入的内容。
所以在你的Page_Load
和BindData
事件上放一个调试器,看看它是否被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();
}