Simulating a button click within ascx page - c#

I need to simulate a button click on my ascx page. The user firstly clicks on a button on the service home page which links to a certain service and a new window opens for that related service. If they are not logged in it directs them to the login page. A querystring is sent with it to keep note of what service they had originally clicked. When the user then logs in they are redirected back to the services page and the querystring of what they clicked on before is sent also.
I have up to this point working fine. The problem is that when I'm redirected back to the online services page I need to simulate an onclick event which will open the new window. I cant click on an the onclick method for the button as there is none, everything is done dynamically. Any ideas?

I would have thought the easiest way to achieve what it sounds like you're trying to do is in the code behind for your Online Services Page check to see whether the query string contains the url for the service they clicked on before you sent them to the login page, if it does then add some javascript to run on start up which launches the pop-up (using the ClientScriptManager.RegisterStartupScript method: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.registerstartupscript.aspx).
If you absolutely have to fire the click event you can also do that from your javascript by finding the button you want to click by it's ClientId (using getElementById) and then calling the javascript click() method on it. Here is an example: http://www.w3schools.com/jsref/met_html_click.asp

Related

Why does the "Back" button on the browser trigger the last activated event?

I have 2 webpage, webpage A and webpage B.
What webpage A does
Webpage A have 2 event, Page_Load event and button_click event.
When Page_Load event is triggered it will check if user is logged in or not, if user is logged in it will redirect user to webpage B directly, if not nothing will happen.
When the button on webpage A is clicked, the user stats will become logged in and redirect to webpage B.
What webpage B does
Webpage B have Page_Load event only.
When Page_Load event is triggered it will check if the user is logged in or not, if the user is logged in it will stay at the webpage, and if it is not it will redirect back to webpage A.
The problem I am facing right now is, if I load webpage A in not logged in stats, click the button proceed to webpage B and click the browser's back button on the top left of the browser, it will proceed to webpage A and trigger the button_click event (which is the last triggered event on webpage A) instead of the Page_Load event which the user is in logged in stats but is not redirected to webpage B.
PS. Webpage A's Page_Load event is not triggered at all when user press the back button.
PSS. Both my event is written in C#(asp.net).
What is the cause of this and how do I fix this? Thanks.
UPDATE
I found out that this problem only occurs when my webpage A's button_click event occur once but doesn't redirect (try to login but login failed/denied). Then on the second click it worked, proceed to webpage B, right now if click back the fail condition will be shown and Page_Load event not triggered. I feel like this is something to do with cache or Ram? Is there anyway to let my webpage "forget" it failed once and goes back to webpage A as nothing happened before?

WebElement.click() causes page refresh

I have a <button> element. I call the click() function on this element using chrome webdriver.
appDone = driver.FindElement(By.Id(Constants.APP_DONE));
appDone.Click();
A strange thing happens in chrome, the page actually get reloaded. A pop up comes
"Are you sure you want to leave the page? You have unsaved data" Leave/Stay (buttons)
But same click event works fine in other browsers (Edge, IE, Edge). I opened the console in Chrome before clicking the button and placed debugger. Then executed the button click, the console window goes out a new console window comes up.
Anyone knows why this happens?
Things I have tried so far:
executed the button click using IJavaScriptExecutor
There is an beforeunload event in page, so when I pass onbeforeunload = null, I don't see the page refresh pop up anymore, but page still refreshes.
Tried to add type = button in the button, still no luck.
This screen works fine when run manually without Automation.
I have few logs recorded in server side, so when a page refresh happens, those logs are actually getting deleted, which should not happen.

Browser back button does not work with asp controls

I have a site with few asp:CheckBoxList and asp:DropDownList controls. Checking few options or selecting few items in the dropdowns makes the back button browse to same page just with previous selections. I would like the back button on the browser take the user to the previous website visited. As example: if the last visited site was: default.aspx, then the back button from my current site should go back to default.aspx.
Adding few header options regarding caching only broke the back button to: Page Not Found message.
How can the user browse back to the default.aspx by the first back button clicked on the browser?
Just need to confirm, are u using IE 10 to run the web system?
I had encounter asp control component not working properly when i was using IE 10.
Maybe consider using other browser type like firefox and safari to see if it can work properly

How to distinguish between a user clicking a link and the page doing an automatic redirect?

Having a C# WebBrowser control inside my WinForms application, and being aware of the Navigating event, I never came up with a nice and elegant solution to the following:
If a user actively navigates to another URL, I want to allow it.
If the page redirects "on its own" to another URL, I want to cancel it.
For case 1 there are some cases I can think of:
User clicks an a tag and the href attribute is evaluated to load another URL.
User clicks on an element with an onclick javascript event handler which calls a function that uses window.location to load another URL.
For case 2 I can imagine of:
The loaded page contains an iframe tag that loads an URL inside the IFrame. This fires the Navigating event.
There is some JavaScript timer that is started on page load and when it fires, it uses window.location to load another URL.
The loaded page contains a meta refresh header tag to load another URL after some seconds.
So my question is:
How to detect inside the Navigating event (or any other mechanism) whether a redirect is triggered explicitly by the user or implicitly by the page?
Some more information
The WebBrowser is being used inside a windows based CMS backend application.
I therefore have full control over the content loaded inside the WebBrowser control.
Meaning that I can manipulate the complete HTML string before being sent to the browser, if required.
If it is more applicable, I also would love to get JavaScript-only solutions which I could inject into the HTML being loaded.
(Please note that I do believe this is not a duplicate of this SO posting)
My take on this is capture user clicks on the web browser control. Have it set a flag that indicates that the user clicked on the web browser. If the flag is true, then allow redirection, if it isn't true don't allow it. Make sure to reset the flag after n number of seconds if no (or after) redirection is made.
It seems you are trying to achieve anti-ads/popup/redirect pattern.
From web browser perspective.. clicking <a href="some.url"> is not different from javascript window.location = "some.url"; or 302 redirect response. there are no explicit signals, no such convenience methods.
The WebBrowser control is just a proxy to IE component. You can't intercept browser's engine or even disable/enable javascript as it's part of internet security option.
You have to create special logic to prevent every possible cases of redirection.
eg.
verify HTML string then restrict some javascript pattern, header or iframe with Regex.Replace before render.
var scriptEx = new Regex("<script (.*?)</script>");
var iframeEx = new Regex("<iframe (.*?)</iframe>");
or intercept Navigating URL and cancel unsafe url, etc.

how to notify webserver when user closes the browser (in asp.net)?

I am developing a relay chat application , divided into 2 panes.
right pane - > The Chat responses of users (this uses a ASP.NET Multiline Label control placed inside the update panel , so when any user types the responses and submits it is added to this control)
left panes -> the list of users currently online(this uses ASP.NET list control which is also placed inside the update panel).
below this is the textbox for the user to enter text and a send button to post his response.
everything works fine. But when user closes the browser window instead of clicking the log out button. the list on the left pane is not getting refreshed.
It happens properly , when the user logs out.
IS there any way to knock of the users name from the list if the user closes the browser?(even before his session is expired on the server side).?
sorry i couldn expose the screen shot.
can any one suggest an idea along with a sample code snippet.?
thanks
vijay
You can do it the other way around: ping the server from the browser with an ajax call periodically. If no ping received, remove the user.
You can use Javascript to detect when the browser has been closed, and then kick-off an AJAX request back to the server notifying that the user left.

Categories

Resources