maika paika

maika paika

  • NA
  • 27
  • 5.5k

After first click_event was fired, the second click_event does not fire

Jul 21 2011 7:49 AM
I have two linkbuttons which are added dynamically depending on conditions, whether to display one or another. These buttons have events also.

Firstly displayed button's event fires, but when conditions change and second button displayed, it's event does not fire.

source code:

//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 = "changable-" + y.Id;
                savePost.CommandArgument = "changable-" + 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;// changePost.CommandArgument = "changable-" + y.Id;
        //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;// savePost.CommandArgument = "changable-" + y.Id;
        //CONDITIONS
        
textBoxOnChangeID = "";
        wantToChangeBlogPost = false;
       
                //some logic to send changed data to the database to upload datatable
                uploadWithChangedDataInTextBox();
       
       
        //GO TO THE DISPLAYING METHOD AGAIN
        displayBlogPost();
    }
}


Answers (2)