2
votes

I'm adding controls to a panel on an Asp.Net webforms page and I'd like to change the html it generates. A simplified example of what I'm doing:

Dim control1 As New HiddenField()
control1.ID = "Control1"
control1.Value = "Text"

Dim control2 As New HiddenField()
control2.ID = "Control2"
control2.Value = "Text"

Panel1.Controls.Add(control1)
Panel1.Controls.Add(control2)

This generates the following HTML:

<input type="hidden" name="Control1" id="Control1" value="Text" /><input type="hidden" name="Control2" id="Control2" value="Text" />

Is there a way to ensure that there's a newline in between every control? The current source is a pretty big mess when you add more controls. I'd like the html to be formatted like this:

<input type="hidden" name="Control1" id="Control1" value="Text" />
<input type="hidden" name="Control2" id="Control2" value="Text" />
2
Are you sure it should be Panel2.Controls.Add(control2)? Looks like it should be Panel1.Controls.Add(control2) - freefaller
You're right, edited the example - Freek Buurman

2 Answers

2
votes

Assuming both controls should be added to Panel1, try this...

Panel1.Controls.Add(control1)
Panel1.Controls.Add(New LiteralControl(Environment.NewLine))
Panel1.Controls.Add(control2)
0
votes

If you don't like how certain control generates itself, you can always create one yourself. This is a fairly simple example and I'm sure you'd have it done it few minutes.