-4

i have an issue in creating dynamic page. i need to create dynamic table,in that the table contains two text boxes and one button.i created and it displayed with help of string builder.

what i want is i need to write C# code for dynamically created button.is it possible to append server button?when i click dynamically created button it should go to code behind.

sb.append("<input type='Button' runat='server'  onclick='Myfun()'");
2
  • 1
    Is this WebForms or MVC? Can you show the code for how you're rendering the layout so far?
    – Yuck
    Jun 21, 2013 at 14:22
  • it is of web form not mvc Jun 21, 2013 at 15:06

1 Answer 1

3

If it's asp.net webforms you can do this:

Button mybutton = new Button();
mybutton.Click += new EventHandler(Mybutton_Click);
container.Controls.Add(mybutton);

Make sure you add it to your OnInit method .

4
  • 1
    +1 Well done. I was writing something similar myself. It's worth mentioning that this code should be somewhere in Page_Load and should only be done if it's not a post back (i.e. IsPostBack == false).
    – Yuck
    Jun 21, 2013 at 14:25
  • i need to create with help of string builder ,,how can i write using string builder Jun 21, 2013 at 14:57
  • @user1611125 You definitely don't need to be building controls using StringBuilder and raw HTML. Follow the approach above.
    – Yuck
    Jun 21, 2013 at 16:13
  • for div it is possible to add like that and apply css also Jun 21, 2013 at 17:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.