DropDownList的和不GridView.Row.Count一起工作 [英] DropDownList and GridView.Row.Count not working together

查看:166
本文介绍了DropDownList的和不GridView.Row.Count一起工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个GridView表,改变页面大小和显示是页面上的行数一个标签一个DropDownList。我的问题是在页面加载时,为lastRowOnPage值为零。当我点击下拉菜单项就会显示为previous选择号码。例如,如果我从列表中选择25,标签将显示显示1 - 10时,它应该说显示1 - 25。它对于所有的选择。

So I have a GridView table with a DropDownList that changes the page size and a Label that displays the number of rows that are on that page. My problem is when the page loads, the value for the lastRowOnPage is zero. And when I click on the drop down items it will display the number for the previous selection. For example, if I select 25 from the list, the label will display "Showing 1 - 10 " when it should say "Showing 1 - 25 ". It does that for all of the selections.

<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(),但我在这里。

Most of the solutions for issues similar to this say something about if(!IsNotPostBack) and DataBind() but I have that in here.

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();
}     
}

我需要什么做的?请帮忙!

What do I need to do?? Please help!

推荐答案

试试这个

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

这篇关于DropDownList的和不GridView.Row.Count一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆