By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,683 Members | 1,597 Online
Bytes IT Community
Submit an Article
Got Smarts?
Share your bits of IT knowledge by writing an article on Bytes.

How to detect HTML Tags in DataColumn

P: 17
This code will help for the beginners, who want to learn, how to detect a HTML Tags inside a gridview.

Before getting to the code I will give a small introduction how to work with gridview with HTMl Tags.
  1. Add a gridview controls to the ASPX Page.
  2. Add AutoGenerateColumns="false" in agridview controls
  3. Add a columns inside a gridview.
  4. Add a Bound field inside a columns.
  5. Add a HtmlEncode="false" to the bound column where you are using a HTML Tags.
  6. Provide a datasource to a GridView.
  7. Bind a GridView

Description:

Below is a sample code.

In ASPX Page
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
  2.             <Columns>
  3.                 <asp:BoundField  DataField="Column1" HeaderText="Column1" />
  4.                 <asp:BoundField HtmlEncode="false" DataField="Column2" HeaderText="Column2" />
  5.             </Columns>
  6.         </asp:GridView>
IN ASPX.CS File
Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.  
  4.             DataTable dtTable = CreateDataTable();
  5.             GridView1.DataSource = dtTable;
  6.             GridView1.DataBind();
  7.  
  8.         }
  9.     DataTable CreateDataTable()
  10.         {
  11.             DataTable dtNew = new DataTable();
  12.             dtNew.Columns.Add("Column1");
  13.             dtNew.Columns.Add("Column2");
  14.             DataRow dr = dtNew.NewRow();
  15.             dr["Column1"] = "Test";
  16.             dr["Column2"] = "<p>My Address Line1</p><p>My Address Line2</p>";
  17.             dtNew.Rows.Add(dr);
  18.             return dtNew;
  19.         }
  20. Summary
  21.  
There are many topics to learn with ASP.Net. I have covered a small portion hope this will helps all the beginners to startup. Please give your feedback and suggestion.
Mar 21 '11 #1
Share this Article
Share on Google+
1 Comment


P: 3
In may page, i am assigning DataTable to a GridView as follows:

Expand|Select|Wrap|Line Numbers
  1. GridView1.DataSource = GetDataTable();
  2. GridView1.DataBind();
Now,the definition of GetDataTable is as follows:

Expand|Select|Wrap|Line Numbers
  1. DataTable abcd = new DataTable();
  2. DataRow dr;  
  3. abcd.Columns.Add(new DataColumn("Column1"));
  4. abcd.Columns.Add(new DataCoumn("Column2"));  
  5. dr=abcd.NewRow();  
  6. dr["Column1"]="My Name";
  7. dr["Column2"]="<p>My Address Line1</p><p>My Address Line2</p>";  
  8. abcd.AddRow(dr);  
  9. return(abcd);
  10.  
dont bother about code syntax or functions, I just dont have actual code right now.Its on the similar lines...

My question is, when i see GridView, It prints <p></p> in Column2 as it is.. it doesnt consider those tags as HTML tags...

I want DataColumn to consider those tags as HTML, so that my address gets printed on two different lines but in same column.

If you guys have any other way to work around... just the thing is that I must assign DataTable to GridView.
Mar 2 '13 #2