DataGrid事件无法响应——初探

这段时间做的一些基于web的项目里面经常会用到DataGrid,有些复杂的功能是在代码中动态生成DataGrid来实现,比如一些自己开发的web control、user control里。
DataGrid确实很好用,但有时会出一些奇怪的问题,比如ItemCommand、PageIndexChanged不响应等,显得很诡异(页面会postback,就是不响应事件)。比如下面这个测试的user control,就一个简单的分页功能,第1页怎么都不响应:
namespace S3WebTest
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//using Microsoft.SharePoint;
//using Microsoft.SharePoint.WebControls;
using System.Collections;
using System.IO;

/// <summary>
/// Summary description for TestGrid.
/// </summary>

public class TestGrid : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.PlaceHolder phMain;

private Hashtable _htGrids = new Hashtable();
DataGrid MyGrid
=new DataGrid();

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}


#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();
CreateDataGrids();
ShowDataGrids();

//RegScripts();

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.Load += new System.EventHandler(this.Page_Load);
}

#endregion


private void dg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGrid dg
= (DataGrid) source;
dg.CurrentPageIndex
= e.NewPageIndex;
dg.DataBind();
}


private DataSet dsSource = new DataSet();


private void ShowDataGrids()
{
DataTable dt
= new DataTable();
dt.Columns.Add(
"id");
dsSource.Tables.Add(dt);
for (int j=0;j<12;j++)
{
DataRow dr
= dt.NewRow();
dr[
"id"] = j.ToString();
dt.Rows.Add(dr);
}


phMain.Controls.Clear();

MyGrid.AutoGenerateColumns
= false;
MyGrid.AllowPaging
= true;
MyGrid.PageSize
= 2;
MyGrid.PagerStyle.Mode
= PagerMode.NumericPages;
MyGrid.ItemCommand
MyGrid.PageIndexChanged
+=new DataGridPageChangedEventHandler(dg_PageIndexChanged);
MyGrid.DataSource
= dsSource;
MyGrid.DataBind();

phMain.Controls.Add(MyGrid);

}


private void CreateDataGrids()
{
BoundColumn bc
=new BoundColumn();
bc.HeaderText
="id";
bc.DataField
= "id";
MyGrid.Columns.Add(bc);
}


}

}

调试了一上午后发现,假如页面是postback的话DataGrid的DataBind操作放在事件代码前执行的话肯定就会出问题(哪位大虾能告诉我这是为什么??),所以必须要把DataBind放在!IsPostBack里面,然后再到事件响应代码里执行DataBind,修改后的代码如下:
namespace S3WebTest
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
//using Microsoft.SharePoint;
//using Microsoft.SharePoint.WebControls;
using System.Collections;
using System.IO;

/// <summary>
/// Summary description for TestGrid.
/// </summary>

public class TestGrid : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.PlaceHolder phMain;

private Hashtable _htGrids = new Hashtable();
DataGrid MyGrid
=new DataGrid();

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!this.Page.IsPostBack)
{
MyGrid.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();
CreateDataGrids();
ShowDataGrids();

//RegScripts();

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.Load += new System.EventHandler(this.Page_Load);
}

#endregion


private void dg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGrid dg
= (DataGrid) source;
dg.CurrentPageIndex
= e.NewPageIndex;
dg.DataBind();
}


private DataSet dsSource = new DataSet();


private void ShowDataGrids()
{
DataTable dt
= new DataTable();
dt.Columns.Add(
"id");
dsSource.Tables.Add(dt);
for (int j=0;j<12;j++)
{
DataRow dr
= dt.NewRow();
dr[
"id"] = j.ToString();
dt.Rows.Add(dr);
}


phMain.Controls.Clear();

MyGrid.AutoGenerateColumns
= false;
MyGrid.AllowPaging
= true;
MyGrid.PageSize
= 2;
MyGrid.PagerStyle.Mode
= PagerMode.NumericPages;
MyGrid.ItemCommand
MyGrid.PageIndexChanged
+=new DataGridPageChangedEventHandler(dg_PageIndexChanged);
MyGrid.DataSource
= dsSource;
//MyGrid.DataBind();

phMain.Controls.Add(MyGrid);

}


private void CreateDataGrids()
{
BoundColumn bc
=new BoundColumn();
bc.HeaderText
="id";
bc.DataField
= "id";
MyGrid.Columns.Add(bc);
}


}

}

这样就能正常响应事件了。
posted on 2006-07-07 13:28  sss  阅读(564)  评论(0)    收藏  举报