private void ProcessRequest() { // Determine whether the request comes as a postback IsPostBack = DeterminePostBackMode();// Fire the Page_Init event to the ASPX source code PageInit();// Load the viewstate and process posted values if (IsPostBack) { LoadPageViewState(); ProcessPostData(); } // Fire the Page_Load event to the ASPX source code PageLoad();// 1) Process posted values for the second time (in case of // dynamically created controls) // 2) Raise property-changed server-side events to input-driven // controls (i.e., the checkbox state changed) // 3) Execute any code associated with the postback event if (IsPostBack) { ProcessPostDataSecondTry(); RaiseChangedEvents(); RaisePostBackEvent(); } // Fire the Page_PreRender event to the ASPX source code PreRender();// Save the current state of the controls to the viewstate SavePageViewState();// Render the contents of the page to HTML RenderControl(CreateHtmlTextWriter(Response.Output)); } 这是页面的HANDLER模型,楼主看一下,在pageload()之后 if (IsPostBack) { ProcessPostDataSecondTry(); RaiseChangedEvents(); RaisePostBackEvent(); } 其中ProcessPostDataSecondTry();帮助动态创建的控件从NameValueCollection中获取该控件等状态,如果是动态增加BUTTON的话,其CALLBACK函数在RaisePostBackEvent();调用,
btn.Text = "按钮A";
Panel1.Controls.Add(btn); RadioButtonList rbtn = new RadioButtonList();
rbtn.Items.Add("ddd");
Panel1.Controls.Add(rbtn); //将btn_Click方法绑定到btn按钮的Click事件中
btn.Click += new EventHandler(btn_Click);放在Page_Load事件中
{
}
else
{
Button btn = new Button();
btn.Text = "按钮A";
Panel1.Controls.Add(btn); RadioButtonList rbtn = new RadioButtonList();
rbtn.Items.Add("ddd");
Panel1.Controls.Add(rbtn); //将btn_Click方法绑定到btn按钮的Click事件中
btn.Click += new EventHandler(btn_Click);}
btn.Click += new EventHandler(btn_Click);
绑定的事件起不到作用啊!
关于btn.Click += new EventHandler(btn_Click);事件起不到作用,看:
http://blog.joycode.com/saucer/archive/2004/04/14/19456.aspx
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Button btn = new Button() ;
btn.Text = "ok";
this.Panel1.Controls.Add(btn);
btn.Click +=new EventHandler(btn_Click);
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e) {
} private void btn_Click(object sender, EventArgs e) {
Response.Write("ppppppppp");
}
}
{
// Determine whether the request comes as a postback
IsPostBack = DeterminePostBackMode();// Fire the Page_Init event to the ASPX source code
PageInit();// Load the viewstate and process posted values
if (IsPostBack) {
LoadPageViewState();
ProcessPostData();
} // Fire the Page_Load event to the ASPX source code
PageLoad();// 1) Process posted values for the second time (in case of
// dynamically created controls)
// 2) Raise property-changed server-side events to input-driven
// controls (i.e., the checkbox state changed)
// 3) Execute any code associated with the postback event
if (IsPostBack) {
ProcessPostDataSecondTry();
RaiseChangedEvents();
RaisePostBackEvent();
}
// Fire the Page_PreRender event to the ASPX source code
PreRender();// Save the current state of the controls to the viewstate
SavePageViewState();// Render the contents of the page to HTML
RenderControl(CreateHtmlTextWriter(Response.Output));
}
这是页面的HANDLER模型,楼主看一下,在pageload()之后
if (IsPostBack) {
ProcessPostDataSecondTry();
RaiseChangedEvents();
RaisePostBackEvent();
}
其中ProcessPostDataSecondTry();帮助动态创建的控件从NameValueCollection中获取该控件等状态,如果是动态增加BUTTON的话,其CALLBACK函数在RaisePostBackEvent();调用,
Response.Write("<script language='language'>alert('dddd');</script>");
应该是
Response.Write("<script language='javascript'>alert('dddd');</script>");哈哈 怪不得不起作用了好了问题已解决,结帐