Search code examples
htmlasp.netvb.netwebformsasp.net-3.5

Dynamically adding divs and images during runtime - vb


I have a table full of users which includes a username and an image. What I'm trying to do is for every user in the table, I want to create a new div with a class of "user" which contains a label for their username and their image. Here's what I have so far:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim label1 As New Label
    label1.ID = "label1"
    label1.Text = "Username goes here"
    Controls.Add(label1)
End Sub

I found out how to add a label but I'm not sure how I'd position it, or add HTML elements with a label and image object inside them. Here's what I'm hoping the result will look like:

<div class="user">
        <h2> Username </h2>
        <img src="user.png">
</div>

Hopefully this could then be repeated as many times as necessary for each user.


Solution

  • Or you can do it like this, here an example in c#

            var div = new Panel();
            div.Controls.Add(new LiteralControl("<h1>hallo</h1>"));
            div.Controls.Add(new Image());
            Page.Form.Controls.Add(div);