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;
    }

相关文章:

  • 2022-12-23
  • 2021-09-27
  • 2021-08-10
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
猜你喜欢
  • 2022-12-23
  • 2021-10-19
  • 2021-11-23
  • 2022-12-23
  • 2021-10-10
相关资源
相似解决方案