WebBrowser 控件更新隐藏字段
我正在尝试更改 WebBrowser 控件中隐藏字段的值。
我可以注入显示警报框的 JavaScript。我可以使警报框显示隐藏字段的当前值。但是,我无法更改隐藏字段的值。
我尝试通过这样做来更改值(tb 是 WebBrowser 控件):
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");
在上面,警报确实弹出。
我已经尝试过:
tb.Document.Body.InnerHtml = tb.Document.Body.InnerHtml.Replace("MyValue", "YourValue");
在上面我看到 InnerHtml 在调试时确实发生了变化。
但是,当页面加载完成并且我查看源代码时,该值永远不会改变。
另外,尽管我可以注入 javascript 并且它会弹出警报,但我永远无法在代码中找到 javascript。
这是怎么回事?我做错了什么?
谢谢!
更新:
我在 DocumentCompleted 事件中执行此操作。
我没有使用 ViewState
另一个更新:
我添加了另一个字段。这次是非隐藏文本字段。
<input type="hidden" id="TestHiddenField" value="MyValue" name="TestHiddenField" />
<input type="text" id="TestField" value="MyValue" name="TestField" />
当我这样做时,会发生以下情况:
tb.Document.Body.InnerHtml = tb.Document.Body.InnerHtml.Replace("MyValue", "YourValue");
当页面在 WebBrowser 控件中呈现时,文本框显示文本“YourValue”,但当我查看源代码时,该值仍然等于“MyValue”。
这是怎么回事?我需要它等于“YourValue”。
有什么想法吗?
再次感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在页面加载完成之前,请勿更改 DOM。您必须等待 DocumentCompleted 事件触发。
Do not alter the DOM until after the page is done loading. You have to wait for the DocumentCompleted event to fire.
由于这是服务器端代码,因此您应该使用 ASP.NET 控件并只需更改该值。如果我要猜测出了什么问题,它很可能涉及以下两件事之一:
如果您想查看发生情况的位置,请考虑添加所有页面事件并设置断点并观察控件的值。您很快就会发现事情发生的地方。
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:
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.