Solved

Add line break to ASP.net code

Posted on 2011-02-23
5
509 Views
Last Modified: 2012-05-11
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
Comment
Question by:ljhodgett
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 34959464
try

Response.Write("\n")
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 34959481
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
 

Author Comment

by:ljhodgett
ID: 34959521
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34959544
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
 

Author Comment

by:ljhodgett
ID: 34959770
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question