在Gridview中使用LinkBut​​ton进行分页

时间:2013-03-29 12:20:45

标签: asp.net c#-4.0

我在Gridview中使用LinkBut​​ton。

在Gridview中进行分页时,它会给出一个错误,即索引不能为空/索引超出范围。

如果假设PageSize = 10则在10之后无法找到LinkBut​​ton,因此它会给出错误消息。

那么,我该怎么做?

    <asp:GridView ID="grid_inbox" runat="server" AutoGenerateColumns="False" GridLines="Horizontal"
        OnRowCommand="grid_inbox_RowCommand" EmptyDataText="No Data Found..!!" Width="100%"
        BackColor="White" BorderColor="#E5E5E5" BorderStyle="None" BorderWidth="1px"
        CellPadding="4" ForeColor="#222222" RowStyle-Height="35" AllowPaging="true" PageSize="12">
        <Columns>
            <asp:TemplateField HeaderStyle-Width="3%" FooterStyle-Width="3%" ItemStyle-Width="3%">
                <ItemTemplate>
                    <asp:CheckBox ID="Delete_CheckBox" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Message">
                <ItemTemplate>
                    <asp:HyperLink ID="Message_hl" runat="server" Font-Overline="false" ForeColor="Black"
                        Text='<%#Eval("message") %>' NavigateUrl='<%# String.Format("~/Templet/SendBox_DisplayList.aspx?inbox_id1={0}",Eval("inbox_id")) %>'>HyperLink</asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            <%--            <asp:BoundField DataField="message" HeaderText="Message" />--%>
            <asp:BoundField DataField="Message_From1" HeaderText="Message From" HeaderStyle-Width="12%"
                FooterStyle-Width="12%" ItemStyle-Width="12%"></asp:BoundField>
            <asp:BoundField DataField="message_to" HeaderText="Message To" HeaderStyle-Width="12%"
                FooterStyle-Width="12%" ItemStyle-Width="12%"></asp:BoundField>
            <asp:TemplateField HeaderStyle-Width="13%" FooterStyle-Width="13%" ItemStyle-Width="13%">
                <ItemTemplate>
                    <asp:LinkButton runat="server" ID="lnk_reply" ForeColor="Maroon" Text="Reply" CommandName="Reply"
                        CommandArgument="<%#((GridViewRow)Container).RowIndex%>"></asp:LinkButton>
                    &nbsp;
                    <asp:LinkButton runat="server" ID="lnk_reject" ForeColor="Maroon" Text="Reject" CommandArgument="<%#((GridViewRow)Container).RowIndex%>"
                        CommandName="Reject"></asp:LinkButton>
                    &nbsp;
                    <asp:LinkButton runat="server" ID="lnk_accept" ForeColor="Maroon" CommandName="Accept"
                        CommandArgument='<%#Eval("inbox_id")%>' Text="Accept"> </asp:LinkButton>
                    <asp:HiddenField runat="server" ID="hdf" Value='<%#Eval("message") %>' />
                    <asp:HiddenField runat="server" ID="hdf_inbox_id" Value='<%#Eval("inbox_id")%>' />
                    <asp:HiddenField runat="server" ID="hdf_msg_to_name" Value='<%#Eval("Message_From")%>' />
                    <asp:HiddenField runat="server" ID="hdf_message_from" Value='<%#Eval("Message_From1")%>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCC99" ForeColor="#222222" />
        <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Left" />
        <HeaderStyle BackColor="White" Font-Bold="True" BorderColor="#E5E5E5" Height="35px"
            ForeColor="#222222" HorizontalAlign="Left" />
        <SelectedRowStyle BackColor="#F5F5F5" Font-Bold="True" ForeColor="White" />
    </asp:GridView>

和cs代码是......

   for (int i = 0; i < j; i++)
    {
        int level_from = Int32.Parse(dt.Rows[i]["level_from"].ToString());
        int level_to = Int32.Parse(dt.Rows[i]["level_to"].ToString());
        LinkButton reply_btn1 = grid_inbox.Rows[i].Cells[0].FindControl("lnk_reply") as LinkButton;

        HyperLink msg = grid_inbox.Rows[i].Cells[0].FindControl("Message_hl") as HyperLink;

        //Show message upto 100 character
        string submsg = dt.Rows[i]["message"].ToString();
        int msglen = submsg.Length;
        if (msglen < 100)
        {
            submsg = submsg.Substring(0, msglen);
        }
        else
        {
            submsg = submsg.Substring(0, 100);
        }
        msg.Text = submsg;

它会做的是,它找到HyperLink / LinkBut​​ton控件并为其分配文本。

但是如果我在分页大小结束后使用分页(假设PageSize = 10),则无法找到HyperLink控件。因为现在pageIndex从1更改为2.

1 个答案:

答案 0 :(得分:0)

我认为您需要在使用分页时在网格视图中管理onselectedindexchanged。

<asp:GridView ID="grid_inbox" runat="server" AutoGenerateColumns="False" GridLines="Horizontal"
        OnRowCommand="grid_inbox_RowCommand" EmptyDataText="No Data Found..!!" Width="100%"
        BackColor="White" BorderColor="#E5E5E5" BorderStyle="None" BorderWidth="1px"
        CellPadding="4" ForeColor="#222222" RowStyle-Height="35" 
        AllowPaging="true" PageSize="12" 
        onselectedindexchanged="grid_inbox_SelectedIndexChanged">

在CS中

protected void grid_inbox_SelectedIndexChanged(object sender, EventArgs e)
{
    //Do your stuff
}

请按照以下网址在gridview中维护分页 GridView Paging and Sorting