Results 1 to 5 of 5

Thread: [RESOLVED] Binding a GridView programmatically with datatable containing html tags

  1. #1

    Thread Starter
    Lively Member n3xus's Avatar
    Join Date
    Feb 2007
    Location
    c:\Programme files\
    Posts
    98

    Resolved [RESOLVED] Binding a GridView programmatically with datatable containing html tags

    let's say we have datatable with "<a>12</a>" as rows.
    when i try to bind it to gridview programmatically
    I'm not getting href links like 12 But just a strings like "<a>12</a>"

    Code:
    Dim datatable as new DataTable
    datatable.Columns.Add("No")
    
    dim datarow1 ad DataRow = datatable.newrow()
    
    datarow1(0) = "<a>12</a>"
    
    datatable.rows.add(datarow1)
    
    gridview1.DataSource = datatable
    gridview1.DataBind()

  2. #2
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Binding a GridView programmatically with datatable containing html tags

    On the grid column set HtmlEncode="false". By default it's true so <> tags will be encoded and appear as text.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  3. #3

    Thread Starter
    Lively Member n3xus's Avatar
    Join Date
    Feb 2007
    Location
    c:\Programme files\
    Posts
    98

    Re: Binding a GridView programmatically with datatable containing html tags

    Thanks For the reply. I solved the problem. Problem i had was columns generated automatically and there was no way to change properties.

    Following is the snippet i have used.
    Code:
    Protected Sub GridDun_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridDun.RowDataBound
            If e.Row.RowType = DataControlRowType.DataRow Then
                For i As Integer = 0 To e.Row.Cells.Count - 1
                    e.Row.Cells(i).Text = Server.HtmlDecode(e.Row.Cells(i).Text)
                Next
            End If
        End Sub

  4. #4
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: [RESOLVED] Binding a GridView programmatically with datatable containing html tag

    It seams strange that there is no way to set that property when auto generating columns. I couldn't find any.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Binding a GridView programmatically with datatable containing html tag

    Depending on where the contents of the cells are coming from, i.e. from user input, it is good practice to always HtmlEncode and HtmlDecode so that you don't get any unexpected side effects from displays the content on your page.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width