1

I really need some help executing this piece of javascript code in a WebBrowser control. On the website, it get's executed like this:

"javascript:setTimeout('__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\')', 0)"

I have tried many things, something like this:

webBrowser1.Navigate("javascript:setTimeout('__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\')', 0)");

But it's not working.

Anybody have any suggestions?

EDIT:

Also tried this:

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
            HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
            IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
            element.text = "function __doPostBack(eventTarget, eventArgument)";
            head.AppendChild(scriptEl);
            webBrowser1.Document.InvokeScript("__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\'");

I get JS exception:

enter image description here

EDIT2:

I also did:

string sa = @"function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}";

element.text = s;

But nothing happens, JS doesn'get get executed.

EDIT3:

Also tried

webBrowser1.Document.InvokeScript(@"__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\'");

But nothing happens. The browser is supposed to refresh. Here is the code from the browser:

<select name="dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995" onchange="javascript:setTimeout('__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995\',\'\')', 0)" id="dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995" class="dropdown" pid="as2ba2d5-d2cc-dad2-9e35-abd345678aac">
                <option value="1 (1)">1</option>
                <option value="2 (2)">2</option>
                <option value="3 (3)">3</option>

            </select>
Omid
  • 823
  • 1
  • 11
  • 31
  • The error is quite clear - you've missed an opening curly bracket! In your line "element.text = "function __doPostBack(eventTarget, eventArgument)";" you've written a function signature but no function body. In fact, I don't think you need that line at all - nirmus showed how to write a function and then call it but the function should already exist (it's the ASP.NET framework code for a postback, right?) so you can probably lose all but the last line of the example code and just do "webBrowser1.Document.InvokeScript("__doPostBack(\'dsl50$ct160$g_818f5662_1255_51ab_d4e1 etc etc etc");" – Jon Jun 21 '13 at 13:49
  • Tried that too, but the JS doesn't get executed :( – Omid Jun 21 '13 at 13:59
  • But there's no error this time? If so, perhaps the javascript was executed but there was no result. What happens if you change the argument of InvokeScript to something simple like "alert('hello');"? – Jon Jun 21 '13 at 14:02
  • the page is supposed to refresh. I am trying to change the value of a checkbox, but it doesn't get triggered unless the JS get's triggered. So I change it using webbrowser.setattribute and used your invokescript, but it doesn't refresh. – Omid Jun 21 '13 at 14:06
  • I just noticed that you've escaped the single quotes in that InvokeScript argument, but there's no need because the string is no longer within single quotes like it was in the original javascript. That's probably killing it. Try webBrowser1.Document.InvokeScript("__doPostBack('dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995',''"); – Jon Jun 21 '13 at 14:06
  • yeah tried that too, still nothing :( – Omid Jun 21 '13 at 14:10
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32165/discussion-between-jon-and-omid) – Jon Jun 21 '13 at 14:11

2 Answers2

0

Check this link:

How to inject Javascript in WebBrowser control?

Code:

    HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
    HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
    IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
    element.text = "function sayHello() { alert('hello') }";
    head.AppendChild(scriptEl);
    webBrowser1.Document.InvokeScript("sayHello");
Community
  • 1
  • 1
nirmus
  • 4,913
  • 9
  • 31
  • 50
0

I imagine you want:

string javascriptDoPostBack = @"function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}";

HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = javascriptDoPostBack;
head.AppendChild(scriptEl);

webBrowser1.Document.InvokeScript("__doPostBack('dsl50$ct160$g_818f5662_1255_51ab_d4e1_6bse0453e306$ba400$rdProcess$ucTopQuestions$qst_1995','');");
Sparko
  • 735
  • 6
  • 15
  • Does the JavaScript not get called at all? If you add an alert/console.log line on the first line of __doPostBack does that output anything? – Sparko Jun 23 '13 at 10:01
  • does get called if I do alert hello but not __dPostBack – Omid Jun 23 '13 at 19:42