如果你想在更清洁的方式来获取 '订单ID',你可以使用CommandName
,CommandArgument
性能和OnRowCommand
事件是这样的:
<asp:GridView (...) OnRowCommand="Gv_RowCommand" (...)>
...
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select"
CommandArgument='<%# Bind("orderId") %>' Text="Button"></asp:LinkButton>
,并在后面的代码:
protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int selectedOrderId = Convert.ToInt32(e.CommandArgument);
// ...
}
}
我希望这是你想要做的。
编辑 - 我的回答对您的评论:
然后,它有点复杂,它的使用在某种程度上“selectedRow”。在我自己的代码,我用这个方法:
<asp:GridView ID="gv1" (...) DataKeyNames="orderId,email,username"
OnRowCommand="Gv_RowCommand" (...)>
...
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select"
CommandArgument='<%# DataBinder.Eval(Container,"RowIndex") %>' Text="Button">
</asp:LinkButton>
并在后面的代码:
protected void Gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
int selectedRowIndex = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "Select")
{
int orderId = Convert.ToInt32(gv1.DataKeys[selectedRowIndex]["orderId"]);
string email = gv1.DataKeys[selectedRowIndex]["email"].ToString();
// ...
}
}
被绑定到小区2什么数据字段? –
@Lucasus - 订单号 – Oshrib