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. dynamic button

dynamic button

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
2 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.
  • D Offline
    D Offline
    david boon
    wrote on last edited by
    #1

    hi, in my page load iam creating a row consisting of text box,drp down and button. on clicking the button a similar row should be created. how to fire the event of dynamically created button? code is there but not working. plz help me out Regards, Boon

    M 1 Reply Last reply
    0
    • D david boon

      hi, in my page load iam creating a row consisting of text box,drp down and button. on clicking the button a similar row should be created. how to fire the event of dynamically created button? code is there but not working. plz help me out Regards, Boon

      M Offline
      M Offline
      Mike Ellison
      wrote on last edited by
      #2

      Hi there. Are you assigning the event handler function to your button's click event? myButton.Click += new System.EventHandler(myButtonClickHandlerFunc); Here's a fuller example of the kind of thing you're describing. Does this help?

      <% @Page Language="C#" %>

      <script runat="server">

      const string SESSION_ROWCOUNT = "RowCount";

      void Page_Load(object o, EventArgs e)
      {
      if (!IsPostBack)
      // create the first row
      CreateARow(0);
      else
      // recreate all the rows that existed
      RecreateRows();
      }

      void CreateARow(int index)
      {
      TableRow r = new TableRow();
      TableCell c = new TableCell();

      TextBox tb = new TextBox();
      tb.ID = "text\_" + index.ToString();
      c.Controls.Add(tb);
      
      Button b = new Button();
      b.ID = "button\_" + index.ToString();
      b.Text = "Add Another";
      b.Click += new System.EventHandler(ButtonClick);
      c.Controls.Add(b);
      
      r.Cells.Add(c);
      myTable.Rows.Add(r);
      
      // store the count in a session variable so we can recreate
      // the existing rows on a postback
      Session\[SESSION\_ROWCOUNT\] = myTable.Rows.Count;
      

      }

      void ButtonClick(object o, EventArgs e)
      {
      CreateARow(myTable.Rows.Count);
      }

      void RecreateRows()
      {
      int iCount = 0;
      if (Session[SESSION_ROWCOUNT] != null)
      iCount = Convert.ToInt32(Session[SESSION_ROWCOUNT]);

      for (int i=0; i<iCount; i++)
      {
          CreateARow(i);
      }
      

      }

      </script>

      <html>
      <head>
      <title>Dynamic TableRow Example</title>
      </head>

      <body>
      <form runat="server">
      <asp:Table id="myTable" runat="server">
      </asp:Table>

          <br />
          <br />
          <asp:Button id="btnForcePostback" runat="server"
                      Text="Force a non-row-creating postback"
                      />
      </form>    
      

      </body>

      </html>

      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