在GridView的RowCommand事件中获取当前点击单元格的参数以及表分页

本文主要记录获取GridView中某一被点击单元格的参数(例如行引)

前端代码:
包含的设置方法,该部分引用参考了其他人写的
分页属性设置参考

<!--关键:操作列中LinkButton的配置CommandArgument="<%# Container.DataItemIndex %>"-->
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="100%"
                ForeColor="#333333" AllowSorting="True" PageSize="20" OnRowCommand="GridView1_RowCommand" AllowPaging="True"
                OnPageIndexChanging="GridView1_PageIndexChanging" ShowHeaderWhenEmpty="True" UseAccessibleHeader="False" AllowCustomPaging="False">
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    
                    <asp:TemplateField HeaderText="序号" InsertVisible="True">
                        <HeaderStyle Width="10%" />
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# this.GridView1.PageIndex * this.GridView1.PageSize + this.GridView1.Rows.Count + 1%>' />
                        </ItemTemplate>
                        <ItemStyle VerticalAlign="Middle" Height="60px" />
                    </asp:TemplateField>


                    <asp:BoundField DataField="文件名称" HeaderText="文件名称">
                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50%" />
                        <ItemStyle VerticalAlign="Middle" Height="60px" />
                    </asp:BoundField>


                    <asp:TemplateField HeaderText="操作">
                        <ItemTemplate>
                            <asp:LinkButton Height="25px" ForeColor="#0099ff" runat="server" ID="LbtnXG" Text=" 预览 " CommandName="btn_YL" CommandArgument="<%# Container.DataItemIndex %>"/>&nbsp&nbsp&nbsp
                            <asp:LinkButton Height="25px" ForeColor="#0099ff" runat="server" ID="LbtnZM" Text=" 下载 " CommandName="btn_XZ" CommandArgument="<%# Container.DataItemIndex %>"/>&nbsp&nbsp&nbsp
                        </ItemTemplate>
                        <HeaderStyle Width="40%" Height="20px" />
                        <ItemStyle VerticalAlign="Middle" Height="60px" />
                    </asp:TemplateField>
                </Columns>
                <PagerTemplate>
                     当前第:<asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
                     页/共: <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label><asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Visible="<%#((GridView)Container.NamingContainer).PageIndex != 0 %>">首页</asp:LinkButton>
                     <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>">上一页</asp:LinkButton>
                     <%--//如果该分页是尾页,那么该连接就不会显示了--%>
                     <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">下一页</asp:LinkButton>
                     <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Visible="<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>">尾页</asp:LinkButton>
                     转到第<asp:TextBox ID="txtNewPageIndex" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>" Width="20px" />页 <%--//这里将CommandArgument即使点击该按钮e.newIndex 值为3 --%>
                     <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2" CommandName="Page" Text="GO" />
                </PagerTemplate>
                <HeaderStyle BackColor="#f2f2f2" Font-Bold="True" ForeColor="#666" HorizontalAlign="Center" Height="39px" />
                <%--Wrap="False"--%>
                <RowStyle ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" BackColor="#f2f2f2" Wrap="True" Height="50px" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            </asp:GridView>

后端代码:

//获取单元格所在行行引很简单的
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "btn_YL")//
            {
                int INDEX = Convert.ToInt32(e.CommandArgument);
                string filename = GridView1.Rows[INDEX- GridView1.PageIndex*20].Cells[1].Text.ToString().Trim();
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值