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

Reading pages HTML into Codebehind using VB.NET

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
RaisinJ
Asked:
RaisinJ
  • 4
  • 3
  • 3
  • +4
1 Solution
 
mmarinovCommented:
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
 
DotNetLover_BaanCommented:
0
 
RaisinJAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
mmarinovCommented:
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
 
tusharashahCommented:
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
 
RaisinJAuthor Commented:
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
 
mmarinovCommented:
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
 
CJ_SCommented:
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
 
tusharashahCommented:
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
 
AerosSagaCommented:
Dim wc As New System.Net.WebClient()
Dim html As String = Encoding.ASCII.GetString(wc.DownloadData("http://abstractvb.com"))

Aeros
0
 
RejojohnyCommented:
>>(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
 
RejojohnyCommented:
Please do have a look at my comments ...
0
 
RejojohnyCommented:
i think my answer is more appririate  and easier to execute ... u no split of points for me?
0
 
RejojohnyCommented:
i meant "y no split of points for me?"
0
 
RaisinJAuthor Commented:
TheLearnedOne, I forgot to split the points, could you please split these?

Thanks,

RaisinJ
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now