I want to add a new set of .net controls and html tags when a button is clicked.
At this moment I have a placeholder where I'm adding controls dynamically from code behind in VB.Net and it is right done but, whenever there is a postback (when a button is clicked or a dropdown is changed its value) they are not re-generated. Which is the right way to maintain these elements?
I have searched in these pages (msd, stackoverflow1 stackoverflow2 ) but it just show "how to add" but no "how to maintain" controls Below is my code if you could help me: This is the aspx:
<asp:PlaceHolder ID="reportFields" runat="server"></asp:PlaceHolder>
And this is the code Behind:
Private Sub generatenewReportField(count As Integer)
reportFields.Controls.Add(New LiteralControl("<div class=""col-sm-12"">"))
'Name field
reportFields.Controls.Add(New LiteralControl("<div class=""form-group col-sm-3"">"))
reportFields.Controls.Add(New LiteralControl("<label for=""txtNewFieldName" & count & """>Name</label>"))
Dim tx1 As New TextBox()
tx1.CssClass = "form-control input-sm"
tx1.ID = "txtNewFieldName" & count
tx1.ToolTip = "New Field Name"
reportFields.Controls.Add(tx1)
reportFields.Controls.Add(New LiteralControl("</div>"))
'formula field
reportFields.Controls.Add(New LiteralControl("<div class=""form-group col-sm-3"">"))
reportFields.Controls.Add(New LiteralControl("<label for=""txtNewFieldFormula" & count & """>Formula</label>"))
Dim tx2 As New TextBox()
tx2.CssClass = "form-control input-sm"
tx2.ID = "txtNewFieldFormula" & count
tx2.ToolTip = "New Field Formula"
reportFields.Controls.Add(tx2)
reportFields.Controls.Add(New LiteralControl("</div>"))
'isKPI?
reportFields.Controls.Add(New LiteralControl("<div class=""form-group col-sm-2"">"))
reportFields.Controls.Add(New LiteralControl("<label for=""chkKPI" & count & """>Is KPI</label><br/>"))
Dim isKPI As New CheckBox
isKPI.CssClass = " switch input-sm"
isKPI.ID = "chkKPI" & count
initializeSwitch()
reportFields.Controls.Add(isKPI)
reportFields.Controls.Add(New LiteralControl("</div>"))
'KPI Weight
reportFields.Controls.Add(New LiteralControl("<div class=""form-group col-sm-3"">"))
reportFields.Controls.Add(New LiteralControl("<label for=""txtNewFieldKPIWeight" & count & """>Weight</label>"))
Dim tx3 As New TextBox()
tx3.CssClass = "form-control input-sm"
tx3.ID = "txtNewFieldKPIWeight" & count
reportFields.Controls.Add(tx3)
reportFields.Controls.Add(New LiteralControl("</div>"))
reportFields.Controls.Add(New LiteralControl("</div>"))
reportFields.Controls.Add(New LiteralControl("<br/>"))
End Sub
Tanks for the help!