HTML Code
<asp:GridView ID="GridView1" DataKeyNames="ID" runat="server">
<Columns>
<asp:boundfield DataField="Name" HeaderText="Name"></asp:boundfield>
<asp:templatefield>
<itemtemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
</itemtemplate>
</asp:templatefield>
</Columns>
</asp:GridView>
C# code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.GridView1.DataSource = GetTable();
this.GridView1.DataBind();
}
}
private DataTable GetTable()
{
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
for (int i = 0; i < 5; i++)
{
dt.Rows.Add(new string[] { "ID"+i.ToString(), "Name"+i.ToString()});
}
return dt;
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
//行号
int row = ((GridViewRow)((LinkButton)sender).NamingContainer).RowIndex;
//所点击行第一列Name的值
Response.Write(this.GridView1.Rows[row].Cells[0].Text);
//所点击行的ID
Response.Write(this.GridView1.DataKeys[row].Value.ToString());
}
翅膀
|
菜鸟二级
|园豆:335
|
2008-03-26 21:47