Re: Databinding and PageIndexChanged() event

Discussion in 'ASP .Net' started by bruceb, Jan 25, 2005.

  1. bruceb

    bruceb Guest

    I have had the same problem and believe that I have discovered the
    exact reaason why the event does not fire.

    It has to do with the way in which the datagrid columns are added. If
    columns are added during the method in which you perform the
    BindData(), then the PageIndexChanged event seems to get corrupted.

    To correct the issue place all of the column code into a method that
    you call from within the OnInit() method as follows:


    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    AddDataGridColumns();
    base.OnInit(e);
    }

    Place all of the code for creating the columns the the
    AddDataGridColumns method:

    private void AddDataGridColumns()
    {
    // Add columns
    HyperLinkColumn hlinkcol = new HyperLinkColumn();

    hlinkcol.DataNavigateUrlField = "AssessorDetailId";
    hlinkcol.DataNavigateUrlFormatString="Receipt.aspx?function=Edit&id={0}";
    hlinkcol.DataTextField="Equ_AcctKey";
    hlinkcol.HeaderText = "Equ Account";
    hlinkcol.Target="_blank";
    grdResults.Columns.Add(hlinkcol);

    BoundColumn dgc_wriAccount = new BoundColumn();
    dgc_wriAccount.DataField = "Wri_acctKey";
    dgc_wriAccount.HeaderText = "WRI Account";
    dgc_wriAccount.ItemStyle.Width = new Unit(100);
    grdResults.Columns.Add(dgc_wriAccount);
    ....
    }

    Then the event and the BindDate method should like like the following:

    private void grdResults_PageIndexChanged(object source,
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    grdResults.CurrentPageIndex = e.NewPageIndex;
    BindData();
    }

    private void BindData()
    {
    ConnectionString =
    ConfigurationSettings.AppSettings["ConnectionString"];

    receipt = new TaxBillReceipt(ConnectionString);
    receipt.LoadList(int.Parse(txtTaxYear.Text),
    txtState.Text,
    txtCadName.Text,
    txtAssessor.Text,
    txtCostCenter.Text,
    txtWriKey.Text,
    txtEquKey.Text);

    grdResults.AutoGenerateColumns = false;
    grdResults.DataSource = receipt.recDs.Tables[0];
    grdResults.DataKeyField = "AssessorDetailId";

    grdResults.DataBind();
    }

    This means that the bound columns are already added to the data grid
    before any binding is attempted. This corrected my problem completely.

    Bruce
    www.gatewayhorizons.com


    Jim Ross [MVP] wrote:
    > I have observed this when viewstate is not turned on for the page or
    > the grid.
    >
    > "Jeffrey H" <> wrote:
    >
    > >I have a DataGrid with paging, implemented in a very standard way.
    > >
    > >My DataGrid posts just fine with the first set of records, and with

    page
    > >numbers. But when I click a page number, the DataGrid does not

    reappear.
    > >
    > >It turns out that the _PageIndexChanged() event is not firing.

    What's
    > >causing this problem?
    > >

    >
    > Jim Ross
    > MS MVP [VC/MFC]
    >
    > Remove "nospam" to send email, but please ask all questions in the

    newsgroups, not via private mail
     
    bruceb, Jan 25, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWlrZUc=?=
    Replies:
    0
    Views:
    512
    =?Utf-8?B?TWlrZUc=?=
    Jun 18, 2004
  2. JenHu
    Replies:
    0
    Views:
    559
    JenHu
    Oct 28, 2004
  3. Gopalan
    Replies:
    2
    Views:
    9,047
    =?Utf-8?B?YWx2aW56X2M=?=
    Jun 18, 2005
  4. abn
    Replies:
    0
    Views:
    644
  5. jobs
    Replies:
    0
    Views:
    1,764
Loading...

Share This Page