ASP.NET Datagrid HTML Output (Display)

I am currently binding a dataset with a datagrid.

ds = query.ExecuteReadQuery("select PollQuestionText as 'Survey Question',    
PollAnswer1Text as 'Selection 1', PollAnswer2Text as 'Selection 2', PollAnswer3Text  
as 'Selection 3', PollEnabled 'Status'  from tbl_pollquestions")

For Each row As Data.DataRow In ds.Tables(0).Rows
        If row.ItemArray(4).ToString = "0" Then
            row.ItemArray."<a href=""""> <img src=""img/box_icon_edit_pencil1.gif"" border=""0""> </a>"

        ElseIf row.ItemArray(4).ToString = "1" Then
            row.Item(4) = "<a href=""""> <img src=""img/box_icon_edit_pencil2.gif"" border=""0""> </a>"
        End If

    Next

GridView1.DataSource = ds

GridView1.DataBind()

      

Since I am pasting the html code, why is it not converting to html?

All text is output. (Let's assume the icon is displayed without url redirection)

I do not know why.

thank

0


source to share


3 answers


Here's one quick way to solve your problem without using a content template.

First add the event RowDataBound

to your GridView.

<asp:GridView ID="GridView1" runat="server" onrowdatabound="GridView1_RowDataBound">
</asp:GridView>

      

Second, add code for the event handler using your logic. The RowDataBound event will fire for every row, we don't need to use foreach

. I am using C #, but you can easily convert it to VB.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {    
    if (e.Row.RowType == DataControlRowType.DataRow) {        
        if (e.Row.Cells[4].Text == "0") {            
            e.Row.Cells[4].Text = "<a href=''> <img src='img/box_icon_edit_pencil1.gif' border='0'> </a>"
        } else {
            e.Row.Cells[4].Text = "<a href=''> <img src='img/box_icon_edit_pencil2.gif' border='0'> </a>"
        }
    }
}

      



As a side note, you can change

<a href=''> <img src='img/box_icon_edit_pencil1.gif' border='0'> </a>
<a href=''> <img src='img/box_icon_edit_pencil2.gif' border='0'> </a>

      

to

<a href="" class="Pencil1"></a>
<a href="" class="Pencil2"></a>

      

and set the background image using CSS.

+2


source


To get the GridView to render HTML, all you have to do is set the HtmlEncode parameter to false in the desired bounding box.



<asp:BoundField DataField="Question" HeaderText="Question" HtmlEncode="false" />

      

+2


source


If you are using a gridview you can use it as intended.

You must have a content template.

If you need to do value-based formatting, you do so in the rowdatabound event.

I think you are getting "unexpected" behavior, because the gridview can bind to a wide variety of collections (Array, Hashtable, Dataset, etc.) and it controls how it binds data specifically.

The purpose of the gridview is to do the formatting in the html section of the page ... There is a lot of fancy formatting you can do there.

If you intend to use gridviews, it is good to be familiar with the onrowdatabound and onrowcommand events ...

QUICK FIX:

I think it might take a little time to find out how to do it right. In the meantime, if you want to quickly fix your problem with the least amount of changes:

  • use an asp: literal control for where you want your image.
  • change your query in db to replace return value with html instead of 0/1 value
  • bind literal to return value and skip current formatting section
+1


source







All Articles