Solved

Add line break to ASP.net code

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

914 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

21 Experts available now in Live!

Get 1:1 Help Now