aspx页面:
<asp:GridView ID="gvDataInfo" runat="server" AutoGenerateColumns="False" OnRowCommand="gvDataInfo_RowCommand">
<Columns>
<asp:BoundField DataField="job_id" HeaderText="编号" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
<asp:ListItem Value="-1">-请选择-</asp:ListItem>
<asp:ListItem Value="0">测试1</asp:ListItem>
<asp:ListItem Value="1">测试2</asp:ListItem>
<asp:ListItem Value="2">测试3</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:CheckBox ID="cbTest" runat="server" AutoPostBack="True" OnCheckedChanged="cbTest_CheckedChanged" />
<asp:BoundField DataField="job_desc" HeaderText="职位描述" />
<asp:BoundField DataField="min_lvl" HeaderText="最小值" />
<asp:BoundField DataField="max_lvl" HeaderText="最大值" />
<asp:TemplateField HeaderText="操作">
<asp:LinkButton ID="lbTestOne" runat="server" CommandName="one">RowCommand事件</asp:LinkButton>
<asp:LinkButton ID="lbTestCommand" runat="server" CommandName="two" OnCommand="lbTestCommand_Command">LinkButton的Command事件</asp:LinkButton>
<asp:LinkButton ID="lbTestClick" runat="server" OnClick="lbTestClick_Click">LinkButton的Click事件</asp:LinkButton>
</Columns>
</asp:GridView>
aspx.cs页面:
/// <summary>
/// 行绑定事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gvDataInfo_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "one")
{
GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
//GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //上下两种方式都可以
int index = drv.RowIndex;//获取行号
Response.Write("<script>alert("+index+");</script>");//行号从0开始
}
}
/// LinkButton的Command事件
protected void lbTestCommand_Command(object sender, CommandEventArgs e)
LinkButton lb = (LinkButton)sender;
DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
GridViewRow gvr = (GridViewRow)dcf.Parent;
int index = gvr.RowIndex;//获取行号
Response.Write("<script>alert(" + index + ");</script>");//行号从0开始
/// LinkButton的Click事件
protected void lbTestClick_Click(object sender, EventArgs e)
GridViewRow gvr = (GridViewRow)((LinkButton)sender).NamingContainer;
/// DropDownList获取当前行
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
DropDownList ddl = (DropDownList)sender;
GridViewRow gvr = (GridViewRow)ddl.NamingContainer;
/// CheckBox获取当前行
protected void cbTest_CheckedChanged(object sender, EventArgs e)
CheckBox chk = (CheckBox)sender;
DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent;
本文转自 韬光星夜 51CTO博客,原文链接:http://blog.51cto.com/xfqxj/477082,如需转载请自行联系原作者