如
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.commandName = "....")
{
int i = Convert.toint32(e.CommandArgument.ToString()); //获取行号
LinkButton lbt1 = ((LinkButton)GridView1.Rows
string id = ((Label)GridView1.Rows
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
LinkButton lbt1 = ((LinkButton)GridView1.SelectedRow.Cells[0].FindControl("LinkButton1")); //SelectedRow获取行号,选择才能用
string id = ((Label)GridView1.SelectedRow.Cells[0].FindControl("ID")).Text;//ID绑定在一个Label上
}
GV在布局里面用的话只能用<asp:TemplateField>里面的<Template>然后就在里面绑定数据
.aspx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="None"
BorderWidth="0px"
onrowcommand="GridView1_RowCommand" AllowPaging="True"
onpageindexchanged="GridView1_PageIndexChanged" PageSize="6">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<div class="mrt2">
<ul>
<li><p class="w542px2"><span>·</span>
<asp:Label ID="ID" runat="server" Text='<%# Bind("Id") %>' Visible="false"></asp:Label>
<asp:Label ID="Title_lbe" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
<asp:Label ID="Class_lbe" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</p>
<p class="w135px"><span class="caozuo marglt24px caozuobg1"><span><a href="Article_mod_up.aspx?id=<%# Eval("Id") %>">编辑</a></span></span>
<span class="caozuo caozuobg2"><span><a href="#">
<asp:LinkButton ID="LinkButton1"
runat="server" CommandName="del" CommandArgument='<%# Eval("Id") %>' >删除</asp:LinkButton></a></span></span></p></li>
</ul>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
.CS:
protected void GridView1_RowCommand(object sender, CommandEventArgs e)
{
if (e.CommandName == "del")
{
string id = e.CommandArgument.ToString();
}
}
简短有力,和传统的获取行号法很像,但是快多了,区别在于一个是获取GV内置控件的属性,一个是自己给CommandArgument赋值