Datagrid PageIndexChanged event doesn't fire

Discussion in 'ASP .Net Datagrid Control' started by vcinquini@gmail.com, Jul 16, 2006.

  1. Guest

    I already did this in the past succesfully. I can't understand why it
    isn' t working now. Anyone can see anything wrong?


    The ASPX page

    <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    AutoEventWireup="false" Inherits="datagrid.WebForm1" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
    <HEAD>
    <title>WebForm1</title>
    <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    <meta content="C#" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5"
    name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 32px;
    POSITION: absolute; TOP: 32px" runat="server"
    Width="248px"></asp:TextBox>
    <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 296px; POSITION:
    absolute; TOP: 32px" runat="server"
    Text="Button"></asp:Button>
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 32px;
    POSITION: absolute; TOP: 72px" runat="server"
    AllowPaging="True"
    AutoGenerateColumns="False"></asp:DataGrid></form>
    </body>
    </HTML>

    The code behind

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;

    namespace datagrid
    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.TextBox TextBox1;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    protected System.Web.UI.WebControls.Button Button1;

    private void Page_Load(object sender, System.EventArgs e)
    {
    GetSource();

    if(!IsPostBack)
    {
    CreateColumns();
    DataGrid1.CurrentPageIndex = 0;
    }
    DataGrid1.DataBind();
    }

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

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.DataGrid1.PageIndexChanged += new
    System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load);

    }
    #endregion


    private void CreateColumns()
    {
    DataGrid1.AutoGenerateColumns = false;
    DataGrid1.PageSize = 10;

    HyperLinkColumn hl = new HyperLinkColumn();
    hl.HeaderText = "Customer ID";
    hl.DataTextField = "CustomerId";
    hl.DataNavigateUrlField = "CustomerId";
    hl.DataNavigateUrlFormatString = "Detail.aspx&cid={0}";
    DataGrid1.Columns.Add(hl);

    BoundColumn b = new BoundColumn();
    b.DataField = "CompanyName";
    b.HeaderText = "Company Name";
    DataGrid1.Columns.Add(b);

    b = new BoundColumn();
    b.DataField = "ContactName";
    b.HeaderText = "Contact Name";
    DataGrid1.Columns.Add(b);

    b = new BoundColumn();
    b.DataField = "Address";
    b.HeaderText = "Address";
    DataGrid1.Columns.Add(b);

    b = new BoundColumn();
    b.DataField = "City";
    b.HeaderText = "City";
    DataGrid1.Columns.Add(b);

    b = new BoundColumn();
    b.DataField = "Region";
    b.HeaderText = "Region";
    DataGrid1.Columns.Add(b);

    }


    private void GetSource()
    {
    string sql;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    SqlConnection cn = new SqlConnection("User ID=sa;Initial
    Catalog=Northwind;Data Source=IDEAFIX;Password=;");

    cn.Open();

    sql = "select * from customers where country like '" +
    TextBox1.Text.Trim() +"%'";

    da = new SqlDataAdapter();
    da.SelectCommand = new SqlCommand(sql, cn);
    da.Fill(ds);

    DataGrid1.DataSource = ds.Tables[0];
    }

    private void Button1_Click(object sender, System.EventArgs e)
    {
    GetSource();
    }

    protected void DataGrid1_PageIndexChanged(object source,
    System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    {
    DataGrid1.CurrentPageIndex = e.NewPageIndex;
    DataGrid1.DataBind();
    }

    private bool DynamicColumnAdded
    {
    get
    {
    object b = ViewState["DynamicColumnAdded"];
    return (b == null) ? false : true;
    }
    set
    {
    ViewState["DynamicColumnAdded"] = value;
    }
    }

    protected override void LoadViewState(object savedState)
    {
    base.LoadViewState(savedState);

    if (DynamicColumnAdded)
    {
    this.CreateColumns();
    }
    }

    }
    }


    Thanks in advance
     
    , Jul 16, 2006
    #1
    1. Advertising

  2. Teemu Keiski Guest

    Hi,
    it's because you call Datagrid.DataBind() on every request (non-postback
    and postbacks) when thsat "eats up the events".

    E.g

    private void Page_Load(object sender, System.EventArgs e)
    {
    GetSource();

    if(!IsPostBack)
    {
    CreateColumns();
    DataGrid1.CurrentPageIndex = 0;
    }
    //ISSUE IS THIS LINE
    DataGrid1.DataBind();
    }

    As a solution move the initial databind inside !IsPostBack

    if(!IsPostBack)
    {
    CreateColumns();
    DataGrid1.CurrentPageIndex = 0;
    DataGrid1.DataBind();
    }

    And just a side-note GetSource method would make more sense if it returned
    the datatable straight

    private DataTable GetSource()
    {
    //...
    return ds.Tables[0];
    }

    when it could be called:

    DataGrid1.DataSource = GetSource();

    This would make more sense so that retrieving data happens just before it is
    needed (you'd set the datasource separately in !isPostBack and then on
    PageIndexChanged)


    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke






    <> wrote in message
    news:...
    >I already did this in the past succesfully. I can't understand why it
    > isn' t working now. Anyone can see anything wrong?
    >
    >
    > The ASPX page
    >
    > <%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
    > AutoEventWireup="false" Inherits="datagrid.WebForm1" %>
    > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    > <HTML>
    > <HEAD>
    > <title>WebForm1</title>
    > <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
    > <meta content="C#" name="CODE_LANGUAGE">
    > <meta content="JavaScript" name="vs_defaultClientScript">
    > <meta content="http://schemas.microsoft.com/intellisense/ie5"
    > name="vs_targetSchema">
    > </HEAD>
    > <body MS_POSITIONING="GridLayout">
    > <form id="Form1" method="post" runat="server">
    > <asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 32px;
    > POSITION: absolute; TOP: 32px" runat="server"
    > Width="248px"></asp:TextBox>
    > <asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 296px; POSITION:
    > absolute; TOP: 32px" runat="server"
    > Text="Button"></asp:Button>
    > <asp:DataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 32px;
    > POSITION: absolute; TOP: 72px" runat="server"
    > AllowPaging="True"
    > AutoGenerateColumns="False"></asp:DataGrid></form>
    > </body>
    > </HTML>
    >
    > The code behind
    >
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.Data;
    > using System.Data.SqlClient;
    > using System.Drawing;
    > using System.Web;
    > using System.Web.SessionState;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Web.UI.HtmlControls;
    >
    > namespace datagrid
    > {
    > /// <summary>
    > /// Summary description for WebForm1.
    > /// </summary>
    > public class WebForm1 : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.TextBox TextBox1;
    > protected System.Web.UI.WebControls.DataGrid DataGrid1;
    > protected System.Web.UI.WebControls.Button Button1;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > GetSource();
    >
    > if(!IsPostBack)
    > {
    > CreateColumns();
    > DataGrid1.CurrentPageIndex = 0;
    > }
    > DataGrid1.DataBind();
    > }
    >
    > #region Web Form Designer generated code
    > override protected void OnInit(EventArgs e)
    > {
    > //
    > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    > //
    > InitializeComponent();
    > base.OnInit(e);
    > }
    >
    > /// <summary>
    > /// Required method for Designer support - do not modify
    > /// the contents of this method with the code editor.
    > /// </summary>
    > private void InitializeComponent()
    > {
    > this.Button1.Click += new System.EventHandler(this.Button1_Click);
    > this.DataGrid1.PageIndexChanged += new
    > System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
    > this.Load += new System.EventHandler(this.Page_Load);
    >
    > }
    > #endregion
    >
    >
    > private void CreateColumns()
    > {
    > DataGrid1.AutoGenerateColumns = false;
    > DataGrid1.PageSize = 10;
    >
    > HyperLinkColumn hl = new HyperLinkColumn();
    > hl.HeaderText = "Customer ID";
    > hl.DataTextField = "CustomerId";
    > hl.DataNavigateUrlField = "CustomerId";
    > hl.DataNavigateUrlFormatString = "Detail.aspx&cid={0}";
    > DataGrid1.Columns.Add(hl);
    >
    > BoundColumn b = new BoundColumn();
    > b.DataField = "CompanyName";
    > b.HeaderText = "Company Name";
    > DataGrid1.Columns.Add(b);
    >
    > b = new BoundColumn();
    > b.DataField = "ContactName";
    > b.HeaderText = "Contact Name";
    > DataGrid1.Columns.Add(b);
    >
    > b = new BoundColumn();
    > b.DataField = "Address";
    > b.HeaderText = "Address";
    > DataGrid1.Columns.Add(b);
    >
    > b = new BoundColumn();
    > b.DataField = "City";
    > b.HeaderText = "City";
    > DataGrid1.Columns.Add(b);
    >
    > b = new BoundColumn();
    > b.DataField = "Region";
    > b.HeaderText = "Region";
    > DataGrid1.Columns.Add(b);
    >
    > }
    >
    >
    > private void GetSource()
    > {
    > string sql;
    > SqlDataAdapter da;
    > DataSet ds = new DataSet();
    > SqlConnection cn = new SqlConnection("User ID=sa;Initial
    > Catalog=Northwind;Data Source=IDEAFIX;Password=;");
    >
    > cn.Open();
    >
    > sql = "select * from customers where country like '" +
    > TextBox1.Text.Trim() +"%'";
    >
    > da = new SqlDataAdapter();
    > da.SelectCommand = new SqlCommand(sql, cn);
    > da.Fill(ds);
    >
    > DataGrid1.DataSource = ds.Tables[0];
    > }
    >
    > private void Button1_Click(object sender, System.EventArgs e)
    > {
    > GetSource();
    > }
    >
    > protected void DataGrid1_PageIndexChanged(object source,
    > System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
    > {
    > DataGrid1.CurrentPageIndex = e.NewPageIndex;
    > DataGrid1.DataBind();
    > }
    >
    > private bool DynamicColumnAdded
    > {
    > get
    > {
    > object b = ViewState["DynamicColumnAdded"];
    > return (b == null) ? false : true;
    > }
    > set
    > {
    > ViewState["DynamicColumnAdded"] = value;
    > }
    > }
    >
    > protected override void LoadViewState(object savedState)
    > {
    > base.LoadViewState(savedState);
    >
    > if (DynamicColumnAdded)
    > {
    > this.CreateColumns();
    > }
    > }
    >
    > }
    > }
    >
    >
    > Thanks in advance
    >
     
    Teemu Keiski, Jul 18, 2006
    #2
    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:
    494
    =?Utf-8?B?TWlrZUc=?=
    Jun 18, 2004
  2. Gopalan
    Replies:
    2
    Views:
    8,993
    =?Utf-8?B?YWx2aW56X2M=?=
    Jun 18, 2005
  3. steve
    Replies:
    0
    Views:
    204
    steve
    Dec 16, 2003
  4. Kilic Beg
    Replies:
    0
    Views:
    157
    Kilic Beg
    Jun 8, 2004
  5. Mike Lashbrook

    Data Grid Paging Prev button does not fire the PageIndexChanged ev

    Mike Lashbrook, Jul 17, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    145
    Mike Lashbrook
    Jul 17, 2004
Loading...

Share This Page