Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add line break to ASP.net code

Posted on 2011-02-23
5
Medium Priority
?
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

661 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