Saturday, February 19, 2011

Render < instead of < in ASP.NET

I'm writing a small page to show the employees in our company. I've written a query to pull the info from our database. Then I'm binding that to a GridView to do the dirty work. Here is the query.

"SELECT tblEmpID.empid AS [Empl ID], tblEmpID.posno AS [Pos #], [name] & ""<br />""  &   [jcn] & ""("" & [jcc] & "")"" AS [Name/Job], [orgno] & "" - "" & [depname] AS Department, tblEmpID.[status] AS Status " & _
        "FROM tblEmpID " & _
        "ORDER BY [orgno] & "" - "" & [depname], tblEmpID.name "

As you can see, I'm trying to include a
inside the SQL so when it renders it will look like:

Name
Job Description

But when it renders it renders as

&lt; and &gt;

Effectively showing the <br /> in the record instead of formatting it like I want.

So how to I make it render like I want? I've already tried escaping the < with \ and that did not work.


EDIT: Thanks gfrizzle. Your answer set me down the right path. Also, thank you NYSystemsAnalyst. Your answer helped me think of a different way to do things in the future. Ultimately, I found a different solution. I put this code in the GridView1_RowDataBound event and it does what I need.

If e.Row.RowType = DataControlRowType.DataRow Then
        Dim cells As TableCellCollection = e.Row.Cells

        For Each cell As TableCell In cells
            cell.Text = Server.HtmlDecode(cell.Text)
        Next
    End If
From stackoverflow
  • If you are doing this for read-only purposes, you may want to consider using the repeater control. Then, you can return those as separate fields, thereby eliminating the HTML from the SQL result set. Then, you can use the ItemTemplate in the control to specify the HTML and exactly how you want the results to appear. You can place them in a table, and use the BR tag. This will look similar to a grid, but give you more control over the layout on the HTML / .aspx side.

  • Try setting HtmlEncode="False" on the column in the GridView. That should stop it from encoding your markup.

0 comments:

Post a Comment