how to render html inside gridview cells

In my gridview cells i dont want to show html rather dispaly the output.e.g.
<a Href=AccountantHoursDetail.aspx?EmployeeID=24556&Subj=Preparing and Maintaining Accounting Records>Test</a>
I want to make Test  a hyperlink inside gridview but it dispaly plain html.
Any ideas
I m using C#
mmalik15Asked:
Who is Participating?
 
prairiedogConnect With a Mentor Commented:
Two ways to do this:
1. Use GridView's RowDataBound event handler to contruct those hyper links instead of in DataTable.
string url = "<a href='www.mysite.com'>Site</a>";
e.Row.Cells[i].Text = url; //<===Change i to the cell containing URL
2. If you need to construct those URL's in DataTable, then in the RowDataBound, for each URL cell, you will need to call HtmlDecode to make it a clickable URL:

e.Row.Cells[i].Text = HttpUtility.HtmlDecode(e.Row.Cells[i].Text);
Change "i" to the actual index of the URL cell. If you have more than one cell containing URL, then you need to use the above line for each cell.
0
 
Obadiah ChristopherCommented:
Set the HTMLEncode = "false" property and it will work
0
 
mmalik15Author Commented:
i have autogenrated columns=true. Html Encode property of which control ???
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
David RobitailleAnalyst ProgrammerCommented:
it the gridview`s BoundField/coulum; you cannot use autogenrated columns=true
0
 
mmalik15Author Commented:
I m adding html link tag in the datatable and then binding it to gridview & in the gridview autogeneratedcolumns=true. But i want gridview to show the links
Thanks
0
 
David RobitailleAnalyst ProgrammerCommented:
does the link is the content (data) from a database column column???
bacause you could add a HyperLinkField has a coulum (EmployeeID is the column name), you dont have to manipulate the datatable.

<asp:HyperLinkField DataNavigateUrlFields="EmployeeID"
DataNavigateUrlFormatString="AccountantHoursDetail.aspx?EmployeeID={0} &Subj=Preparing and Maintaining Accounting"
Text="test" />
0
 
Obadiah ChristopherCommented:
in rowdatabound of gv

replace < with < and > with >
0
 
mmalik15Author Commented:
no i have to dynamically create the URL after i get the results from my database. Here is code

 string Value,EmpID,Subj,str;
            int row,column;
            for(row=0;row<dt.Rows.Count;row++)
            {
                for(column=2;column<dt.Columns.Count-1;column++)

                {
                 
                    Value=Convert.ToString(dt.Rows[row][column]);
                    EmpID=Convert.ToString(dt.Rows[row][0]);
                    Subj=Convert.ToString(dt.Columns[column].ColumnName);

                    str = "<a Href=AccountantHoursDetail.aspx?EmployeeID=" + EmpID.ToString() + "&Subj=" + Subj.ToString() + ">" + Value.ToString() + "</a>";
                                                       
                    dt.Columns[column].ReadOnly = false;
                   
                    dt.Rows[row][column] = str.ToString();

after this i bind my datatable to the gridview.
0
 
David RobitailleAnalyst ProgrammerCommented:
informaniac, i think you should use the code snippet...
infomaniac, is solution is if your data is html formated, mine (the HyperLinkField ) is if you just what to add a link to each row the grid view (and keep autogeneratedcolumns=true)
0
 
David RobitailleAnalyst ProgrammerCommented:
0
 
mmalik15Author Commented:
Thanks but
I can not add hyperlink field because i have to dynamically create the parameters and then over write my existing datatable and then i bind this datatable to the gridview
0
 
mmalik15Author Commented:
informaniac can you please give me an example
0
 
David RobitailleAnalyst ProgrammerCommented:
well, now i see exacly what you whant ton do...
i think you will find that one interesting. you will be able to use the replace function to replace the HTML code for < and > using this.
http://www.codeproject.com/KB/webforms/FrmtAutoGenClmnASPNETGrid.aspx
0
 
mmalik15Author Commented:
Do you mean this replacement inside cell

 cell.Text.Replace("<", "&lt");
 cell.Text.Replace(">", "&gt");

but it does not seem to work either.
0
 
David RobitailleAnalyst ProgrammerCommented:
humm, i thoug that would work.
What if you loop the cell s controls??? ther may also the / char to replace..
Cell.Controls
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.