我在web项目之外的,另外一个类库项目里某个类里,动态创建了一个按钮。如何创建这个按钮的事件呢?做了几次都不成功。
//这个事件是由UI层页面一个按钮单击触发的,这个无关紧要吧。
void CreateButton()
{
System.Web.UI.Page currentPage = HttpContext.Current.Handler as Page;
Button b = new Button();
b.Click += new EventHandler(b_Click);//与下句的顺序调换,仍旧不行。
currentPage.FindControl("form1").Controls.Add(ButtonExcStep2);
}void ButtonExcStep2_Click(object sender, EventArgs e)
{
    //该按钮的一些事件,跟踪了,根本就不执行。
}
查过资料说如果在aspx.cs页面这么写,必须放在page_load里!ispostback外,这样的位置。
可问题是我动态创建的不在aspx.cs页面,那怎么做呢?请注意,在"非aspx.cs"动态创建按钮,创建它的事件。谢谢

解决方案 »

  1.   

    还有:不想在UI层的aspx和aspx.cs上添加任何动态按钮相关的代码和控件,全部由WEB项目之外的这个类库来完成这个功能。
      

  2.   


    //刚才代码贴错了,抱歉。
    void CreateButton()
    {
        System.Web.UI.Page currentPage = HttpContext.Current.Handler as Page;
        Button b = new Button();
        b.Click += new EventHandler(b_Click);//与下句的顺序调换,仍旧不行。
        currentPage.FindControl("form1").Controls.Add(b);
    }
    void b_Click(object sender, EventArgs e)
    {
        //该按钮的一些事件,跟踪了,根本就不执行。
    }
      

  3.   

    protected override void CreateChildControls()
    {
         Button btn;
         btn = new Button();
         btn.ID = "btn1";
         btn.Text = "Add the infomation to the survey";
         btn.Click += btn_Click;
         btn.Style.Add("margin", "20px");
    }
    public void btn_Click(object sender, EventArgs e)
    {
           //do something                                  
    }
      

  4.   

    上面那个不太完整,下面是类里的全部代码,跟楼主的需求差不多
    namespace TestSurvey
    {
        public class Class1 : Microsoft.SharePoint.WebPartPages.WebPart
        {
            private TextBox txt;
            private Button btn;
            private Label lbl;        public Class1()
            {        }        public void btn_Click(object sender, EventArgs e)
            {
                 SPSite site;
                 SPWeb web;
                site =
                    new SPSite(string.Format("{0}/{1}", "http://wqc:9000", ""));
                 if (SPContext.Current.Web.CurrentUser != null)
                 {
                     
                     if (site != null)
                     {
                         web = site.RootWeb;
                         if (web != null)
                         {
                             try
                             {
                                 SPList list = web.Lists["职场调查"];
                                 var flag = 0;
                                 foreach (SPListItem temp in list.Items)
                                 {
                                     if (temp["创建者"].ToString().Contains(SPContext.Current.Web.CurrentUser.Name))
                                     {
                                         flag++;
                                     }
                                 }
                                 if(flag > 0)
                                 {
                                     System.Web.HttpContext.Current.Response.Write("<script type='text/javascript'>alert('您已经参与过调查了!');</script>");
                                 }
                                 else if (!string.IsNullOrEmpty(txt.Text))
                                 {
                                     SPListItem item = list.Items.Add();
                                     item["1.人为什么要活着"] = txt.Text;
                                     item.Update();
                                     System.Web.HttpContext.Current.Response.Write(
                                         "<script type='text/javascript'>alert('谢谢,调查完毕!');</script>");
                                 }
                                 web.Close();
                                 site.Close();
                             }
                             catch (Exception ex)
                             {
                                 System.Web.HttpContext.Current.Response.Write(
                                         "<script type='text/javascript'>alert('对不起,调查异常!');</script>");
                             }
                         }
                     }
                 }                                    
            }        
            protected override void CreateChildControls()
            {
                base.CreateChildControls();
                lbl = new Label();
                lbl.ID = "lbl1";
                lbl.Text = "1.人为什么要活着";
                lbl.Style.Add("margin", "20px");
                txt = new TextBox();
                txt.ID = "txt1";
                txt.Style.Add("margin", "20px");
                btn = new Button();
                btn.ID = "btn1";
                btn.Text = "Add the infomation to the survey";
                btn.Click += btn_Click;
                btn.Style.Add("margin", "20px");
                Controls.Add(lbl);
                Controls.Add(txt);
                Controls.Add(btn);
            }        
        }
    }
      

  5.   

    ... 你没理解容器的概念 
    asp.net页面是个容器 void CreateButton(System.Web.UI.Page currentPage)
    {
    Button b = new Button();
    b.Click += new EventHandler(b_Click);//与下句的顺序调换,仍旧不行。
    currentPage.FindControl("form1").Controls.Add(ButtonExcStep2);
    }
      

  6.   

    谢谢小白。你倒数第四、五、六行的Controls是什么?
      

  7.   

    try:    public class Class1
        {
            public void CreateButton(EventHandler handler)
            {
                Page currentPage = HttpContext.Current.Handler as Page;
                Button b = new Button();
                b.Click += handler;
                b.Text = "点一下看看";
                currentPage.FindControl("form1").Controls.Add(b);
            }
        }        protected void Page_Load(object sender, EventArgs e)
            {
                Class1 cl = new Class1();
                cl.CreateButton(new EventHandler(ButtonExcStep2_Click));
            }
            protected void ButtonExcStep2_Click(object sender, EventArgs e)
            {
                Response.Write("调用了");
            }
      

  8.   

       Controls UI层的对象   
      

  9.   

    Controls 是一个区域,类似你代码中往页面里加控件的功能!
      

  10.   

    天行健,很感谢你的回复。我刚才说过了:

    不想在UI层的aspx和aspx.cs上添加任何动态按钮相关的代码和控件, 全部由WEB项目之外的这个类库来完成这个功能。
      

  11.   

    你看清楚一点,我什么时候说在UI层添加动态按钮的代码了
    我的例子里Class1就是你的类库中的类,你妹看到我是按照你的例子来写的吗
      

  12.   

    小白,问你个问题,这个函数 protected override void CreateChildControls() 由谁来触发?无需触发吗?
      

  13.   

     protected void Page_Load(object sender, EventArgs e)
            {
                Class1 cl = new Class1();
                cl.CreateButton(new EventHandler(ButtonExcStep2_Click));
            }
            protected void ButtonExcStep2_Click(object sender, EventArgs e)
            {
                Response.Write("调用了");
            }
    可是这些代码,不是在aspx.cs页面里吗?
      

  14.   

    你的意思是按钮的Click事件处理也在类库里?那不更简单吗:    public class Class1
        {
            public void CreateButton()
            {
                Page currentPage = HttpContext.Current.Handler as Page;
                Button b = new Button();
                b.Click += new EventHandler(b_Click);
                b.Text = "点一下看看";
                currentPage.FindControl("form1").Controls.Add(b);
            }        void b_Click(object sender, EventArgs e)
            {
                HttpContext.Current.Response.Write("调用了");
            }
        }
      

  15.   

    我想保持aspx.cs页面里protected void Page_Load(object sender, EventArgs e) 这个函数里,一行代码都没有,因为有其他的作用。
      

  16.   

    天行健,谢谢你呀,可是那样不行呀,我当初就是这么写的,你看看我第一个帖子。这样写不行,我测试过了,根本不触发b_Click事件的,你试下就知道了。我原来也是觉得就该这么做了,可是不行。//这个类是在WEB项目之外的另外一个类库里。
    public class Class1
        {
            public void CreateButton()
            {
                Page currentPage = HttpContext.Current.Handler as Page;
                Button b = new Button();
                b.Click += new EventHandler(b_Click);
                b.Text = "点一下看看";
                currentPage.FindControl("form1").Controls.Add(b);
            }        void b_Click(object sender, EventArgs e)
            {
                HttpContext.Current.Response.Write("调用了");
            }
        }
      

  17.   

    我试过了,不过我可没有本事在.aspx.cs里一句代码都不写,不写的话怎么调用:        protected void Page_Load(object sender, EventArgs e)
            {
                Class1 cl = new Class1();
                cl.CreateButton();
            }
      

  18.   


    我在一楼的帖子里已经说了,用UI层的aspx.cs里的某个按钮事件来触发“void CreateButton()”函数。调用类库,用aspx上的某个按钮的后台事件(aspx.cs里的单击事件)来触发void CreateButton()嘛~
      

  19.   

    小白,你刚才的代码我测试了。跟踪CreateChildControls() 里的代码,不执行。我public class Class1: System.Web.UI.Page 这么写的。重写了CreateChildControls(),但是调试时不执行。不知道啥原因。
      

  20.   

    呵呵,那是我没看仔细,原来是想在页面的某个按钮的单击事件里创建,那这个就要涉及到asp.net的页面生命周期了,你稍等,我再给你写一个demo
      

  21.   

    try:    public class Class1 : Page
        {
            private bool Added
            {
                get { return ViewState["Added"] != null; }
                set { ViewState["Added"] = value; }
            }
            
            public void CreateButton()
            {
                Added = true;
                Page currentPage = HttpContext.Current.Handler as Page;
                Button b = new Button();
                b.Click += new EventHandler(b_Click);
                b.Text = "点一下看看";
                currentPage.FindControl("form1").Controls.Add(b);
            }        void b_Click(object sender, EventArgs e)
            {
                HttpContext.Current.Response.Write("调用了");
            }
            protected override void OnLoad(EventArgs e)
            {
                if (Added)
                {
                    CreateButton();
                }
            }
        }    public partial class _Default : Class1
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }
            protected void Button1_Click(object sender, EventArgs e)
            {
                CreateButton();
            }
        }
      

  22.   

    修改一下:    public class Class1 : Page
        {
            private bool Added
            {
                get { return ViewState["Added"] != null && Convert.ToBoolean(ViewState["Added"]); }
                set { ViewState["Added"] = value; }
            }        public void CreateButton()
            {
                if (!Added)
                {
                    Added = true;
                    Page currentPage = HttpContext.Current.Handler as Page;
                    Button b = new Button();
                    b.Click += new EventHandler(b_Click);
                    b.Text = "点一下看看";
                    currentPage.FindControl("form1").Controls.Add(b);
                }
            }        void b_Click(object sender, EventArgs e)
            {
                HttpContext.Current.Response.Write("调用了");
            }
            protected override void OnLoad(EventArgs e)
            {
                if (Added)
                {
                    Added = false;
                    CreateButton();
                }
            }
        }
      

  23.   

    這個不錯,收藏一下。
    我以前做動態生成控件時在page_init裡實現。