GridView控件使用(在GridView中放入其他控件的情况如何取得当前行)

本文介绍如何在ASP.NET中使用GridView控件,通过设置CommandArgument和AccessKey实现对不同行的操作。提供了具体的代码示例,展示了如何为每个行内的按钮和下拉列表设置动态属性。

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值