Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Datagrid Control > Datagrid PageIndexChanged event doesn't fire

Reply
Thread Tools

Datagrid PageIndexChanged event doesn't fire

 
 
vcinquini@gmail.com
Guest
Posts: n/a
 
      07-16-2006
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>
<aspataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 32px;
POSITION: absolute; TOP: 72px" runat="server"
AllowPaging="True"
AutoGenerateColumns="False"></aspataGrid></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.DataGridPageChangedEvent Handler(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.DataGridPageChangedEvent Args 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

 
Reply With Quote
 
 
 
 
Teemu Keiski
Guest
Posts: n/a
 
      07-18-2006
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






<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
>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>
> <aspataGrid id="DataGrid1" style="Z-INDEX: 103; LEFT: 32px;
> POSITION: absolute; TOP: 72px" runat="server"
> AllowPaging="True"
> AutoGenerateColumns="False"></aspataGrid></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.DataGridPageChangedEvent Handler(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.DataGridPageChangedEvent Args 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid PageIndexChanged event handling Gopalan ASP .Net 2 06-18-2005 05:36 AM
Data Grid Paging Prev button does not fire the PageIndexChanged ev Mike Lashbrook ASP .Net Web Controls 0 07-16-2004 11:50 PM
Detecting a datagrid pageIndexChanged event in page_load =?Utf-8?B?TWlrZUc=?= ASP .Net 0 06-18-2004 12:08 PM
datagrid as Composite control... PageIndexChanged event don't get Fired!! Pls Help Kilic Beg ASP .Net Datagrid Control 0 06-08-2004 10:10 PM
DataGrid pageIndexChanged event not firing - simple solution steve ASP .Net Datagrid Control 0 12-16-2003 07:27 PM



Advertisments