Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 523
  • Last Modified:

Add line break to ASP.net code

Hi All,

I've got the following code: -

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For n As Integer = 0 To 2

            Dim btn As New ImageButton
            btn.ImageUrl = "~/x.bmp"
            btn.ID = Trim(Str(n))
            AddHandler btn.Click, AddressOf ShowDetails
            Pnl.Controls.Add(btn)

            Dim btn2 As New Button
            btn2.ID = Trim(Str(n) & "Button")
            btn2.Text = "Delete Question"
            AddHandler btn2.Click, AddressOf DeleteQuestion
            Pnl.Controls.Add(btn2)
            Dim MessageToDisplay As String = ("Are you sure u want to delete question: " & n & "?")
            btn2.Attributes.Add("onClick", "javascript:if(!confirm('" & MessageToDisplay & "')) return false;")

            Dim txtb As New TextBox
            txtb.ID = Trim(Str(n) & "A")
            Pnl.Controls.Add(txtb)

            Dim lstA As New ListBox
            lstA.ID = Trim(Str(n) & "B")
            lstA.Items.Add("Yes \ No")
            lstA.Items.Add("Question")
            lstA.Items.Add("Routing")
            Pnl.Controls.Add(lstA)

            Dim lbl As New Label
            lbl.Text = "Cost>£100"
            Pnl.Controls.Add(lbl)
            Response.Write("<br>")
        Next
    End Sub

The code creates the controls dynamically and I would like to put a line break between each set of controls. I've tried response.write("<br />") but this doesn't seem to work and think I'm missing something.

thanks for your help.

Lee
0
ljhodgett
Asked:
ljhodgett
  • 2
  • 2
1 Solution
 
Pratima PharandeCommented:
try

Response.Write("\n")
0
 
Carl TawnSystems and Integration DeveloperCommented:
That's because you are trying to do an arbitary Response.Write in the middle of creating a collection of controls, which isn't going to work. What you should do instead is add a LiteralControl to the collection with a <br /> tag in it:
Dim lc As New LiteralControl()
lc.Text = "<br />"
Pnl.Controls.Add(lc)

Open in new window

0
 
ljhodgettAuthor Commented:
Hi Carl,

Many Thanks for that.

As this is the case does this mean that I cant create the controls dynamically in a table as I was going to use the page break as a starting block to encasing the controls in an arrangement with table html code?

Many thanks
Lee
0
 
Carl TawnSystems and Integration DeveloperCommented:
You can create any arrangement of controls you like, you just can't Response.Write arbitary code because the result won't output in sequence with the items in the Controls collection. If you need to output raw html in a specific place relative to the other controls then you need to add it as a literal control.

I didn't quite get what you meant about the table. If you want to use a table you can also create that in code and create a new TableRow for each set of controls if that is what you mean.
0
 
ljhodgettAuthor Commented:
Many Thanks for your help Carl,

The table is just so I can place the controls in certain areas to format the page. I've managed to do it with the code below: -

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim lc As New LiteralControl()
        lc.Text = "<Table>"
        Pnl.Controls.Add(lc)

        For n As Integer = 0 To 2
            Dim lc2 As New LiteralControl()
            lc2.Text = "<tr><td>"
            Pnl.Controls.Add(lc2)

            Dim btn As New ImageButton
            btn.ImageUrl = "~/x.bmp"
            btn.ID = Trim(Str(n))
            AddHandler btn.Click, AddressOf ShowDetails
            Pnl.Controls.Add(btn)

            Dim btn2 As New Button
            btn2.ID = Trim(Str(n) & "Button")
            btn2.Text = "Delete Question"
            AddHandler btn2.Click, AddressOf DeleteQuestion
            Pnl.Controls.Add(btn2)
            Dim MessageToDisplay As String = ("Are you sure u want to delete question: " & n & "?")
            btn2.Attributes.Add("onClick", "javascript:if(!confirm('" & MessageToDisplay & "')) return false;")

            Dim txtb As New TextBox
            txtb.ID = Trim(Str(n) & "A")
            Pnl.Controls.Add(txtb)

            Dim lstA As New ListBox
            lstA.ID = Trim(Str(n) & "B")
            lstA.Items.Add("Yes \ No")
            lstA.Items.Add("Question")
            lstA.Items.Add("Routing")
            Pnl.Controls.Add(lstA)

            Dim lbl As New Label
            lbl.Text = "Cost>£100"
            Pnl.Controls.Add(lbl)

            Dim lc3 As New LiteralControl()
            lc3.Text = "</td><td>test</td></tr>"
            Pnl.Controls.Add(lc3)

        Next
        Dim lc4 As New LiteralControl()
        lc4.Text = "</Table>"
        Pnl.Controls.Add(lc4)
    End Sub

Not sure if this is the most efficient way of doing it but it seems to work so I can build on it from here.

cheers
Lee
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now