kamiyann
kamiyann

Reputation: 53

Asp.net /vb elements generated in code behind doesnt render in HTML

i will try to explain myself better.

I have this problem in my asp.net vb, in the code behind i am generating html content, with data being pulled from a database.

The problem comes when i have to generate an asp element (lets say, an asp button or an asp dropdownlist), problem is that the asp elements generate in the html code. But they dont show up, the button and the dropdown list are invisible, if i hit F12 the code is there.

This is the code im generating:

In aspx page i have this:

<div id="tarjetas"  runat="server"></div>

Then in code behind i have this:

html = "<div class='col-lg-3'> <div class='panel panel-default'> <div class='panel-heading' style=' background-color: #1c72dc; '>Nº: " & id_ticket.ToString & " - " & Urgente.ToString & " </div> <!-- /.panel-heading --> <div class='panel-body'> <div class='row'> <div class='col-lg-12 col-lg-12'> <div class='thumbnail' style='background-color:rgba(60, 88, 188, 0.15); border-color: red;'>  <div class='caption'> <h2>DIR: " & Calle_Numero.ToString & "</h2> <h4>ED: " & Nombre_Edificio.ToString & "</h4> <h4>TIPO: " & Tipo_Urgente.ToString & "</h4> <h4>TEL: " & Tel_Contacto.ToString & "</h4> <h5>DETALLE: " & Detalle_Problema.ToString & "</h5> <p style='text-align: center;'> <asp:DropDownList ID='ddlGender' runat='server' Width='200px'> <asp:ListItem Text='Select Gender' Value='0'></asp:ListItem> <asp:ListItem Text='Male' Value='1'></asp:ListItem> <asp:ListItem Text='Female' Value='2'></asp:ListItem> </asp:DropDownList> </p> <p style='text-align: center;'>   </p> </div>   </div>  </div>  </div>  </div>  </div>  </div>

That's from the code behind. Then i do

Label.Text = html
tarjetas.Controls.Add(Label)

How can i make the buttons,especifically dropdownlist, to show up in aspx ?

Rendered HTML

Thanks in advance, im new to this. Im googled a lot but cant find answer.

Upvotes: 0

Views: 265

Answers (1)

VDWWD
VDWWD

Reputation: 35514

What you are doing is never going to work. You are creating asp:controls as a string and adding them to the page. Therefore they do not even show up in the browser since they do not understand the tag <asp:.

OPTION 1

Create a "real" DropDownList in the html string and place that on the page.

<select name="ddlGender" id="ddlGender">
    <option value="1">Item 1</option>
    <option value="2">Item 2</option>
</select>

Code behind

Dim html As String = "<select name=""ddlGender"" id=""ddlGender""><option value=""1"">Item 1</option><option value=""2"">Item 2</option></select>"

OPTION 2 (best choice)

What you can do is place a Control on the aspx page directly and then fill it with ListItems, either in code behind or on the page itself.

<asp:DropDownList ID="ddlGender" runat="server">
    <asp:ListItem Text="Item 1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Item 2" Value="2"></asp:ListItem>
</asp:DropDownList>

Code behind.

ddlGender.DataSource = mySource
ddlGender.DataTextField = "textField"
ddlGender.DataValueField = "valueField"
ddlGender.DataBind

//or

ddlGender.Items.Insert(0, New ListItem(("Item 1 - " + i), ("1-" + i.ToString), true))
ddlGender.Items.Insert(1, New ListItem(("Item 2 - " + i), ("2-" + i.ToString), true))

OPTION 3 (most complex)

Create the Controls dynamically in code behind and add them to a PlaceHolder

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    Dim i As Integer = 0
    Do While (i < 5)
        Dim ddl As DropDownList = New DropDownList
        ddl.ID = ("ddlGender_" + i)
        ddl.Items.Insert(0, New ListItem(("Item 1 - " + i), ("1-" + i.ToString), true))
        ddl.Items.Insert(1, New ListItem(("Item 2 - " + i), ("2-" + i.ToString), true))
        PlaceHolder1.Controls.Add(ddl)
        i = (i + 1)
    Loop

End Sub

Upvotes: 1

Related Questions