asp.net GridView 未正确更新
我有一个带有这些参数的 Gridview:
<asp:GridView runat="server" ID="ItemGrid" CssClass="Grid"
AutoGenerateColumns="false"
AutoGenerateDeleteButton="true" OnRowDeleting="RowDeleting"
AutoGenerateEditButton="true" onRowEditing="RowEdit"
OnRowCancelingEdit="CancelRowEdit" onRowUpdating="RowUpdating"
DataKeyNames="Item_ID">
<Columns>
<asp:BoundField HeaderText="Item" DataField="Item"/>
<asp:BoundField HeaderText="Family" DataField="Family"/>
<asp:BoundField HeaderText="Structure" DataField="Structure"/>
<asp:BoundField HeaderText="Updated" ReadOnly="true" DataFormatString="{0:d}" DataField="Updated"/>
</Columns>
</asp:GridView>
更新时调用:
protected void RowUpdating(object sender, GridViewUpdateEventArgs e){
int Item_ID = (int)this.ItemGrid.DataKeys[e.RowIndex][0];
//Problem is something right here:
string Item = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string Family = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string Structure = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
ItemTableAdapter taItem = new ItemTableAdapter();
taItem.UpdateItem(Item, Family, Structure, DateTime.Now, Item_ID);
//just a <asp:Label> for seeing some output.
Alert.Text= string.Format("Item:{0}Family:{1}Structure:{2}",Item,Family,Structure);
this.ItemGrid.EditIndex = -1;
dataBind();
}
它生成更新/编辑/删除按钮,我的删除功能完全按照我想要的方式工作,“编辑”按钮生成可编辑的文本框。
我的问题是在更新部分,字符串 Item、Family、Structure 正在获取旧值,而不是我在生成的文本框中放入的新值。
如果我对值进行硬编码,它们将更新到数据库,并且 DateTime.Now
始终在数据库中正确更新,因此更新查询正在工作。
我已经看这个/阅读论坛测试了几天了。我确信我只是错过了一些我忽略的简单的东西。
感谢您的任何帮助。
编辑: 它已经得到回答,但对于那些好奇的人来说,这是我的 dataBind();
protected void dataBind()
{
ItemTableAdapter taItem = new ItemTableAdapter();
this.ItemGrid.DataSource = taItem.GetActive();
this.ItemGrid.DataBind();
}
I have a Gridview with these parameters:
<asp:GridView runat="server" ID="ItemGrid" CssClass="Grid"
AutoGenerateColumns="false"
AutoGenerateDeleteButton="true" OnRowDeleting="RowDeleting"
AutoGenerateEditButton="true" onRowEditing="RowEdit"
OnRowCancelingEdit="CancelRowEdit" onRowUpdating="RowUpdating"
DataKeyNames="Item_ID">
<Columns>
<asp:BoundField HeaderText="Item" DataField="Item"/>
<asp:BoundField HeaderText="Family" DataField="Family"/>
<asp:BoundField HeaderText="Structure" DataField="Structure"/>
<asp:BoundField HeaderText="Updated" ReadOnly="true" DataFormatString="{0:d}" DataField="Updated"/>
</Columns>
</asp:GridView>
On updating it calls:
protected void RowUpdating(object sender, GridViewUpdateEventArgs e){
int Item_ID = (int)this.ItemGrid.DataKeys[e.RowIndex][0];
//Problem is something right here:
string Item = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string Family = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string Structure = ((TextBox)ItemGrid.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
ItemTableAdapter taItem = new ItemTableAdapter();
taItem.UpdateItem(Item, Family, Structure, DateTime.Now, Item_ID);
//just a <asp:Label> for seeing some output.
Alert.Text= string.Format("Item:{0}Family:{1}Structure:{2}",Item,Family,Structure);
this.ItemGrid.EditIndex = -1;
dataBind();
}
It generates the Update/Edit/Delete buttons, my Delete function is working exactly how I want and the 'Edit' button generates editable TextBoxes as it should.
My problem is in the updating part, the strings Item, Family, Structure are getting the old values, not the new values I put in the generated text boxes.
If I hard code in values they are updated to the database and the DateTime.Now
is always updating correctly in the database so the update query is working.
I've been looking at this/reading forums testing things for a couple days now. I'm sure I'm just missing something simple that I have overlooked.
Thanks for any help.
Edit:
It has been answered but for those who were curious this is my dataBind();
protected void dataBind()
{
ItemTableAdapter taItem = new ItemTableAdapter();
this.ItemGrid.DataSource = taItem.GetActive();
this.ItemGrid.DataBind();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否错误地在回发时重新绑定了 GridView?您应该仅在初始加载时从数据库中获取数据:
Are you re-binding your GridView on postback by mistake? You should only fetch the data from the database on initial load:
RowUpdating
和RowUpdated
在不同时间触发。看看这不是你的问题。RowUpdating
andRowUpdated
fire at different times. See if that isn't your problem.尝试使用以下方法获取新值:
Try using the following method to get your new values: