如果从下拉列表中选择最大值,则寻呼模板将从Gridview中消失

时间:2015-01-16 10:50:03

标签: c# asp.net gridview drop-down-menu

我有一个gridview,我使用PagerTemplate进行分页。问题是,如果gridview中只有3条记录,并且当我从寻呼机的下拉列表中选择 PageSize = 3 时。 gridivew显示3条记录,但同时 pagerTemplate 消失。

我不希望pagerTemplate得到消失。请参阅您的参考代码: -

<PagerTemplate>
                <table runat="server" id="testTable1" style="width: 100%" class="k-grid td">
                    <tr>
                        <td class="col-md-7 pull-left">
                            <asp:Label ID="MessageLabel"
                                Text="Select a page:"
                                runat="server" />
                            <asp:LinkButton ID="FirstLB" runat="server" CommandName="Page" CommandArgument="First" ToolTip="First" CssClass="btn-pager btn-default"><<</asp:LinkButton>
                            <asp:LinkButton ID="PrevLB" runat="server" CommandName="Page" CommandArgument="Prev" ToolTip="Previous" CssClass="btn-pager btn-default"><</asp:LinkButton>
                            <asp:DropDownList runat="server" ID="PageDropDownList" AutoPostBack="true" EnableViewState="true" OnSelectedIndexChanged="PageDropDownList_SelectedIndexChanged" CssClass="selectpicker form-control-drp"></asp:DropDownList>

                            <asp:LinkButton ID="NextLB" runat="server" CommandName="Page" CommandArgument="Next" ToolTip="Next" CssClass="btn-pager btn-default">></asp:LinkButton>
                            <asp:LinkButton ID="LastLB" runat="server" CommandName="Page" CommandArgument="Last" ToolTip="Last" CssClass="btn-pager btn-default">>></asp:LinkButton>
                        </td>

                        <td class="col-md-3 pull-right">
                            <asp:Label ID="PageSizeLabel" runat="server" Text="Select Page Size: "></asp:Label>
                            <asp:DropDownList ID="ddlPageSize" runat="server" OnSelectedIndexChanged="ddlPageSize_SelectedIndexChanged" AutoPostBack="true" CssClass="selectpicker form-control-drp">
                                <%-- <asp:ListItem Value="0" Text="0" />--%>
                                <asp:ListItem Value="1" Text="1" />
                                <asp:ListItem Value="2" Text="2" />
                                <asp:ListItem Value="3" Text="3" />
                            </asp:DropDownList>
                        </td>
                        <td class="col-md-2">
                            <asp:Label ID="CurrentPageLabel" runat="server" />
                        </td>
                    </tr>
                </table>
            </PagerTemplate>

另请参阅pagerTemplate,dropdownlist

背后的代码
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow pagerRow = grdCSRPageData.BottomPagerRow;
        DropDownList pageSizeList = (DropDownList)pagerRow.Cells[0].FindControl("ddlPageSize");
        grdCSRPageData.PageSize = Convert.ToInt32(pageSizeList.SelectedValue);
        Context.Session["PageSize"] = pageSizeList.SelectedValue;
        BindGrid();
    }
    protected void PageDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow pagerRow = grdCSRPageData.BottomPagerRow;
        DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
        grdCSRPageData.PageIndex = pageList.SelectedIndex;
        BindGrid();
    }
protected void PreRenderGrid(object sender, EventArgs e)
    {
        if (grdCSRPageData.Rows.Count > 0)
        {
            GridViewRow pagerRow = grdCSRPageData.BottomPagerRow;
            DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");//error
            Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
            if (pageList != null)
            {
                for (int i = 0; i < grdCSRPageData.PageCount; i++)
                {
                    int pageNumber = i + 1;
                    ListItem item = new ListItem(pageNumber.ToString());
                    if (i == grdCSRPageData.PageIndex)
                    {
                        item.Selected = true;
                    }
                    pageList.Items.Add(item);
                }
            }
            if (pageLabel != null)
            {
                int currentPage = grdCSRPageData.PageIndex + 1;
                pageLabel.Text = "Page " + currentPage.ToString() + " of " + grdCSRPageData.PageCount.ToString();
            }
            this.grdCSRPageData.Controls[0].Controls[this.grdCSRPageData.Controls[0].Controls.Count - 1].Visible = true;

            if (ddlPagesNgo.SelectedIndex != 0)
            {
                BindGridView(ddlPagesNgo.SelectedValue);
            }
            else
            {
                BindGrid();
            }
        }
    }

Gridview的数据绑定代码: -

public void BindGrid()
    {
        string conString = ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString;
        SqlCommand cmd = new SqlCommand("Select Id,cat_id, title, description, active from tbl_post 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);
                    grdPostData.DataSource = dt;
                    grdPostData.DataBind();
                    DisablePageDirections();
                    grdPostData.BottomPagerRow.Visible = true;
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:2)

如果要将PagerTemplate设置为最高限制。您需要设置gridview集Visible = true; {/ p>的BottomPager

如下所示: -

public void BindGrid()
    {
        string conString = ConfigurationManager.ConnectionStrings["DefaultCSRConnection"].ConnectionString;
        SqlCommand cmd = new SqlCommand("Select Id,cat_id, title, description, active from tbl_post 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);
                    grdPostData.DataSource = dt;
                    grdPostData.DataBind();
                    DisablePageDirections();
                    grdPostData.BottomPagerRow.Visible = true;    //This will set the pagerTemplate visible even if you set the maximum value from the dropdownlist.
                }
            }
        }
    }

请告诉我它是否有效..!