views:

1382

answers:

4

Currently I am 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 inserting html code, why this is not being converted to html?

The output result is all text. (Suppose an icon is being displayed with no redirect url)

I dont know why.

Thanks

+1  A: 

If you are using a gridview, you might want to use as it was intended.

You should have a content template.

If you need to do formatting based on values, you do it in the rowdatabound event.

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

The intent of gridview is to do the formatting in the html section of the page... There is lots of fancy formatting you can do there.

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

QUICK FIX:

I guess it could take a little time to learn how to do it the right way. In the interim, if you want a quick fix to your problem with the least amount of change:

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

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

First, add RowDataBound event to your GridView.

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

Second, add the code for the event handler using your logic. RowDataBound event will fire for every row, we don't have to use foreach. I'm 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 might want to 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.

Salamander2007
A: 

To get the GridView to output HTML, all you need to do is set the HtmlEncode parameter to false on the desired bound field.

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

Thank you, I have just intented to change my design b/c I think that no way to do like it. And now I come back to my original grid

CongPhuoc