HtmlElement.OuterHtml.Replace() fails with COM exception

C

cweeks

Dev env: VS 2005 C# Express, .Net Framework 2.0

I am trying to programmatically select an item in a listbox on a web
page. The first thing I do is go through and de-select any selected
items. The OuterHtml field of a selected element in the listbox looks
something like this:

<OPTION value=* selected>Default item</OPTION>

I want to simply remove the the term 'selected' this way:

elt.OuterHtml = elt.OuterHtml.Replace("selected","");

This results in OuterHtml being set to:

<OPTION value=* selected></OPTION>

Notice: 1) the 'selected' term is untouched, 2) the visible text has
been removed.

After the assignment I can see in the debugger that one of the members
of the HtmlElement object (OffsetParent, I think) has a string
indicating some kind of COM Interop services exception.

Is it possible to change the HTML in a web page? Am I going about it
the wrong way? Is there some kind of initialization or setup call I am
supposed to make? Is it just a bug in .NET 2.0?
 
C

cweeks

The MSDN documentation at

http://tinyurl.com/edbln

does contain this warning:

"If you assign a new value to OuterHtml, the current element reference
will become invalid; it will not reflect the name, properties and child
content of the HTML you have just assigned."

but I don't really understand what that means.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top