Solved

Reading pages HTML into Codebehind using VB.NET

Posted on 2004-10-19
17
1,116 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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,…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

16 Experts available now in Live!

Get 1:1 Help Now