本文主要记录获取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 %>"/>   
<asp:LinkButton Height="25px" ForeColor="#0099ff" runat="server" ID="LbtnZM" Text=" 下载 " CommandName="btn_XZ" CommandArgument="<%# Container.DataItemIndex %>"/>   
</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();
}
}