2013-12-17 97 views
1

所以我有一个GridView表与一个DropDownList的更改页面大小和一个标签,显示该页面上的行数。我的问题是当页面加载时,lastRowOnPage的值为零。当我点击下拉项目时,它会显示前一个选择的号码。例如,如果我从列表中选择25,那么标签将在显示“显示1 - 25”时显示“显示1 - 10”。它为所有选择做到了这一点。DropDownList和GridView.Row.Count不能一起工作

<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> 

大多数类似这种问题的解决方案的说说,如果(!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

@Emre那什么也没做...... – cocoa

+0

应该已经想通,但只是写在这里..'的OnLoad =“Page_Load中”'是没有必要的..它只是打服务器两次。另外'int lastRowOnPage = startRowOnPage + GridView1.Rows.Count - 1; '没有工作..需要使用'int lastRowOnPage = startRowOnPage + GridView1.PageSize - 1;' – Interstellar

回答

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; 
      } 
+0

超级简单的圣牛!非常感谢!!奇迹般有效。为什么我没有想到... – cocoa