gridview模板列LinkButton获取行索引

我在gridview的模板列中放置了一个LinkButton,用做一个处理操作的按钮,原来打算在LinkButton的click事件里面处理,后来处理的时候发现,获取LinkButton所触发的行的索引有点困难,考虑用findcontrol,但是后来没有搞好.

所以用了下面这种方面.

aspx页面代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White"
            BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1"
            GridLines="None" Width="662px" OnRowCommand="GridView1_RowCommand" OnRowCreated="GridView1_RowCreated">
            <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
            <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
            <Columns>
                <asp:BoundField DataField="SID" HeaderText="ID" />
                <asp:BoundField DataField="title" DataFormatString="{0:c}" HeaderText="问卷列表" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkButton1" runat="server"  CommandName="Down" Text="点击下载"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
            <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
        </asp:GridView> 

 

下面就利用RowCreated事件来为模版列中LinkButton写入CommandArgument事件。

下面的代码是如何使用 RowCreated 事件将正在创建的行的索引存储在该行中所包含的 LinkButton 控件的 CommandArgument 属性中。这允许您确定在用户单击 LinkButton 控件按钮时包含该控件的行的索引。


 /// <summary>

    /// 为模版列LinkButton写入CommandArgument事件

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {

            // Retrieve the LinkButton control from the first column.

            LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");

            // Set the LinkButton's CommandArgument property with the row's index.

            LinkButton1.CommandArgument = e.Row.RowIndex.ToString();

        }

    }
    /// <summary>
    /// linkbutton的单击事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //下载按扭

        if (e.CommandName == "Down")
        {

            //设置下载行高亮显示

            this.GridView1.EditRowStyle.BackColor = Color.FromName("#F7CE90");

            //string index= this.GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value.ToString();

            int index = Convert.ToInt32(e.CommandArgument);

            GridViewRow row = GridView1.Rows[index];

            int sid =int.Parse( row.Cells[0].Text.ToString());
            //save(sid);
            //down(sid);    这两个是我用到的函数...呵呵
        }
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值