Gridview中的CommandArgument

时间:2011-12-13 18:07:11

标签: c# asp.net gridview

我有这样的网格视图。

<asp:GridView ID="grdMerchant" runat="server" GridLines="None"
   HeaderStyle-Font-Bold="True" AutoGenerateColumns="False" AllowSorting="True" ShowHeader="false" OnRowDataBound="grdMerchant_RowDataBound" OnRowCommand="grdMerchant_RowCommand" DataKeyNames="OrderID" style="table-layout:auto;width:100%;" >  


<asp:TemplateField >
<ItemTemplate>
   <asp:Linkbutton ID= "btnView" runat="server" Text="View" OnClick="btnView_OnClick" CommandArgument='<%#Eval("OrderID")%>' ></asp:Linkbutton>

 

如何获取所选行的OrderID。我尝试使用

int OrderID = (int)grdMerchant.DataKeys[row.RowIndex][2];

但它变为空,我知道这不是方法。帮我。

提前谢谢!

1 个答案:

答案 0 :(得分:22)

试试这个

 <asp:GridView ID="grd1" runat="Server" width="500px" AutoGenerateColumns="false" DataKeyNames="StateID" OnRowEditing="grd1_RowEditing">
  <Columns>
       <asp:TemplateField>
              <ItemTemplate>
                      <asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%#Eval("StateID")%>' OnCommand="lnkDelete" Text="Delete">
                      </asp:LinkButton>
               </ItemTemplate>
        </asp:TemplateField>
   </Columns>

 protected void lnkDelete(Object sender, CommandEventArgs e)
 {
    int iStID=int32.Parse(e.CommandArgument.ToString());
 }

 //iStID has the DataKey value which you can use.