DATAGRID自定义控件的问题
hubro 2005-05-13 09:56:01 先看下面的代码
using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.OleDb;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Configuration;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Security;
using article;
//中文么//
namespace MyDataGrid
{
public class Grid:WebControl,INamingContainer
{
function datacommand=new function();
public DataGrid TheGrid=new DataGrid();
public Button delbutton=new Button();
public Label PageLabel = new Label();
public String _sql;
public int _pagesize;
public String _cells;
public String sql
{
get { return _sql;}
set { _sql = value;}
}
public int pagesize
{
get { return _pagesize;}
set { _pagesize = value;}
}
public String cells
{
get { return _cells;}
set { _cells = value;}
}
public string[] thecel()
{
return cells.Split(',');
}
public String datadb()
{
Regex regexmail = new Regex(@"from (\w+)");
return regexmail.Match(sql).Groups[1].ToString();
}
public void pagebind()
{
//PageLabel.Text+=1;
TheGrid.DataSource=datacommand.GetDataList(sql);
TheGrid.DataBind();
}
private void SubmitBtn_Click(object sender, EventArgs e)
{
PageLabel.Text=TheGrid.Items.Count.ToString();
for (int j=0;j<this.TheGrid.Items.Count;j++)
{
CheckBox CB=(CheckBox)this.TheGrid.Items[j].FindControl("del");
string lb=TheGrid.Items[j].Cells[2].Text;
if (CB.Checked==true)
datacommand.update("delete from "+datadb()+" where id="+int.Parse(lb)+"");
}
pagebind();
}
public void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
{
PageLabel.Text="hubro";
TheGrid.CurrentPageIndex = e.NewPageIndex;
pagebind();
}
public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs e)
{
}
public void MyDataGrid_Edit(Object sender, DataGridCommandEventArgs e) {
TheGrid.EditItemIndex = (int)e.Item.ItemIndex;
pagebind();
}
public void MyDataGrid_Cancel(Object sender, DataGridCommandEventArgs e) {
PageLabel.Text="hubro";
TheGrid.EditItemIndex = -1;
pagebind();
}
public Grid()
{
TheGrid.CellPadding=4;
TheGrid.CellSpacing=0;
TheGrid.AllowPaging=true;
TheGrid.AutoGenerateColumns=false;
TheGrid.PagerStyle.Mode=PagerMode.NumericPages;
TheGrid.HeaderStyle.BackColor=System.Drawing.Color.FromName("#f0f0f0");
TheGrid.PageIndexChanged+=new DataGridPageChangedEventHandler(this.MyDataGrid_Page);
TheGrid.EditCommand+=new DataGridCommandEventHandler(this.MyDataGrid_Edit);
TheGrid.CancelCommand+=new DataGridCommandEventHandler(this.MyDataGrid_Cancel);
TheGrid.UpdateCommand+=new DataGridCommandEventHandler(this.MyDataGrid_Update);
this.Controls.Add(this.TheGrid);
delbutton.Text="Del";
delbutton.Click += new EventHandler(this.SubmitBtn_Click);
this.Controls.Add(this.delbutton);
this.Controls.Add(this.PageLabel);
}
protected override void Render(HtmlTextWriter output)
{
TheGrid.PageSize=20;
if (pagesize>0)
TheGrid.PageSize=pagesize;
EditCommandColumn theedit=new EditCommandColumn();
theedit.EditText="编辑";
theedit.CancelText="取消";
theedit.UpdateText="修改";
theedit.ItemStyle.Wrap=false;
theedit.HeaderText="操作";
theedit.HeaderStyle.Wrap=false;
TheGrid.Columns.Add(theedit);
TemplateColumn tm2=new TemplateColumn();
tm2.ItemTemplate=new checkbox();
tm2.HeaderText="选择";
TheGrid.Columns.Add(tm2);
for (int ii=0;ii<thecel().Length;ii++)
{
BoundColumn tm=new BoundColumn();
tm.HeaderText=thecel()[ii];
tm.DataField=thecel()[ii];
TheGrid.Columns.Add(tm);
}
pagebind();
this.TheGrid.RenderControl(output);
this.delbutton.RenderControl(output);
this.PageLabel.RenderControl(output);
}
}
public class checkbox : ITemplate
{
public void InstantiateIn(Control container)
{
CheckBox thecheck=new CheckBox();
thecheck.ID="del";
container.Controls.Add(thecheck);
}
}
}
DATAGRID的分页事件是这样写的
TheGrid.PageIndexChanged+=new DataGridPageChangedEventHandler(this.MyDataGrid_Page);
public void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e)
{
PageLabel.Text="hubro";//我在这里跟踪一下,看有没执行
TheGrid.CurrentPageIndex = e.NewPageIndex;
pagebind();
}
结果,没执行,其它事件也是如此,不知为什么,哪位高手知道