4

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?

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
Jivan
  • 1,300
  • 6
  • 21
  • 33
  • Does\should the click event cause a page the to post back ? – Mark Broadhurst Aug 10 '12 at 11:00
  • No it doesnot get postback @Saint – Jivan Aug 10 '12 at 11:01
  • @burning_LEGION, what did you edit on my question? I can't find any improvement? – Jivan Aug 10 '12 at 11:07
  • So basicaly you have 2 solutions to your issue you can make it so that the page is posted back on every click, which is probably not the best user experience but easier to fix, or you can implement a javascript solution which would be more work but you would have a better product. Which do you want ? – Mark Broadhurst Aug 10 '12 at 11:14

5 Answers5

3

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)

Community
  • 1
  • 1
WraithNath
  • 17,658
  • 10
  • 55
  • 82
  • Did you mean I have to create the onclick handler on OnInit method? @WraithNath – Jivan Aug 10 '12 at 11:10
  • Hi, yes, you could try the OnInit method of the page you are using, you could try and find the control in the oninit method and add the handler from there. One other thing you could try is having the button there in the designer but with style="display:none" or visible=false this way it would not be render but you could still add the handler without having to find the control (depeing on whether its in a grid or just on the page etc) – WraithNath Aug 10 '12 at 12:05
1

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";

Rroman
  • 666
  • 1
  • 7
  • 11
0

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

}
Anand
  • 14,545
  • 8
  • 32
  • 44
  • Thanks for your help, but the error saying 'object reference not set' occurs, I think it is due to loading of the button for which session is not created. How can it be solved? @Anand – Jivan Aug 10 '12 at 11:33
  • Thats not the full working code. Obviously there are things that I have missed(Like I never created an instance of Lists to hold button and textboxes). It was just a pointer in direction of how to do it. – Anand Aug 11 '12 at 05:09
0

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);
 } 
Waqar Janjua
  • 6,113
  • 2
  • 26
  • 36
-1

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
   });
});
Asmarah
  • 134
  • 8