我在gridview中有两个下拉列表。
第一个下拉列表 - 用于选择Page No
。
第二次下拉 - 选择Page Size
。
与下拉列表相关的问题是: -
当我change
选择时,它工作正常,并向我显示确切的结果。但当我再次将选择带到最后一个值时,gridview
会中断。尝试了所有代码并调试但无法找到确切的解决方案。
请参阅dropdownlist
中的Gridview
的aspx代码: -
<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>
注意:Gridview位于UpdatePanel
。
另请参阅每个事件背后的代码和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();
}
另请参阅gridview数据绑定事件: -
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();
}
}
此外,ddlPageSize
没有问题,而且工作正常
答案 0 :(得分: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
并选择当前页面时
您可能会想到一种更好的方法来在不破坏网格的情况下将该下拉列表化身
但我的建议是TextBox
使用CurrentPage
而不是DropDownList
<强>为什么吗
假设网格有1000页,用户想要页面号5555.您的方法强制用户在DropDownList
中从1滚动到5555,这或多或少就像用户保持导航一样页面直到达到他/她的目标页面。
虽然TextBox
允许最终用户只需输入其号码
它可以帮助您免除填充DropDownList
的麻烦,并希望解决您的问题
使用TextBox
,您的代码看起来应该是这样的(但我没有编写完整的解决方案来测试它)
在Aspx中,将PageDropDownList
控件替换为文本框,如下所示
<asp:TextBox ID="txtPageNumber" AutoPostBack="true" OnTextChanged="txtPageNumber_TextChanged" runat="server" />
在代码behinde中,将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
}
答案 1 :(得分:0)
在我之前的回答中,我尝试使用Microsoft GridView
处理技术挑战。
但对于商业应用程序,我建议使用来自Telerik,DevExpress,Infragistics,ComponentOne等供应商的第三部分GridView
。
因为这些供应商提供的大多数GridViews
为分页,自定义页面大小等简单内容提供了内置功能
基于这个答案,您不会编写任何代码行来担心在运行时或类似的东西自定义页面大小