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

 

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

posted @ 2006-12-04 09:44  永春  阅读(1083)  评论(0)    收藏  举报