我在这里回顾了各种问题,但我仍然无法做到这一点。
我有一个网格视图(OrderList),在这个网格视图中,我有一个带有按钮的列,可以简单地更新一个特定的字段(基本上将一个顺序标记为已接受)。
在aspx文件中,我有以下代码
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="ButtonToAcceptOrder" runat="server" Text="Accept Order" CommandName="AcceptOrder" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>在文件后面的代码中,我有以下代码:
protected void OrderList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName =="AcceptOrder")
{
// Retrieve the row index stored in the
// CommandArgument property.
int index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button
// from the Rows collection.
GridViewRow row = OrderList.Rows[index];
Int32 oId = Int32.Parse(row.Cells[1].Text);
//Code to Update database by order id
}
}错误来自oId,表明输入格式不正确。
在网格视图的第一列中,我将显示所需的ID。而其他栏目则显示了其他细节。
如果你发现我的代码中有任何问题,你能帮忙吗?
(我以前学过本教程:http://msdn.microsoft.com/en-us/library/vstudio/bb907626(v=vs.100).aspx)
发布于 2014-05-02 19:26:58
如果每一行都有唯一的id,则应该使用GridView的GridView属性。
<GridView id="OrderList" DataKeyNames="oId"... />然后在您的RowCommand代码中,按如下方式访问它:
Int32 oId = OrderList.DataKeys[row.DataItemIndex].Value;这比试图从细胞中获取价值要干净得多。
https://stackoverflow.com/questions/23435484
复制相似问题