☆☆☆第一个答对的给100分,动态生成的Button,如何触发单击事件,有代码请高手溜一眼,很快的,谢谢
namespace Teach_databinder
{
/// <summary>
/// three_Repeater_page 的摘要说明。
/// </summary>
public class three_Repeater_page : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater Repeater1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.PlaceHolder ph;
protected System.Web.UI.WebControls.Panel Panel1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
ViewState[ "page "] =1;
ViewState[ "Data "]=this.newTable();
databinderR();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void databinderR()
{
System.Web.UI.WebControls.PagedDataSource newds = new PagedDataSource();
DataTable newone = this.newTable();
newds.DataSource=((DataTable)ViewState[ "Data "]).DefaultView;
newds.AllowPaging=true;
newds.PageSize=3;
newds.CurrentPageIndex=(int)ViewState[ "page "]-1;
/////
///
for(int k=0;k <newds.PageCount;k++)
{
System.Web.UI.WebControls.LinkButton newbt = new LinkButton();
newbt.Text=(k+1).ToString()+ "页 ";
newbt.ID= "BT "+(k+1);
newbt.ForeColor= Color.DarkBlue;
newbt.Click += new System.EventHandler(newbt_Click);//
ph.Controls.Add(newbt);
}
this.Repeater1.DataSource=newds;
this.Repeater1.DataBind();
this.Button1.Enabled=true;
this.Button2.Enabled=true;
if(newds.CurrentPageIndex <1)
{
this.Button1.Enabled=false;
}
if(newds.CurrentPageIndex==newds.PageCount-1)
{
this.Button2.Enabled=false;
}
}
public DataTable newTable()
{
DataTable tt = new DataTable();
tt.Columns.Add( "id ",typeof(int));
tt.Columns.Add( "name ",typeof(string));
tt.Columns.Add( "content ",typeof(string));
for(int i=0;i <30;i++)
{
DataRow nr = tt.NewRow();
nr[ "id "]=i+1;
nr[ "name "]= "用户 "+(i+1);
nr[ "content "]= "您是第 "+(i+1)+ "位用户 ";
tt.Rows.Add(nr);
}
return tt;
}
public void newbt_Click(object sender, EventArgs e)
{
Response.Write( "ok ");
//这里的所有动作都没有反应。
}
private void Button1_Click(object sender, System.EventArgs e)
{
ViewState[ "page "]=(int)ViewState[ "page "]-1;
databinderR();
}
private void Button2_Click(object sender, System.EventArgs e)
{
ViewState[ "page "]=(int)ViewState[ "page "]+1;
databinderR();
}
}
}
[解决办法]
试试:
namespace Teach_databinder
{
/// <summary>
/// three_Repeater_page 的摘要说明。
/// </summary>
public class three_Repeater_page : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater Repeater1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.PlaceHolder ph;
protected System.Web.UI.WebControls.Panel Panel1;
public System.Web.UI.WebControls.LinkButton newbt;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
ViewState[ "page "] =1;
ViewState[ "Data "]=this.newTable();
databinderR();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void databinderR()
{
System.Web.UI.WebControls.PagedDataSource newds = new PagedDataSource();
DataTable newone = this.newTable();
newds.DataSource=((DataTable)ViewState[ "Data "]).DefaultView;
newds.AllowPaging=true;
newds.PageSize=3;
newds.CurrentPageIndex=(int)ViewState[ "page "]-1;
/////
///
for(int k=0;k <newds.PageCount;k++)
{
newbt = new LinkButton();
newbt.Text=(k+1).ToString()+ "页 ";
newbt.ID= "BT "+(k+1);
newbt.ForeColor= Color.DarkBlue;
newbt.Click += new System.EventHandler(newbt_Click);//
ph.Controls.Add(newbt);
}
this.Repeater1.DataSource=newds;
this.Repeater1.DataBind();
this.Button1.Enabled=true;
this.Button2.Enabled=true;
if(newds.CurrentPageIndex <1)
{
this.Button1.Enabled=false;
}
if(newds.CurrentPageIndex==newds.PageCount-1)
{
this.Button2.Enabled=false;
}
}
public DataTable newTable()
{
DataTable tt = new DataTable();
tt.Columns.Add( "id ",typeof(int));
tt.Columns.Add( "name ",typeof(string));
tt.Columns.Add( "content ",typeof(string));
for(int i=0;i <30;i++)
{
DataRow nr = tt.NewRow();
nr[ "id "]=i+1;
nr[ "name "]= "用户 "+(i+1);
nr[ "content "]= "您是第 "+(i+1)+ "位用户 ";
tt.Rows.Add(nr);
}
return tt;
}
public void newbt_Click(object sender, EventArgs e)
{
Response.Write( "ok ");
//这里的所有动作都没有反应。
}
private void Button1_Click(object sender, System.EventArgs e)
{
ViewState[ "page "]=(int)ViewState[ "page "]-1;
databinderR();
}
private void Button2_Click(object sender, System.EventArgs e)
{
ViewState[ "page "]=(int)ViewState[ "page "]+1;
databinderR();
}
}
}
[解决办法]
因为回发后的Page_Load没有执行databinderR
你的Button也就没有在这次对象生命周期内被创建
不存在的对象怎么会得到事件响应呢?
[解决办法]
databinderR();写在if之外