如果从下拉列表中选择最大值,寻呼机模板会从 Gridview 中消失
Pager Template gets disapper from the Gridview if maximum value selected from dropdownlist
我有一个 gridview,我在其中使用 PagerTemplate 进行分页。问题是,如果 gridview 中只有 3 条记录,并且当我 select 来自寻呼机下拉列表的 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;
}
}
}
}
如果您想设置 PagerTemplate,即使它已达到最大限制。你需要设置gridview的BottomPager
set Visible= true;
如下所示:-
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.
}
}
}
}
请告诉我它是否有效...!
我有一个 gridview,我在其中使用 PagerTemplate 进行分页。问题是,如果 gridview 中只有 3 条记录,并且当我 select 来自寻呼机下拉列表的 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;
}
}
}
}
如果您想设置 PagerTemplate,即使它已达到最大限制。你需要设置gridview的BottomPager
set Visible= true;
如下所示:-
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.
}
}
}
}
请告诉我它是否有效...!