Datagrid PageIndexChanged event doesn't fire

V

vcinquini

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
 
T

Teemu Keiski

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






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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top