GridView行号大集合

简介:
aspx页面:
<asp:GridView ID="gvDataInfo" runat="server" AutoGenerateColumns="False" OnRowCommand="gvDataInfo_RowCommand">
                    <Columns>
                        <asp:BoundField DataField="job_id" HeaderText="编号" />
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlTest_SelectedIndexChanged">
                                    <asp:ListItem Value="-1">-请选择-</asp:ListItem>
                                    <asp:ListItem Value="0">测试1</asp:ListItem>
                                    <asp:ListItem Value="1">测试2</asp:ListItem>
                                    <asp:ListItem Value="2">测试3</asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="cbTest" runat="server" AutoPostBack="True" OnCheckedChanged="cbTest_CheckedChanged" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="job_desc" HeaderText="职位描述" />
                        <asp:BoundField DataField="min_lvl" HeaderText="最小值" />
                        <asp:BoundField DataField="max_lvl" HeaderText="最大值" />
                        <asp:TemplateField HeaderText="操作">
                            <ItemTemplate>
                                <asp:LinkButton ID="lbTestOne" runat="server" CommandName="one">RowCommand事件</asp:LinkButton>
                                <asp:LinkButton ID="lbTestCommand" runat="server" CommandName="two" OnCommand="lbTestCommand_Command">LinkButton的Command事件</asp:LinkButton>
                                <asp:LinkButton ID="lbTestClick" runat="server" OnClick="lbTestClick_Click">LinkButton的Click事件</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
aspx.cs页面:
    /// <summary>
    /// 行绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvDataInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "one")
        {
            GridViewRow drv = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
            //GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //上下两种方式都可以
            int index = drv.RowIndex;//获取行号
            Response.Write("<script>alert("+index+");</script>");//行号从0开始
        }
    }
    /// <summary>
    /// LinkButton的Command事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbTestCommand_Command(object sender, CommandEventArgs e)
    {
        LinkButton lb = (LinkButton)sender;
        DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;
        GridViewRow gvr = (GridViewRow)dcf.Parent;
        int index = gvr.RowIndex;//获取行号
        Response.Write("<script>alert(" + index + ");</script>");//行号从0开始

    }
    /// <summary>
    /// LinkButton的Click事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void lbTestClick_Click(object sender, EventArgs e)
    {
        GridViewRow gvr = (GridViewRow)((LinkButton)sender).NamingContainer;
        int index = gvr.RowIndex;//获取行号
        Response.Write("<script>alert(" + index + ");</script>");//行号从0开始
    }
    /// <summary>
    /// DropDownList获取当前行
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        GridViewRow gvr = (GridViewRow)ddl.NamingContainer;
        int index = gvr.RowIndex;//获取行号
        Response.Write("<script>alert(" + index + ");</script>");//行号从0开始
    }
    /// <summary>
    /// CheckBox获取当前行
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cbTest_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent;
        GridViewRow gvr = (GridViewRow)dcf.Parent;
        int index = gvr.RowIndex;//获取行号
        Response.Write("<script>alert(" + index + ");</script>");//行号从0开始
    }



本文转自 韬光星夜 51CTO博客,原文链接:http://blog.51cto.com/xfqxj/477082,如需转载请自行联系原作者
相关文章
|
Android开发
ListView实现倒序显示
实现聊天列表主要依赖ListView的2个属性android:stackFromBottom和android:transcriptMode
338 0
|
C#
C# ListBox实现显示插入最新的数据的方法
原文:C# ListBox实现显示插入最新的数据的方法 在我们使用ListBox控件时,如果我们在里面不断的添加一条条数据,但是在我们添加的数据过多超过了ListBox显示的窗口时(此时会产生滑动条), 发现我们无法看到最新添加的数据。
1682 0
|
JavaScript 前端开发 .NET
列表ListBox、ListView、GridView 排序
列表排序 1.使用控件默认排序方式(推荐) ListControl.Items.SortDescriptions.Clear(); ListControl.Items.SortDescriptions.
872 0
|
开发框架 .NET
GridView中实现点击某行的任意位置就选中该行
来源:http://auv2009.blog.163.com/blog/static/68858712200992731010670/ 在 GridView中增加一列:(该列是选择按钮,让其不显示) 在GridView的RowDataBound事件中增加以下代码: p...
763 0
listview添加行
转自博客http://blog.csdn.net/hyouq110/article/details/6057179 ListView lv = new ListView (); //添加一行的方法 ListViewItem item = lv.
743 0