首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

关于gridview的按钮,该怎么处理

2012-06-05 
关于gridview的按钮我想在gridview里添加一个按钮,点这个按钮后可以查看这一行信息更详细的信息。要怎么办

关于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);

热点排行