Cancel Requests on link clicked asp.net - c#

I have a home page on a website (ASP.NET 4.0 C#) which loads a lot of data every time it is accessed (basically a dashboard). This can (depending on the user) take a while to load, so I broke each section into update panels and load them separately so that the user can see it loading and not just think it the page has frozen. However this presents new issues because if the user does not want to wait and clicks on the navigation menu (or a link) to a different page, they have to wait until the page has fully loaded before it moves on.
Is there anyway to stop page requests loading the data if another link is clicked and just deal with that link?
Further info - I am using a master page where the navigation menu is located and also ajax update panels on the home page.

function cancelPostBack() {
var prm = Sys.WebForms.PageRequestManager.getInstance();
if (prm.get_isInAsyncPostBack()) {
prm.abortPostBack();
}
}
Attach this function to the onclick event of the navigation item to ensure any pending requests are cancelled before navigation continues.

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.

MVC - Catching a navigation request on the site controller before the LayOut controller

At my company we are discussing wether or not to got and make our next web app in MVC.
I have been charged to figure out some basic stuff which could stop us dead in our tracks, and so have spend some time figureing out the MVC platform as best i could.
There is however one thing im wondering, is it possible, and if so how - to have a menu made on the _LayOut page/controller, which when a user presses a menu navigation i am able to catch on the specfic page before it goes to the layout controller?
UPDATED I forgot to mention that i want to be able to save a form depending on what site i am on. So i may have 10 pages with different forms and depending on which one of these i am on, i have to save the form on that page using the same link in my menu.
My explaning might be abit off so ill quickly describe the scenario and maybe there is another way of doing this.
The user is filling out alot of data on the page, they then press the navigation menu to go to a new page, i want to save the entered data before navigating to the next page for them.
Sorry for my bad english it is not my primary language.
Thanks in advance for any help.
A way to do what you're looking for is to attach a client-side event handler which submits the data before navigating to a new page. It would look something like this:
$(".navigation a").click(function (event) {
// Get form data, process it and POST/PUT/DELETE
});
If you're supporting modern browsers then you can subscribe to the input event of your form and attach yourself on the before unload if anything in your form has changed since the window was loaded as suggested in one of the comments. If you need to support older browsers as well subscribe to the change event of the input fields in the form in order to attach the handler for beforeunload.
form.oninput = function () {
window.onbeforeunload = submitFormData;
};
function submitFormData() {
// Gather and submit your data
}

Strange basket session state issues

We're having a bizarre issue with our checkout process. Lets say that: -
User adds a few products to their basket
Clicks to view basket and then removes these products Then uses the browsers <- back button to
navigate to a previous product page
Then they add another product to
the basket, this causes the layout of the entire site to crumble,
it's as if none of the code to load the menus or product listings is
being fired off e.g.
I think it may be related to us using AJAX on the basket so i tried the suggestion posted here adding a hidden form element to the page which actually resolved the issue in chrome but in firefox the page gets stuck in an infinite loop,
Has anyone encountered similar issues with user navigating back to a previous state? I'm completely lost as to what to try next
Thanks for any help
Well i managed to fix this. The issue was that when the user navigated back via history to a previous product page, it was being treated as a postback and so none of the code to populate the controls on the page was being fired as it was inside a !Page.IsPostBack block...
Since I couldn't find anything better to use to determine when the page was broken I simply read in the navigation tabs and check if any of them are empty which is an indication of the page not loading right and then we refresh e.g.
<script type="text/javascript">
$(function() {
$("#tabs-nav ul li").each(function() {
if (!$(this).children().text() || /^\s*$/.test($(this).children().text())) {
location.reload();
}
});
});
</script>
It's not pretty but it does the job
Thanks

Wait till the GeckoFX Webbrowser has loaded

I want to automate a few tasks on my website with GeckoFX for some testing.
That should happen when I click a button and everything should be automated after that button click.
This includes clicking buttons where the page refreshes so the code has to wait till the page has loaded and that's where my problem is.
If I do it like that:
geckoWebBrowser1.Navigate("http://mywebsite.com");
GeckoInputElement searchText = new GeckoInputElement(geckoWebBrowser1.Document.GetElementsByName("searchbox")[0].DomObject);
searchText.Value = "GeckoFx";
I get an error, so how can I put it that the code after .Navigate waits till the webbrowser has fully loaded the page?
You can use DocumentCompleted Method to perform your automatic operations.
private void geckoWebBrowser1_DocumentCompleted(object sender,EventArgs e)
{
// Here you can add the coding to perform after document loaded
}
For example : First initiate the browser to google page by geckoWebBrowser1.Navigate("https://www.google.com");
After google page loaded you can do the following in document_completed method:
GeckoInputElement search =new GeckoInputElement(geckoWebBrowser2.Document.GetElementsByName("q")[0].DomObject);
GeckoInputElement button = new GeckoInputElement(geckoWebBrowser2.Document.GetElementsByName("btnG")[0].DomObject);
search.focus();
search.Value = "Master Blaster Sachin";
button.Click();
so it will search the value you given automatically after the google page loaded. Like that you can modify the program as per your logic. Hope it helps..
I would go an use a product like Selenium http://seleniumhq.org/. It's free open source web testing which is scriptable.

Want to cause a postback after closing a Shadowbox

I have a site which uses Shadowbox-JS to bring up a settings page when a user clicks on a little icon. The settings are there for the user to be able to customise their view of what they're looking at in the main application.
The settings page is a full (but compact) .aspx page which has all the relevant code for the settings to be applied, updated to a database, read from a database etc, so that's all nicely dynamic. What I want is for a postback to occur once the user closes the shadowbox so that the view on the main page automatically updates to reflect their changes.
The following link appears once the user presses 'save' in the settings area:
<a onclick="window.parent.location.href = window.parent.location.href;">Settings saved. Please close this window</a>
This basically just refreshes the whole page and of course, in the process the shadowbox is no more. This approach works fine but the problem is the user also has the option to close the shadowbox by clicking outside of it. I need to capture when this happens and cause a postback (or page refresh) when this happens so that no matter what the settings are always applied when the shadowbox is closed.
I've found the answer, I needed to add an option to the Shadowbox.init() function as follows:
Shadowbox.init({
onClose: function () {
window.location.href = window.location.href;
}
});

Categories

Resources