日期:2014-05-17  浏览次数:20361 次

关于gridview的按钮
我想在gridview里添加一个按钮,点这个按钮后可以查看这一行信息更详细的信息。要怎么办呢?
我现在这样写,但是都说e.RowIndex缺少命名空间呢,但是我查过不少的啊~~
C# code

<asp:GridView ID="GridView1" runat="server"  AutoGenerateColumns="False" 
          Width="90%" AllowPaging="True" PageSize="15" style="margin-top:0px" AllowSorting="True" 
       onpageindexchanging="GridView1_PageIndexChanging" 
          onsorting="GridView1_Sorting" Font-Bold="True" 
          onrowcommand="GridView1_RowCommand" onrowediting="GridView1_RowEditing">


        <Columns>
            
            <asp:BoundField DataField="local_name" HeaderText="地区" SortExpression="local_name"  />    
            
             <asp:TemplateField>
              <ItemTemplate> 
                   <asp:LinkButton ID="check_info" runat="server" CommandName="checks">查看详情</asp:LinkButton>
                </ItemTemplate> 
                </asp:TemplateField>   
         </Columns>
      </asp:GridView>




后台代码就一句话,可是都说e.RowInde有问题
C# code


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        Label3.Text =  GridView1.DataKeys[e.RowIndex].Value.ToString();
       
    }



------解决方案--------------------
RowCommand事件的GridViewCommandEventArgs 类型没有e.RowIndex的属性,你可以使用e.CommandArgument来传递Key


<asp:LinkButton ID="check_info" runat="server" CommandName="checks" CommandArgument='<%#Eval("ID") %>'>查看详情</asp:LinkButton>



protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

if (e.CommandName == "checks")
{
String id = e.CommandArgument.ToString();
}
}
------解决方案--------------------
是主键

如果值也是唯一的,不用主键行
------解决方案--------------------
int index = Convert.ToInt32(e.CommandArgument);