Solved

Reading pages HTML into Codebehind using VB.NET

Posted on 2004-10-19
17
1,111 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
Comment Utility
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
Comment Utility
0
 
LVL 6

Author Comment

by:RaisinJ
Comment Utility
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
 
LVL 28

Expert Comment

by:mmarinov
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Expert Comment

by:CJ_S
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>(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
Comment Utility
Please do have a look at my comments ...
0
 
LVL 26

Expert Comment

by:Rejojohny
Comment Utility
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
Comment Utility
i meant "y no split of points for me?"
0
 
LVL 6

Author Comment

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

Thanks,

RaisinJ
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now