开发者

WebBrowser Control update hidden field

开发者 https://www.devze.com 2023-04-01 08:47 出处:网络
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. Howe

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? 开发者_如何学JAVAWhat 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!


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.


Since this is server side code, you should use an ASP.NET control and simply change the value. If I was to take a guess what is wrong, it most likely deals with one of two things:

  1. you are setting the value before you set the "initial value" in your code. Since you do not have events listed, I have no clue if this is correct.
  2. You are using viewstate and the viewstate value is overwriting, as you are going through a rather heavy way of changing values.

If you want to see where things are happening, consider adding all page events and set breakpoints and watch the value of the control. You will find, rather quickly, where things are happening.

0

精彩评论

暂无评论...
验证码 换一张
取 消