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


    本文转自永春博客园博客,原文链接http://www.cnblogs.com/firstyi/archive/2006/12/04/581098.html:,如需转载请自行联系原作者


相关文章
|
2月前
|
C#
45.c#:listview控件
45.c#:listview控件
12 1
|
Java Android开发
GridView(网格视图)的基本使用
本节给大家介绍的是第二个Adapter类的控件——GridView(网格视图),见名知义,ListView是列表, GridView就是显示网格!他和ListView一样是AbsListView的子类!很多东西和ListView都是相通的, 本节我们就来学习他的基本用法。
136 0
|
C#
WPF 实现 DataGrid/ListView 分页控件
原文:WPF 实现 DataGrid/ListView 分页控件 在WPF中,通常会选用DataGrid/ListView进行数据展示,如果数据量不多,可以直接一个页面显示出来。如果数据量很大,2000条数据,一次性显示在一个页面中,不仅消耗资源,而且用户体验也很糟糕。
1876 0
|
.NET 开发框架 Go
GridView控件自定义分页的实现
前人栽树,后人乘凉,话不多说,代码如下:     实现方式一: .aspx: [c-sharp] view plain copy <form id="form1" runat="server">       <table style="width: 605px">         .
1413 0
|
数据库 索引