C# 4.0 触发第一个单击事件后,第二个单击事件不会触发

C# 4.0 触发第一个单击事件后,第二个单击事件不会触发,c#-4.0,C# 4.0,我有两个链接按钮,它们是根据条件动态添加的,无论是显示一个还是另一个。这些按钮也有事件 第一个显示的按钮的事件会触发,但当条件发生变化并显示第二个按钮时,它的事件不会触发 源代码: //conditions bool wantToChangeBlogPost = false; string textBoxOnChangeID = ""; protected void displayBlogPost() { SomeArraylist[]

我有两个链接按钮,它们是根据条件动态添加的,无论是显示一个还是另一个。这些按钮也有事件

第一个显示的按钮的事件会触发,但当条件发生变化并显示第二个按钮时,它的事件不会触发

源代码:

    //conditions
    bool wantToChangeBlogPost = false;
    string textBoxOnChangeID = "";

    protected void displayBlogPost()
    {
        SomeArraylist[] arr = valuesFromDdataBase();
        foreach(BlogPost y in arr)
        { 
            string contentStr = y.BlogMailingText;//"some content in mailing";

            //div for displaying content in webpage
            System.Web.UI.HtmlControls.HtmlGenericControl contentDIV = new 
                    System.Web.UI.HtmlControls.HtmlGenericControl("div");
            contentDIV.InnerHtml = contentStr;

            //TB for changes
            TextBox TBcontent = new TextBox();
            TBcontent.Text = contentStr;
            TBcontent.AutoPostBack = true;
            TBcontent.TextMode = TextBoxMode.MultiLine;
            TBcontent.Wrap = true;
            TBcontent.ReadOnly = true;
            TBcontent.EnableViewState = true;


            //two different buttons for cases, whether or not want to change the text 
                            //of blogPost
            LinkButton changePost = new LinkButton();
            changePost.Text = "Change MailingText";
            LinkButton savePost = new LinkButton();
            savePost.Text = "Save Changes";


                //id 's are needed for controls
                TBcontent.ID = "content-" + y.Id;
                contentDIV.ID = "contentDIV-" + y.Id;

                    changePost.ID = "changePost-" + y.Id;
                    savePost.ID = "savePost-" + y.Id;

                    changePost.CommandArgument = "content-" + y.Id;
                    savePost.CommandArgument = "content-" + y.Id;


             //Add these controls to the placeholder, which is defined in asmx:
             //initially add only the contentDiv
             myPlaceHolder.Controls.Add(contentDiv);


             ///////////////////////////
             //  HERE IS THE PROBLEM:  //
             ///////////////////////////

             //Conditions determing when to display one or another linkbutton and 
                                    //TBcontent
             if (wantToChangeBlogPost == true && textBoxOnChangeID == "content-" + y.Id)
                { 
                    savePost.Click += new EventHandler(save_click);
            //HERE IS THE PROBLEM: this event never fires :(
                    contentDIV.InnerHtml = "";
                    TBcontent.ReadOnly = false;
                    TBcontent.Visible = true;

                    // this button is displayd when someone has clicked on button 
                            //'changePost'
                    myPlaceHolder.Controls.Add(savePost);
                }
             else
                {
                    changePost.Click += new EventHandler(changePost_Click); 
                    contentDIV.InnerHtml = contentStr;                         
                    TBcontent.ReadOnly = true;
                    TBcontent.Visible = false;//initially the TB is not visible

                    //initially the bool is false and
                    // this button is displayd
                    myPlaceHolder.Controls.Add(changePost);
                }

        }


        //event methods for both buttons

        //AFTER THIS METHOD COMPLETED I WANT TO DISPLAY THE ANOTHER LINKBUTTON 
                            //'savePost' WITH ANOTHER EVENT 'save_click'
        protected void changePost_Click(object sender, EventArgs e)
        {
            LinkButton LB = sender as LinkButton;
            //CONDITIONS
            textBoxOnChangeID = LB.CommandArgument;
            wantToChangeBlogPost = true;
            //GO TO THE DISPLAYING METHOD AGAIN
            displayBlogPost();
        }

        //THIS METHOD NEVER EVEN FIRES!  WHY??????
        protected void save_click(object sender, EventArgs e)
        {
            LinkButton LB = sender as LinkButton;
            //CONDITIONS
            textBoxOnChangeID = "";
            wantToChangeBlogPost = false;

                    //some logic to send changed data to the database to upload 
                                    //datatable
                    uploadWithChangedDataInTextBox();


            //GO TO THE DISPLAYING METHOD AGAIN
            displayBlogPost();
        }
    }

如果你是动态添加控件,那么你必须在每个帖子上重新创建这些控件,保持id不变,那么只有事件才会触发

或者,如果您通过任何其他方法(如客户端)添加控件,那么您可以调用


\uuuu doPostBack(eventTarget,eventArg)
并检查服务器端的id,然后相应地调用sm函数

这是一个很好的资源,可以获得有关动态控件和回发的更多信息