所以我有一个GridView表,其中DropDownList用于更改页面大小,而Label用于显示该页面上的行数。我的问题是当页面加载时,lastRowOnPage的值为零。当我点击下拉项时,它将显示上一个选项的编号。例如,如果我从列表中选择25,则标签将显示“显示1 - 10”时应显示“显示1 - 25”。它为所有选择都做到了。
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" EnableViewState="True"
OnLoad="Page_Load" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Text="10" Value="10" />
<asp:ListItem Text="25" Value="25" />
<asp:ListItem Text="50" Value="50" />
<asp:ListItem Text="100" Value="100" />
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" onSelected="onSelectedData" ... ></asp:SqlDataSource>
与此类似问题的大多数解决方案都说if(!IsNotPostBack)和DataBind(),但我在这里有。
public partial class UserControls_CQGCompanyControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView1();
}
}
protected void onSelectedData(object sender, SqlDataSourceStatusEventArgs e)
{
int startRowOnPage = (GridView1.PageIndex * GridView1.PageSize) + 1;
int lastRowOnPage = startRowOnPage + GridView1.Rows.Count - 1;
int totalRows = e.AffectedRows;
Total1.Text = "Showing " + startRowOnPage.ToString() +
" - " + lastRowOnPage + " of " + totalRows;
}
protected void BindGridView1()
{
try
if (Convert.ToInt32(DropDownList1.SelectedValue) != null)
GridView1.PageSize = Convert.ToInt32(DropDownList1.SelectedValue);
this.GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView1();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
BindGridView1();
}
}
我需要做什么?请帮忙!
答案 0 :(得分:1)
试试这个
protected void onSelectedData(object sender, SqlDataSourceStatusEventArgs e)
{
int startRowOnPage = (GridView1.PageIndex * GridView1.PageSize) + 1;
int lastRowOnPage = startRowOnPage + GridView1.PageSize - 1;
int totalRows = e.AffectedRows;
lastRowOnPage = totalRows < lastRowOnPage ? totalRows : lastRowOnPage;
startRowOnPage = startRowOnPage > lastRowOnPage ? (int)(totalRows / GridView1.PageSize) * GridView1.PageSize + 1 : startRowOnPage;
Label1.Text = "Showing " + startRowOnPage.ToString() +
" - " + lastRowOnPage + " of " + totalRows;
}