第一种方法:在onselectedindexchanged中写事件
第二种方法:OnRowCommand事件
前端页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="no" onselectedindexchanged="GridView1_SelectedIndexChanged" OnRowCommand="TestGridView_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="TestLinkBtn" runat="server"
CommandName="Increase"
CommandArgument='<%#Eval("no")+","+Eval("MAC") %>'
Text="CommandArgument示例"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="no" HeaderText="no1" Visible="false"/>
<asp:BoundField DataField="MAC" HeaderText="name1"/>
<asp:BoundField DataField="DnsName" HeaderText="password1"/>
<asp:BoundField DataField="Domain" HeaderText="role1"/>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
后台页面:
第一种方法:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
this.Label1.Text = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
}
第二种方法:
protected void TestGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Increase")
{
//int index = Convert.ToInt32(e.CommandArgument);
//this.Label1.Text = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
this.Label1.Text = e.CommandArgument.ToString();
//GridViewRow row = GridView1.Rows[index];
Response.Write("<script language=javascript>alert('haahah')</script>");
}
}