Solved

Add line break to ASP.net code

Posted on 2011-02-23
5
505 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
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 …
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now