Reputation: 157
I have a windows forms application with a form that has a webbrowser control on it. In the html body of the webbrowser control there is a button I am trying to invoke the click event html tag
<input name="ctl00$MainContentPlaceHolder$ctl00$FilterButton" class="MPPBtn" id="ctl00_MainContentPlaceHolder_ctl00_FilterButton"
onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContentPlaceHolder$ctl00$FilterButton", "", true, "", "", false, true))' type="button" value="Filter">
I already got the code to get the button and invoke the click event
HtmlElement filterBtn = webBrowser1.Document.GetElementById("ctl00_MainContentPlaceHolder_ctl00_FilterButton");
filterBtn.InvokeMember("Click");
My issue is that when invoking the click event thru code it doesn't behave the same way as it would if I clicked in a regular browser like chrome. I am thinking that might be because of the javascript:WebForm_DoPostbackoption
onclick='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$MainContentPlaceHolder$ctl00$FilterButton", "", true, "", "", false, true))'
I have already looked around and saw that I might have to invoke javascript:webform_dopostback but that doesn't really make sense bec I would think that once you get the button and invoke the click event then that would be taken care for you? But I tried adding invoking the script as so with no avail
webBrowser1.Document.InvokeScript("WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('ctl00$MainContentPlaceHolder$ctl00$FilterButton', '', true, '', '', false, true))");
Can someone explain what I am suppose to do or how to handle the WebForm_DoPostBackWithOptions?
Upvotes: 1
Views: 471
Reputation: 157
I added these line of code
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
element.text = "function Filter() { javascript: WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('id of element', '', true, '', 'url', false, true)) }";
head.AppendChild(scriptEl);
webBrowser1.Document.InvokeScript("Filter");
Upvotes: 1