Creating dynamic server controls

Discussion in 'C#' started by 70Cents, Nov 9, 2009.

  1. #1
    Hi,

    I've been struggling with this for the whole day... :(

    I need to create a list of buttons in a table, all buttons have the same OnClick event. I use code-behind to make the table, it's something like this:

    for (int i = 0; i < end; i++)
    {
    response += @"<tr><td>Data</td><td>Data2</td><td>Data3</td><td><asp:Button runat='server' /></tr>"
    }

    Of course, this doesn't work. I don't have asp:Buttons to work with.

    I tried to do Ajax buttons, but they also need to run on server side...

    Can anyone help me with that?

    Thanks a lot
     
    70Cents, Nov 9, 2009 IP
  2. 70Cents

    70Cents Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I solved this problem by using Table class and TableCell.AddControl() method
     
    70Cents, Nov 11, 2009 IP
  3. Sosyopat

    Sosyopat Active Member

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    for (int i = 0; i < end; i++)
    {
    Button btn = new Button();
    btn.Text="Button "+i;
    btn.Click +=new EventHandler(btn_Click);
    Page.Controls.Add(btn);
    }

    this code adds buttons on your page, and all buttons have same click event.
    maybe it'll be helpfully.
     
    Sosyopat, Nov 11, 2009 IP
  4. 70Cents

    70Cents Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sosyopat, you're right. But this will add buttons directly to the page. I needed it to be in a table in the last column. That's why I built the table dynamically first. And then used Controls.Add()
     
    70Cents, Nov 11, 2009 IP
  5. charlota

    charlota Peon

    Messages:
    210
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Haiii...

    Thanks for you're information about that ..
    mybe you must used Table class..for creating dynamic server controls ...

    I have information about Windows Hosting provided by ASP.NET with Microsoft License ..Visit webhostforasp.net for more their windows hosting services...
     
    charlota, Nov 24, 2009 IP