Jivan
Jivan

Reputation: 1320

How to create an onClick eventHandler for a dynamically created button

Currently, I am doing a project for students' hostel and now I have to implement some search strategies about students.Here I have to create a button dynamically when the user clicks on the another server button in .aspx page and accordingly I have to create the onclick event handler for the newly created button. The code-snippet that I used is:

protected void btnsearchByName_Click(object sender, EventArgs e)
    {
        TextBox tbsearchByName = new TextBox();
        Button btnsearchName = new Button();
        tbsearchByName.Width = 250;
        tbsearchByName.ID = "tbsearchByName";
        tbsearchByName.Text = "Enter the full name of a student";
        btnsearchName.ID = "btnsearchName";
        btnsearchName.Text = "Search";
        btnsearchName.Click += new EventHandler(this.btnsearchName_Click);

        pnlsearchStudents.Controls.Add(tbsearchByName);
        pnlsearchStudents.Controls.Add(btnsearchName);
    }
     protected void btnsearchName_Click(object sender, EventArgs e)
    {
        lblsearch.Text = "btnsearchName_Click event fired in " + DateTime.Now.ToString();

    }

Here, the problem is newly created eventHandler doesnot get fired. I have gone through this site and looked several questions and answers and also gone through the page life-cycle and they all say that the dynamic button should be on Init or Pre_init, but my problem is I have to create it when another button is clicked, how can it be possible?

Upvotes: 4

Views: 40392

Answers (5)

WraithNath
WraithNath

Reputation: 18013

You need to add the click handler for the button on every postback.

you could look for the button in the search students panel on page load or try the page OnInit() method to add the handler when its created.

Also check here:

Dynamically added ASP.NET button click handler being ignored

and here: asp.net dynamically button with event handler

and here: asp:Button Click event not being fired

(all of which give similar suggestions)

Upvotes: 3

Waqar Janjua
Waqar Janjua

Reputation: 6123

I am not sure but may be you have to override the OnInit() method like this.

 protected override void OnInit(EventArgs e)
 {
    base.OnInit(e);
 } 

Upvotes: 0

Anand
Anand

Reputation: 14955

You need to recreate the button and attach the event handler every time. For this, create a list of button and save it on session. On page load, go through the List and create the button every time

public Button create_button()
{
        btnsearchName.ID = "btnsearchName";
        btnsearchName.Text = "Search";
        btnsearchName.Click += new EventHandler(this.btnsearchName_Click);

       return btnsearchName;
 }

 public TextBox create_textbox()
 {
      TextBox tbsearchByName = new TextBox();
        Button btnsearchName = new Button();
        tbsearchByName.Width = 250;
        tbsearchByName.ID = "tbsearchByName";
        tbsearchByName.Text = "Enter the full name of a student";
        return tbsearchByName;
 }


protected void btnsearchByName_Click(object sender, EventArgs e)
{
    TextBox tbsearchByName = create_textbox();
    Button btnsearchName = create_button();
    //add to panels
    pnlsearchStudents.Controls.Add(tbsearchByName);
    pnlsearchStudents.Controls.Add(btnsearchName);

   //add to session
   List<Button> lstbutton = Session["btn"] as List<Button>
   lstbutton.add(btnsearchName);
   //similarly add textbox

  //again add to session
  Session["btn"] = lstbutton 
}

public override page_load(object sender, eventargs e)
{
   //fetch from session, the lstButton and TextBox and recreate them
   List<Button> lstbutton = Session["btn"] as List<Button>;
   foreach(Button b in lstbutton)
       pnlsearchStudents.Controls.Add(b);

   //similar for textbox

}

Upvotes: 0

Rroman
Rroman

Reputation: 666

Try this http://msdn.microsoft.com/ru-ru/library/system.web.ui.webcontrols.button.command(v=vs.90).aspx

btnsearchName.Command += new CommandEventHandler(this.btnsearchName_Click);

btnsearchName.CommandName = "Click";

Upvotes: 1

Asmarah
Asmarah

Reputation: 134

You just need to add this code on ready state of jquery code and it will work fine for the dynamic button too

$(document).ready(function(){
   $('input#tbsearchByName').click(function(){
         // code goes here
   });
});

Upvotes: -1

Related Questions