1、放入按钮控件时, 可直接使用e.CommandArgument取得
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
object aaa = e.CommandArgument;
object bbb = e.CommandName;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex == -1)
{
return;
}
((LinkButton)e.Row.Cells[2].FindControl("LinkButton1")).CommandArgument = Convert.ToString(e.Row.RowIndex);
((LinkButton)e.Row.Cells[2].FindControl("LinkButton2")).CommandArgument = Convert.ToString(e.Row.RowIndex);
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="aa" HeaderText="aaaaa" />
<asp:ButtonField CommandName="bbb" HeaderText="tttt" Text="按钮" />
<asp:TemplateField HeaderText="awfaw" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="ccccc"
Text="按钮"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="false" CommandName="dddd"
Text="按钮"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
可以动态设置每个命令是在第几行的
2、放入DropDownList等控件时:
可以使用AccessKey来设置
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
int i = e.Row.RowIndex;
if (i < 10)
{
((DropDownList)e.Row.Cells[2].FindControl("DropDownList1")).AccessKey = Convert.ToString(e.Row.RowIndex);
}
else
{
string ww = Convert.ToString(Convert.ToChar((i+55)));
((DropDownList)e.Row.Cells[2].FindControl("DropDownList1")).AccessKey = ww;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
object aaa = ((DropDownList)sender).AccessKey;
}
本文转自永春博客园博客,原文链接http://www.cnblogs.com/firstyi/archive/2006/12/04/581098.html:,如需转载请自行联系原作者