Q:

WebBrowser Control update hidden field

I am trying to change the value on a hidden field in a WebBrowser control.
I can inject Javascript that displays an alert box.  I can make the alert box show the current value of the hidden field. However, I cannot get the value of the hidden field to change.  
I have tried changing the value by doing this (tb is the WebBrowser control):    
HtmlElement head = tb.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = tb.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function DoIt() { document.getElementById('TestHiddenField').value='Hello World'; alert('Updated'); }";
head.AppendChild(scriptEl);
tb.Document.InvokeScript("DoIt");

In the above the alert does pop up.
And I have tried this:
tb.Document.Body.InnerHtml = tb.Document.Body.InnerHtml.Replace("MyValue", "YourValue");

In the above I see that the InnerHtml does change when debugging.
However, when the page is done loading and I view source the value is never changed.
Also, Even though I can inject javascript to and it pops up the alert, I am never able to find the javascript int the code.
What's going on?  What am I doing wrong?
Thanks!
UPDATES:

I'm doing this in the DocumentCompleted event.
I'm not using ViewState

ANOTHER UPDATE:
I added another field.  This time a non-hidden text field.
<input type="hidden" id="TestHiddenField" value="MyValue" name="TestHiddenField" />
<input type="text" id="TestField" value="MyValue" name="TestField" />

Here is what happens when I do this:
tb.Document.Body.InnerHtml = tb.Document.Body.InnerHtml.Replace("MyValue", "YourValue");

When the page renders in the WebBrowser Control the text box displays the text "YourValue" but when I View Source the value still equals "MyValue".
What's up with that?  I need for it to equal "YourValue".
Any ideas?
Thanks again!

A:

However, when the page is done loading and I view source the value is never changed.

Do not alter the DOM until after the page is done loading.  You have to wait for the DocumentCompleted event to fire.

