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();
}
}
我該怎麼做?請幫忙!
@Emre那什麼也沒做...... – cocoa
應該已經想通,但只是寫在這裏..'的OnLoad =「Page_Load中」'是沒有必要的..它只是打服務器兩次。另外'int lastRowOnPage = startRowOnPage + GridView1.Rows.Count - 1; '沒有工作..需要使用'int lastRowOnPage = startRowOnPage + GridView1.PageSize - 1;' – Interstellar