• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2388
  • Last Modified:

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#
0
mmalik15
Asked:
mmalik15
  • 6
  • 6
  • 2
  • +1
1 Solution
 
informaniacCommented:
Set the HTMLEncode = "false" property and it will work
0
 
mmalik15Author Commented:
i have autogenrated columns=true. Html Encode property of which control ???
0
 
David RobitailleAnalyst ProgrammerCommented:
it the gridview`s BoundField/coulum; you cannot use autogenrated columns=true
0
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
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
 
informaniacCommented:
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
 
prairiedogCommented:
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

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now