0

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.

4
  • 1
    Where is your code and a Minimal, Complete, and Verifiable example? Now we can only speculate if you are adding Controls dynamically or just creating them in HTML as a string. Commented Feb 7, 2017 at 15:00
  • Oh damn! I forgot, i'll edit the post with that info. My bad . Commented Feb 7, 2017 at 15:04
  • 1
    Maybe post your fully rendered HTML and CSS? It could be a markup issue, or it could be a CSS or even scripting issue. Commented Feb 7, 2017 at 15:23
  • Will do good sir! Commented Feb 7, 2017 at 15:26

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Sadly none of the options will work, because i am dynamically generating "cards" with that html string, inside them i need the dropdownlist. On the aspx page i cant have more code. Anyways, thanks to your great answer im thinking of using an html element(dropdown or select). Do you think it will be possible that it will be possible to get the selected value of that select(generated dynamically) in the code behind ? I ask this because you read my code and understand the situation. Thanks in advance, people like u make the internet a wonderfull place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.