Solved

Reading pages HTML into Codebehind using VB.NET

Posted on 2004-10-19
17
1,123 Views
Last Modified: 2010-08-05
Hi,

I have an <TD> element on my aspx page.  I want to read all the HTML between this elements begin and end tag into my VB.NET code behind page so I can manipulate it and then output it back to the source as the new HTML.  How do I do this.  Is there an HTMLReader as there is an HTMLWriter.  

Thanks...
0
Comment
Question by:RaisinJ
  • 4
  • 3
  • 3
  • +4
17 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12347074
Hi RaisinJ,

actually there is no such objects like HTMLReader or writer
what is your approach ? you can redesign your page direct at design time or use dynamically creates controls    

Regards!
B..M
0
 
LVL 18

Expert Comment

by:DotNetLover_Baan
ID: 12347089
0
 
LVL 6

Author Comment

by:RaisinJ
ID: 12347103
mmarinov, what do you mean there is not such object as an HTMLWriter.  I've used it in ASP.NET to render Custom Web Controls.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 28

Expert Comment

by:mmarinov
ID: 12347124
RaisinJ,

i don't know to exist of such object, searched for it in MSDN and didn't find anithing
may be it is some third parti object -

B..M
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 12347144
RaisinJ,

You can make your table RunAsServer Control ( by right clicking the table & selecting RunAsServer)

& then you can access InnerText/InnerHtml like following in your codebehind

Table1.Rows[0].Cells[0].InnerText = "TEST";
Table1.Rows[0].Cells[0].InnerHtml = "TEST";


-tushar
0
 
LVL 6

Author Comment

by:RaisinJ
ID: 12347277
mmarinov, the objects name is HTML32TextWriter....

I'll try and be more specific with my question...  Here is what I have on my page...

The actual ASP.NET control is a datagrid.  I am trying to dynamically change an Image ()"><img src='Images\CheckMark.gif' border='0px'>) contained in my <td> element, based on another field in the Datagrid which is not visible.  I am handling this on the "Item_Created" event of the DataGrid.

Once the Datagrid renders into HTML, the output looks like this...

<td class="Status" id="h0_0"><a href="javascript:__doPostBack('DataGrid$_ctl2$_ctl0','')"><img src='Images\CheckMark.gif' border='0px'></a></td>

(I do not want to handle this with Javascript!)

I want my new output to look like this:

<td class="Status" id="h0_0"><a href="javascript:__doPostBack('DataGrid$_ctl2$_ctl0','')"><img src='Images\Error.gif' border='0px'></a></td>

Note, I would just be changing the CheckMark.gif to the image Error.gif, everthing else would remain the same.  I figured, if I could somehow read the HTML back into VB.NET, I could perform a simple "Replace" statement, and put the HTML back.

0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12347333
RaisinJ,

why don't you use ItemDataBound event of the datagrid, and depends on the value in a cell ( doesn't matter that the column is not visible ) change the imageurl of the image object ?

B..M
0
 
LVL 22

Expert Comment

by:CJ_S
ID: 12347497
Rather easy

Add a runat="server" attribute to the TD element and an ID.
<td runat="server" id="tdMyEl">
...
</td>

In your codebehind you can declare the element like
Protected tdMyEl as System.Web.UI.HtmlControls.HtmlTableCell

Now you can use tdMyEl to manipulate any of the underlying Controls. If you want to manipulate only the resulting html code then you have to override the Render(HtmlTextWriter) procedure.

CJ.
0
 
LVL 18

Accepted Solution

by:
tusharashah earned 500 total points
ID: 12347498
B..M is correct... if you are dealing with DataGrid then what you need here is ItemDataBound event. You'll be able to access everything in DataGrid & will be able to modify it before showing them tom your page!

The way you do it is like following:
-------------------------------------------------------------------------------------------------------------------------------------
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
  switch( e.Item.ItemType )
  {
      case ListItemType.Item:
      case ListItemType.AlternatingItem:
         ( (HyperLink) e.Item.Cells[8].Control[0] ).ImageUrl = "Images\Error.gif";   // Replace 8 with the number of your Image Column
// If you are dealing with Template Column then you need something like following
//                ( (Sytem.Web.UI.WebControls.Image) e.Item.FindControl("Image1") ).ImageUrl = "Images\Error.gif";
  }
}
-------------------------------------------------------------------------------------------------------------------------------------

-tushar
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 12347743
Dim wc As New System.Net.WebClient()
Dim html As String = Encoding.ASCII.GetString(wc.DownloadData("http://abstractvb.com"))

Aeros
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12355447
>>(I do not want to handle this with Javascript!)
any specific reason y u do not want to use it .. u case is best suited for that .. just add an id to u iamge tag and change its src value
<img id=myimage src='Images\CheckMark.gif' border='0px'>

then using javscript ....
document.getelementsbyid("myimage").src = 'Images\Error.gif' ;

if not then as mentioned by some others .. itemdatabound is the event that u should look for ..

also when is it that u want to change the image .. when an error occurs during loading the datagrid??
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12703065
Please do have a look at my comments ...
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12746928
i think my answer is more appririate  and easier to execute ... u no split of points for me?
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12746929
i meant "y no split of points for me?"
0
 
LVL 6

Author Comment

by:RaisinJ
ID: 12765229
TheLearnedOne, I forgot to split the points, could you please split these?

Thanks,

RaisinJ
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question