gridview设置行号与RowCommand事件中获取行号

本文介绍了如何在ASP.NET的GridView控件中动态设置行号,并在RowCommand事件中正确获取行号。通过在GridView的RowCreated事件中设置每个单元格的文本为行号,以及在模板列中使用Container.DataItemIndex获取行号作为CommandArgument,然后在RowCommand事件处理中解析CommandArgument以获取对应的行数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

aspx页面:

 

<Columns>
                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
                        ReadOnly="True" SortExpression="id" Visible="False" />
                    <asp:BoundField HeaderText="步骤">
                    <HeaderStyle Width="50px" />
                    </asp:BoundField>

</Columns>

 

 

.cs文件动态生成行号

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex > -1)
            {
                e.Row.Cells[1].Text = Convert.ToString(e.Row.RowIndex + 1);

            }
        }

 

 

 

在RowCommand中获取行号

 

在模板列中添加=<%# Container.DataItemIndex + 1 %>

 <ItemTemplate>
                            <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false"
                                CommandName="modify" Text="修改" CommandArgument=<%# Container.DataItemIndex + 1 %>></asp:LinkButton>
                        </ItemTemplate>

 

 

 

cs文件

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridView gv = ((GridView)sender);
            int rowNum = int.Parse(e.CommandArgument.ToString()) - 1;
            txbBeizhu.Text = gv.Rows[rowNum].Cells[2].Text;
            ModalPopupExtender1.Show();
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值