How do you inject a header using the Navigating event in WebBrowser - c#

I'm trying to solve a problem where I'm intercepting a request in Navigating and want to inject an additional request parameter into that request before passing it off.
Here are the steps:
First I make a request via WebBrowser.Navigate(). I have an event handler to capture the Navigating event.
The request hits the Navigating event (before the actual request is made). I, at this point, want to either cancel the request and issue a new request with the added parameters, or somehow alter the current request to have the correct parameter.
Unfortunately I cannot simply call the Cancel event, and re-issue a Navigate call because of one problem. This request is from an iframe, so its one level deep, and the navigate call would clobber the end result of the document contents (as it would show only the iframe contents and not its parent's).
Any suggestions?

It seems impossible to inject a new parameter into a request on low level.
A recommended way is to alter html source of a page sends a request. For instance, add a hidden input field inside a form just after page is loaded. IMHO.

Related

How to get updated page information through C# WebBrowser when updatePanel AJAX event happens?

I am automating a scenario in WPF using WebBrowser. Code snippet is as follows
string id = paginationControlProcessing[1].PageCtrl.id;
webDoc.GetElementById(id).GotFocus += new HtmlElementEventHandler(wb_OnGotFocus_PaginationControls);
webDoc.GetElementById(id).Focus();
webDoc.GetElementById(id).RaiseEvent("onChange");
webDoc.InvokeScript("__doPostBack", new object[] { "ctl00$ContentPlaceHolder1$pgControl$nextPageLink", "" });
When the page changes through pagination control, I am able to hit the event handler for focus change. Problem is happening when pagination controls are hit where I am not able to retrieve the data for 2nd and 3rd page.
I can do the page navigation and it works in the WebBrowser control. Data is also updated in the table to which these ASP.net AJAX controls are attached. In the event handler, when I look into the content of object WebBrowser1, it does not contain the content of new data that appears in the UpdatePanel control. Data for the control when 2 and 3 are clicked comes through UpdatePanel ASP.Net control.
What event can be used to capture the data coming for UpdatePanel control from server?
One way to achieve the task specified above is to use FiddlerCore but one has to be aware of the gotchas related to certificate generation in fiddlercore since ASP Panel controls use HTTPS mostly. All interactions on Panel control of ASP generates partial POST requests that result in partial responses with updatePanel contents. It does not update the page source nor there is any automatic redirection happening. Once a page is loaded, any interactions happening in Panel controls must be handled by application driving the automation and it cannot be done through WebBrowser as it is unaware of any update in the page. Be aware that UpdatePanel responses can also update the __VIEWSTATE and other hidden values. Use fiddler to capture responses and then re create them using custom processing through HttpWebRequest. HttpClient is not suitable for most cases since it needs a separate thread to handle async task and then it can become messy to handle events passing between UI, WebBrowser, Fiddler thread and HttpClient tread.
A good place to get help on usage of FiddlerCore is at the Google group for Fiddler.

ASP.NET Web Forms - How to redirect and pass parameters without problems

I wanted to redirect (by code) to another ASPX page and on that page I wanted to select the appropriate view (in a multi-view and during page load).
My solution was to add a url parameter that was checked on the "OnLoadComplete" event and take appropriate action, but it seems that this url parameter gets copied to all the links on the page. So, the user cannot navigate anywhere else because always this view gets presented.
My second thought was to use a Session variable, but I am afraid that this will be an overkill.
Any ideas/thoughts/suggestions on this matter?
Can I prevent the use of the url parameter in all the page-links?
Is it bad if I use a Session variable for this temporarily?
Is there some other way to do this?

Postbackurl Vs Response.Redirect (losing data)

I have a button in my aspx page, which in code behind the value of postbackurl gets set. I updated the code to add click event handler and added Response.Redirect code to redirect user to the page once the button is clicked. The only reason i ended up adding click event was because i had to add some extra logic when the button was clicked including redirecting to same url, that was used in postbackurl. The page does get redirected on click however it seems like all the hidden fields from the field gets lost once the form get redirected to the url.
Is there anyway i can submit the form without loosing the hidden data.?
Maybe one way you can solve this problem is to use the server side Transfer() call.
http://msdn.microsoft.com/en-us/library/540y83hx(v=vs.85).aspx
In most cases I've seen what you really want to do is pass the information for the new page using URL parameters. Take all the stuff the new page needs and put it into the url (encrypt if it is security sensitive).
If the only issue you are having is when you want to stay on the same page, this is simple, just have the click event handler return without doing a response.redirect or a transfer.
Post the code if this is not working for you.

More detailed explanation about GetPostBackEventReference method

I understand that this will causes a page reload (partial or full, depending on how your UpdatePanels are set up)
But,
where in the code I should put it (client or server side)?
which control should I send to the method? Is it must be inside the UpdatePanel?
does this method work only for controls inside update panels?
must the control have a postback capability?
what is the engine behind this? How does this method work, so I could use it properly.
Thanks.
The function call returns a string of executable JavaScript, which you need to write to the client somewhere in your response.
Typically, you're sending your Page (this/Me) unless you have a control that you specifically want to handle the postback (ie, that implements IPostBackEventHandler)
GetPostBackEventReference is not related to UpdatePanels; if you have one, it will handle the postback.
No (see #2)
This makes a postback to the page. If you want it to raise an event when it posts back, you need to implement IPostBackEventHandler, either on your page or on one of your controls.
http://msdn.microsoft.com/en-us/library/ms153112.aspx

ID for page is always __Page

I am registering a callback that I want to access asynchronously. This callback will be executed for validation before form submit. Problem is, since the form action goes to a different page, making a reference to Page results in a reference to "__Page", which gets evaluated as a reference to the target page, and the asynchronous calls go there instead of the current page. (The callback is registered in a usercontrol on the page instead of the page itself.)
I can change the form action on the client-side after I make the calls, but I'd rather take care of it all in the callback registration. So how can I register the callback to something other than __Page?
tldr: change the action of the form client-side. DoCallback uses the action of the form to make the async request.

Categories

Resources