Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Programmatically creating button

Programmatically creating button

Scheduled Pinned Locked Moved ASP.NET
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #1

    I am creating a button, a textbox and a label control, when a linkbutton is clicked. I have assigned an event handler to the button, but when the button is clicked nothing happens and even the previous controls created, disappear protected void LinkButton2_Click(object sender, EventArgs e) { TableRow row = new TableRow(); TableCell cell1 = new TableCell(); txt = new TextBox(); txt.Rows = 2; txt.Height = 40; txt.Width = 200; cell1.Controls.Add(txt); row.Controls.Add(cell1); btn = new Button(); btn.Text = "Submit"; btn.Click += new EventHandler(btn_Click); TableCell cell2 = new TableCell(); cell2.Controls.Add(btn); row.Controls.Add(cell2); lbl1 = new Label(); lbl1.Text = "Your Comments"; TableCell cell3 = new TableCell(); cell3.Controls.Add(lbl1); row.Controls.Add(cell3); Table1.Controls.Add(row); } void btn_Click(object sender, EventArgs e) { //Does not reach here }

    A 1 Reply Last reply
    0
    • H humayunlalzad

      I am creating a button, a textbox and a label control, when a linkbutton is clicked. I have assigned an event handler to the button, but when the button is clicked nothing happens and even the previous controls created, disappear protected void LinkButton2_Click(object sender, EventArgs e) { TableRow row = new TableRow(); TableCell cell1 = new TableCell(); txt = new TextBox(); txt.Rows = 2; txt.Height = 40; txt.Width = 200; cell1.Controls.Add(txt); row.Controls.Add(cell1); btn = new Button(); btn.Text = "Submit"; btn.Click += new EventHandler(btn_Click); TableCell cell2 = new TableCell(); cell2.Controls.Add(btn); row.Controls.Add(cell2); lbl1 = new Label(); lbl1.Text = "Your Comments"; TableCell cell3 = new TableCell(); cell3.Controls.Add(lbl1); row.Controls.Add(cell3); Table1.Controls.Add(row); } void btn_Click(object sender, EventArgs e) { //Does not reach here }

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      You need to create all dynamic control before page_Load, best to create them on Page_PreInit. Other wise the control will not able to maintain viewstate and postback data. As LoadPostBack and LoadViewState Fired before Page_Load.

      Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

      H 1 Reply Last reply
      0
      • A Abhijit Jana

        You need to create all dynamic control before page_Load, best to create them on Page_PreInit. Other wise the control will not able to maintain viewstate and postback data. As LoadPostBack and LoadViewState Fired before Page_Load.

        Abhijit Jana | Codeproject MVP Web Site : abhijitjana.net Don't forget to click "Good Answer" on the post(s) that helped you.

        H Offline
        H Offline
        humayunlalzad
        wrote on last edited by
        #3

        When recreating the controls in Page_PreInit, the line Table1.Controls.Add(row); gives an error.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups