页面上有个DataGrid,用的是自带的分页,还有一个查询功能,可以按输入的条件进行查询,遇到的问题是如果当前页为非第一页,按条件查询时,程序就会报错"无效的 CurrentPageIndex 值。它必须大于等于 0 且小于 PageCount。" 针对这个问题,我在页面加载时调用的方法中做了个判断,如果查询条件不为空,就让当前页索引为0,这样显示是没问题了,但是当查询出来的数据过大时,分页就不起作用了,求个解决办法.

解决方案 »

  1.   

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridView1.PageIndex = e.NewPageIndex;
            InitPage(); //重新绑定GridView数据的函数     }
      

  2.   

    楼上的正解,this.GridView1.PageIndex   =   e.NewPageIndex后再绑定下数据就可以了。不过数据量大的话建议楼主用存储功能分页。
      

  3.   

    private void Page_Load(object sender, System.EventArgs e)
    {
    this.SessionJudge();
    try
    {
    this.IsManager (); if(!Page.IsPostBack )
    {
    Initial(intSearchType);
    txtOperator.Text = this.GetCurrUser().CurrUserName;
    txtDealTime.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
    }
    }
    catch
    {
    this.ShowErrPage("页面初始化失败!");
    }
    }
    /// <summary>
    /// 绑定查询列表
    /// </summary>
    /// <param name="intTrapType">查询类别0为人员,1为车辆</param>
    /// <param name="wherevalue"></param>
    void BindGrid(int intTrapType,string wherevalue)
    {
    if(this.trap == null)
    this.trap = new Jdc.Data.TrapManage (); string _sql = "";
    if(intTrapType == 0)
    {
    if(wherevalue == null || wherevalue.Length == 0)
    _sql="Select * from v_alarm_trapinfo where flag =0 and credtype is not null";
    else
    _sql = "Select * from v_alarm_trapinfo where flag =0 and credtype is not null and "+wherevalue;
    }
    if(intTrapType == 1)
    {
    if(wherevalue == null || wherevalue.Length == 0)
    _sql="Select * from v_alarm_trapinfo where flag =0 and credtype is null";
    else
    _sql = "Select * from v_alarm_trapinfo where flag =0 and credtype is null and "+wherevalue;
    }
    DataSet _ds = this.trap.GetDataSet(_sql);
    if(intTrapType == 0)
    {
    if (wherevalue != "")
    {
    this.dgPersonList.CurrentPageIndex = 0;
    }
    this.dgPersonList.DataSource = _ds;
    this.dgPersonList.DataBind();
    dgPersonList.Visible = true;
    dgCarList.Visible    = false;
    }
    if(intTrapType == 1)
    {
    // if (wherevalue != "")
    // {
    // this.dgCarList.CurrentPageIndex = 0;
    // }
    this.dgCarList.DataSource = _ds;
    this.dgCarList.DataBind();
    dgPersonList.Visible = false;
    dgCarList.Visible    = true;
    }
    }
    void Initial(int intTrapType)
    {
    dlistQType.Items.Clear();
    if(intTrapType == 0)
    {
    //this.dlistQType.Items.Add (new ListItem ("布控编号","TrapID"));
    this.dlistQType.Items.Add (new ListItem ("证件号码","credcode"));
    }
    if(intTrapType == 1)
    {
    this.dlistQType.Items.Add (new ListItem ("车牌号","CarID"));
    this.dlistQType.Items.Add (new ListItem ("发动机号","EngineCode"));
    this.dlistQType.Items.Add (new ListItem ("车架号码","BodyCode"));
    }

    // this.dlistQType.Items.Add (new ListItem ("撤控人","tperson"));
    this.dlistQType.Items.Add (new ListItem ("批准人","tallow"));
    BindGrid(intSearchType,"");
    } /// <summary>
    /// 取得查询条件
    /// </summary>
    /// <returns></returns>
    string CreatQueryValue()
    {
    string _sql = "";
    if(txtValue.Text!="")
    _sql = this.dlistQType.SelectedItem.Value + " like '%"+this.txtValue.Text  +"%'";
    // new Jdc.Data.JDataException (_sql);
    return _sql;
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
    this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
    this.dgCarList.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgCarList_PageIndexChanged);
    this.dgPersonList.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgPersonList_PageIndexChanged);
    this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void btnQuery_Click(object sender, System.EventArgs e)
    {
    BindGrid(intSearchType,CreatQueryValue ());
    } private void btnSave_Click(object sender, System.EventArgs e)
    {
    bool _IsSelect = false;

    if(this.txtDealPerson.Text.Trim().Equals (""))
    {
    this.Message ("请输入撤控人的姓名","txtDealPerson");
    return ;
    }
    if(this.txtDemo.Text.Trim().Equals (""))
    {
    this.Message ("请输入撤控的原因","txtDemo");
    return ;
    }
    try
    {
    DateTime.Parse (this.txtDealTime.Text );
    }
    catch
    {
    this.Message ("时间格式错误,请采用类似2005/03/16 12:24:36 格式输入","txtDealTime");
    return ;
    } DataGrid _dataGrid = new DataGrid();
    if(intSearchType == 0)
    _dataGrid = dgPersonList;
    else 
    _dataGrid = dgCarList; for (int i=0; i<_dataGrid.Items.Count; i++) 
    {    
    DataGridItem _item = _dataGrid.Items[i];
    CheckBox cbSelect = (CheckBox)_item.FindControl("chkGIft");
    if(cbSelect.Checked )
    {
    CancelAlarmInfo(_item.Cells[1].Text);
    _IsSelect = true;
    }
    }
    if(!_IsSelect)
    {
    this.Message ("没有选择布控信息");
    return ;
    }
    this.ShowRightPage ("撤控成功!");
    //重新绑定
    BindGrid(intSearchType,CreatQueryValue());
    this.txtDealPerson.Text = "";
    this.txtDealTime.Text = DateTime.Now.ToString ("yyyy/MM/dd HH:mm:ss");
    this.txtDemo.Text = "";
    }
    /// <summary>
    /// 撤控
    /// </summary>
    /// <param name="alarmID"></param>
    private void CancelAlarmInfo(string alarmID)
    {
    Jdc.Data.CAlarmInfo _calarm = new CAlarmInfo ();
    _calarm.AlarmID = alarmID;
    _calarm.Idea = this.txtDemo .Text .Trim ();
    _calarm.HDateTime = DateTime.Parse (this.txtDealTime.Text).ToString ("yyyyMMddHHmmss");
    _calarm.Handler = this.txtDealPerson.Text.Trim ();
    _calarm.Operator = Session["username"].ToString ();
    _calarm.TAllow = this.txtAllow.Text ; if(cancelAlarm == null)
    this.cancelAlarm = new Jdc.Data.CancelAlarm ();
    try
    {
    this.cancelAlarm.Add (_calarm);
    }
    catch(JDataException je)
    {
    this.ShowErrPage (je.Message );
    }
    } private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    intSearchType = int.Parse(DropDownList1.SelectedItem.Value);
    Initial(intSearchType);
    } private void dgCarList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    dgCarList.CurrentPageIndex = e.NewPageIndex ;
    BindGrid(intSearchType,CreatQueryValue());
    } private void dgPersonList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    dgPersonList.CurrentPageIndex = e.NewPageIndex ;
    BindGrid(intSearchType,CreatQueryValue());
    }