-2

I have two dropdownlist inside a gridview.

  1. 1st Dropdown -- For Selecting the Page No.

  2. 2nd Dropdown -- For Selecting the Page Size.

The Issue related to the dropdownlist is:-

When I change the Selection it works fine and shows me the exact result. But When I bring the selection to the last value again my gridview breaks. Tried with all the code and debugged also but couldn't find the exact solution.

Please see the aspx code for the dropdownlist inside the Gridview:-

<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always">
            <ContentTemplate>
                <asp:GridView ID="grdUser"
                    OnPageIndexChanging="grdUser_PageIndexChanging"
                    AutoGenerateColumns="false" EnableViewState="true"
                    AllowPaging="true"
                    OnDataBound="grdUser_DataBound"
                    runat="server"
                    Width="100%"
                    border="1"
                    DataKeyNames="Id"
                    PageSize="2">

                    <Columns>
                        <asp:TemplateField HeaderText="Select" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
                            <ItemTemplate>
                                <asp:CheckBox ID="chkDelete" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="username" HeaderText="UserName" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                        <asp:BoundField DataField="email" HeaderText="Email Id" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                        <asp:BoundField DataField="usertype" HeaderText="UserType" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                        <asp:BoundField DataField="active" HeaderText="Active" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td" />
                        <asp:TemplateField HeaderText="Action" HeaderStyle-Width="15%" ItemStyle-Width="30" HeaderStyle-CssClass="k-grid td">
                            <ItemTemplate>
                                <asp:ImageButton ID="btnDelete" AlternateText="Delete" ImageUrl="~/images/delete.png" runat="server" Width="15" Height="15" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' CausesValidation="false" OnClientClick="return confirm('Are you sure you want to delete this record?')" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField ButtonType="Image" ItemStyle-Width="15" EditImageUrl="~/images/edit.png" ShowEditButton="True" ControlStyle-Width="15" ControlStyle-Height="15" CancelImageUrl="~/images/close.png" UpdateImageUrl="~/images/update.png">
                            <ControlStyle Height="20px" Width="20px"></ControlStyle>
                        </asp:CommandField>
                    </Columns>

                    <PagerStyle ForeColor="Blue"
                        BackColor="LightBlue" />
                    <PagerTemplate>
                        <table style="width: 100%">
                            <tr>
                                <td class="col-md-7">
                                    <asp:Label ID="MessageLabel"
                                        ForeColor="Blue"
                                        Text="Select a page:"
                                        runat="server" />
                                    <asp:LinkButton ID="FirstLB" runat="server" CommandName="Page" CommandArgument="First" ToolTip="First"><<</asp:LinkButton>

                                    <asp:LinkButton ID="PrevLB" runat="server" CommandName="Page" CommandArgument="Prev" ToolTip="Previous"><</asp:LinkButton>
                                    <asp:DropDownList ID="PageDropDownList"
                                        AutoPostBack="true"
                                        OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged"
                                        runat="server" />
                                    <asp:LinkButton ID="NextLB" runat="server" CommandName="Page" CommandArgument="Next" ToolTip="Next">></asp:LinkButton>

                                    <asp:LinkButton ID="LastLB" runat="server" CommandName="Page" CommandArgument="Last" ToolTip="Last">>></asp:LinkButton>
                                </td>
                                <td class="col-md-3">
                                    <asp:Label ID="PageSizeLabel" runat="server" Text="Select Page Size: "></asp:Label>
                                    <asp:DropDownList ID="ddlPageSize" runat="server" OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged" AutoPostBack="true">
                                        <asp:ListItem Value="2" Text="2" />
                                        <asp:ListItem Value="5" Text="5" />
                                        <asp:ListItem Value="10" Text="10" />
                                    </asp:DropDownList>
                                </td>
                                <td class="col-md-2">
                                    <asp:Label ID="CurrentPageLabel"
                                        ForeColor="Blue"
                                        runat="server" />
                                </td>
                            </tr>
                        </table>
                    </PagerTemplate>
                </asp:GridView>
            </ContentTemplate>
        </asp:UpdatePanel>

Note: The Gridview is under the UpdatePanel.

Also see the Code behind for the each event and the BindGrid();

protected void BindGrid()
    {
        string conString = ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString;
        SqlCommand cmd = new SqlCommand("select Id,username,email,usertype,active from tbl_User ORDER By Id DESC");
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    grdUser.DataSource = dt;
                    grdUser.DataBind();
                    DisablePageDirections();
                    // System.Threading.Thread.Sleep(3000);
                    //return dt;
                }
            }
        }
    }
    private void DisablePageDirections()
    {
        if (grdUser.PageIndex == 0)
        {
            (grdUser.BottomPagerRow.FindControl("FirstLB") as LinkButton).Enabled = false;
            (grdUser.BottomPagerRow.FindControl("PrevLB") as LinkButton).Enabled = false;
        }
        if (grdUser.PageIndex == grdUser.PageCount - 1)
        {
            (grdUser.BottomPagerRow.FindControl("NextLB") as LinkButton).Enabled = false;
            (grdUser.BottomPagerRow.FindControl("LastLB") as LinkButton).Enabled = false;
        }
    }
    protected void ddlPageSize_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow pagerRow = grdUser.BottomPagerRow;
        DropDownList pageSizeList = (DropDownList)pagerRow.Cells[0].FindControl("ddlPageSize");
        grdUser.PageSize = Convert.ToInt32(pageSizeList.SelectedValue);
        Context.Session["PageSize"] = pageSizeList.SelectedValue;
        BindGrid();
    }
    protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow pagerRow = grdUser.BottomPagerRow;
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        grdUser.PageIndex = pageList.SelectedIndex;
        BindGrid();
    }

Also see the gridview databound Event:-

protected void grdUser_DataBound(object sender, EventArgs e)
    {
        GridViewRow pagerRow = grdUser.BottomPagerRow;
        DropDownList pageSizeList = (DropDownList)pagerRow.Cells[0].FindControl("ddlPageSize");
        if (Context.Session["PageSize"] != null)
        {
            pageSizeList.SelectedValue = Context.Session["PageSize"].ToString();
        }
        // Retrieve the DropDownList and Label controls from the row.
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
        if (pageList != null)
        {
            for (int i = 0; i < grdUser.PageCount; i++)
            {
                int pageNumber = i + 1;
                ListItem item = new ListItem(pageNumber.ToString());
                if (i == grdUser.PageIndex)
                {
                    item.Selected = true;
                }
                pageList.Items.Add(item);
            }
        }
        if (pageLabel != null)
        {
            int currentPage = grdUser.PageIndex + 1;
            pageLabel.Text = "Page " + currentPage.ToString() + " of " + grdUser.PageCount.ToString();
        }
    }

Also, there is no problem in the ddlPageSize and it is working fine

4

2 回答 2

0

问题在下面的部分

protected void grdUser_DataBound(object sender, EventArgs e)
    {
        //...
            for (int i = 0; i < grdUser.PageCount; i++)
            {
                int pageNumber = i + 1;
                ListItem item = new ListItem(pageNumber.ToString());
                if (i == grdUser.PageIndex)
                {
                    item.Selected = true;
                }
                pageList.Items.Add(item);
            }
        //...
    }

当您尝试填充PageDropDownList以反映当前网格PageCount并选择当前页面时

您可能会想到一种更好的方法来填充该下拉列表而不破坏网格

但我的建议是使用TextBoxforCurrentPage而不是DropDownList

为什么?

假设网格有 1000 页,并且用户想要页码 5555。您的方法强制用户滚动DropDownList从 1 滚动到 5555,这或多或少就像用户一直在页面中导航直到到达他/她的目标页面.

WhileTextBox将允许最终用户通过输入页面编号来选择页面

它会让你免于填充它的麻烦,DropDownList并希望能解决你的问题

更新:

使用TextBox,您的代码应该看起来像这样(但我没有编写完整的解决方案来测试它)

在aspx中,将PageDropDownList控件替换为文本框如下

<asp:TextBox ID="txtPageNumber" AutoPostBack="true" OnTextChanged="txtPageNumber_TextChanged" runat="server" />

在后面的代码中,将PageDropDownList_SelectedIndexChanged事件处理程序替换为以下事件处理程序

protected void txtPageNumber_TextChanged(object sender, EventArgs e)
    {
        GridViewRow pagerRow = grdUser.BottomPagerRow;
        TextBox txtPageNumber = (TextBox)sender;
        //Note you need to do more validation on the entered value 
        //Do not allow strings, floats, etc
        int currentPage = int.Parse(txtPageNumber.Text);
        //To handle entries larger than page count        
        if(currentPage >= grdUser.PageCount)
        {
         grdUser.PageIndex = grdUser.PageCount - 1;
         txtPageNumber.Text = grdUser.PageIndex.ToString();             
        }
        else 
          grdUser.PageIndex  = currentPage;

        BindGrid();
    }

最后更新grdUser_DataBound如下

protected void grdUser_DataBound(object sender, EventArgs e)
    {
        //...... your original code here
        //DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        TextBox txtPageNumber = (TextBox)pagerRow.Cells[0].FindControl("txtPageNumber");
        txtPageNumber.Text = grdUser.PageIndex.ToString();
        //Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
        // if (pageList != null)
        // {
            // for (int i = 0; i < grdUser.PageCount; i++)
            // {
                // int pageNumber = i + 1;
                // ListItem item = new ListItem(pageNumber.ToString());
                // if (i == grdUser.PageIndex)
                // {
                    // item.Selected = true;
                // }
                // pageList.Items.Add(item);
            // }
        // }
        //...... your original code here
    }
于 2014-12-23T12:49:36.160 回答
0

在我之前的回答中,我尝试使用 Microsoft 应对技术挑战GridView

但对于商业应用程序,我建议使用GridView来自 Telerik、DevExpress、Infragistics、ComponentOne 等供应商的第三方组件。

因为GridViews这些供应商提供的大多数都为简单的事情提供了内置功能,例如分页、自定义页面大小等

基于这个答案,您不会编写任何代码行来担心在运行时自定义页面大小或类似的东西

检查这个演示

于 2014-12-23T17:55:24.743 回答