Click button server side using JavaScript - javascript

I have a JavaScript timer, on time out I am given an alert, after which I have to call a method.
For that I have taken a temporary button and onClick I call the method like this:
__doPostBack('btn','Click');
But it is going to page load, and not the click event of the button. What is the solution to this?

PostBack is the name given to the process of submitting an ASP.NET page to the server for processing .
Examples -- 1) a login page. After the user has typed in his credentials, he clicks on the ‘Login’ button. OnClick, the page is sent to the server to check against the DB/XML file to check if the user with supplied details is an authenticated user or not.
2) There also arise certain situations wherein, you would want to do a PostBack, say you have 2 combo-boxes, Country and State, based on the value selected in the Country combo-box, the values in the state combo-box will be populated. So in this case, once the value from the Country combo-box is selected, data is "Posted-Back" to the server to retrieve the values to be populated in the State Combo-box.
Also, every time a PostBack is done, the Page-Life cycle is executed once the request comes back to the client. Hence in your case, once you do the postBack, the PageLoad() is called.

Related

How to show a confirmation box inside code behind in the middle of a procedure

I have an issue I wonder if this fine community can help me out.
I have a webform asp.net (using vb.net code behind) web application, on one of my forms has a datagrid. Each record can have a different status and each record can be selected via a checkbox. At the bottom of the grid I have a dropdown of possible actions that users can take, these actions will be against the records selected and a button to invoke the action selected.
Now the issue I have and I am getting rather annoyed at it is that due to the difference in status before I complete the action selected i need to check whether the records selected can do such action.
Example
I select records 1,2,3,4,5. The action I select is to download in a CSV format, however only records which are unlocked can be downloaded, so in my example only records 1,3,5 are unlocked. This means I can only download 1,3,5 and not records 2,4. Once downloaded I want to mark the record that it has been downloaded and refresh the grid.
I have this completed this by finding the records to be downloaded (aka 1,3,5), create a CSV (or XML as that is also one of the actions) as a string, mark the records 1,3,5 as downloaded, refresh the grid and then I invoke a hidden button on the form which will actually download the data.
If Not ViewState("Data") Is Nothing Then
Response.Clear()
Response.ClearHeaders()
Response.Buffer = True
Response.ContentEncoding = Encoding.UTF8
Response.ContentType = IIf(ViewState("ExportType").ToString() = SharedApplication.ExportedType.XML, "application/xml", "text/csv")
Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", "records-" & "." & ViewState("ExportType").ToString()))
Response.Write(ViewState("Data"))
HttpContext.Current.Response.Flush()
HttpContext.Current.Response.SuppressContent = True
HttpContext.Current.ApplicationInstance.CompleteRequest()
ViewState("Data") = Nothing
End If
The above downloads the data, to invoke the download button I use javascript to do a postback on the click event of the hidden button.
However the issue I have is before I download the data I want to add a confirmation box, something to say "Only 3 of the 5 records will be downloaded, do you want to continue". With yes continuing download and No to stop action. How do I go about doing this. I have tried all sorts of techniques but cannot figure it out.
I have tried adding a return confirmation on the hidden button in its onlcientclik event, but this never gets fired. It does work if you actually click on the button but not when I do a postback to its click via JS.
The closet i can find is using the registeronsubmit in the code behind.
ClientScript.RegisterOnSubmitStatement(btnHidden.GetType, "confirm", "return confirm('Are you sure?');")
But this gets fired constantly, even when we I click on another button. I have tried adding another hidden button with the hope I can add the clientclick to show a confirmation box but that does not work as well.
Does anyone have an idea, I have herd of possibly creating a popup to display the messagebox but then how do I capture if my code needs to continue.
Hi I thought I better share what I eventually did to complete my issue incase someone else moves into this territory.
So I went away and thought about this more and the solution I came up and actually works pretty well although some may say this is rather an ugly way of doing this.
I use a dropdown list to mark what action is needed, so I thought why don't we use the postback event on the dropdownlist to build up the message of the confirmation box and then add the JS confirmation box on the OnClientClick event of the button.
btnRun.OnClientClick = "return confirm('" & sMessage & "');"
The message variable is a string which builds up the message depending on which process action is selected.
This means as soon as the dropdownlist is changed to an action, the codes will work out how many selected items will be changed. This is a sql query which picks up which codes/ids will be changed, then the count of data against the total count of selected items will be changed/exported whatever the action selected. This builds the process button confirmation, so when the user clicks on the process button it displays X out of X will be changed, do you want to continue. If cancel is selected nothing happens if ok is selected it carries on and completes it action as per usual before I wanted to show some message box.

Reload partial and retain form values

When purchasing a course, the user can enter 1 or more students to register for the course. By default there is only one entry but the user can use a dropdown to select more and then the form will update to show more.
I am accomplishing this by triggering an event when the user changes the dropdown value that uses ajax to call an action which returns a partial with the appropriate number of entries and then I just replace the existing div with the new one.
My question is whether there is a way to implement this so that it's kind of like "refreshing" the page where the form remembers and automatically refills in he values the user already entered just like if you were to refresh the entire webpage. Is there a way to do this, or will I need to pass in the existing values into the action in my ajax call and have the partial set them?
A secondary question I just thought of (and perhaps this should be in another post but I will go ahead and put it here for now) is whether I should be concerned about any weird behavior with validation when doing it this way? (I'm using stock, built in validation with annotations).

Browser Back button changes dynamic url Parameters

I am working on a website, where url parameter gets updated based on user action, according to which I update the webpage without refreshing.
Consider the scenario of E-commerce where url changes when user clicks on filters and then updated products gets displayed.
Now the problem is, when user clicks on Browsers's back button the browser goes back to previous url-parameter, but page did not gets changed. I want to change the page also based on url parameter that gets changed after back button clicked.
I have tried this solution:
$($window).on('popstate', function (e) {
// Update the page also
});
The problem with this code is, this gets fired as url changes, means it does not care about if browser back button is clicked, or url is changing using the jQuery. So if I am changing url based on user interaction, the popstate callback will be called and my custom function also. To update the page I am using https requests, so http api gets called two times.
Is there any way to check if only "Back button" is clicked?
I would recommend you to change your design a litle bit and trigger all content updates (the product list in your case) by listening to url changes, not only url changes caused by the back button. So instead of triggering any re-rendering on click events, let these buttons be regular link to the url that represent your content and trigger the functionality from the popstate event.
This is how all MVVM-frameworks like Angular.js, Backbone etc are designed and meant to be used.
By doing this it will also be so much easier for you to maintain the application in the long run.
Good luck!
You can do this with sessionStorage! Below is the relevant part of an answer I always refer to for stuff like this https://stackoverflow.com/a/45408832
sessionStorage is a storage type like localStorage but it only saves your data for the current tab.
Session storage can be used like this.
sessionStorage.setItem('key', 'value'); //saves the value
sessionStorage.getItem('key'); //gets the saved value
performance.navigation.type is the browser is the variable that hold users navigation info.
if(performance.navigation.type == 2){
//User is coming with back button
}
So to put it all together, you can set/update a sessionStorage item as part of the callback of the click event for your filter, then performance.navigation.type to check if they used the back button to load the page and apply the data!

Need to redirect after back button pushed. How?

I have a set of three apps/scripts.
The first allows the user to select a value. That value is passed to the second script, which takes the value, reads a database, and produces XML which is then posted to an Eclipse/Java/RAP application immediately, without user intervention using Javascript "onload'.
After the RAP application is loaded, to the user the back button doesn't seem to work. The back button takes the user to the second script, which gets the same values it did the first time and then immediately forwards to the RAP application again.
We want the back button to work as the user expects, i.e. to take the user back to the first script.
Since using the back button submits exactly the same information as it did in the first pass, including the referrer, the only way I can see to do this is to use cookies.
Is that it, or is there a better way?
Thanks,
Sean.
On page B set a cookie
On page A, detect the cookie. If it exists, clear the cookie then redirect.
One issue: Page A doesn't know if you got there by pressing BACK or if you navigated there directly. If you can live with that, it will work.

Check to see if a user has came from a 'forward' page

I am currently working on a form which posts to a web service. The form validation is done at the web service and if a validation error occurs the user is presented with an error and a back button.
The form contains a number of default values which I am auto populating. These values then overwrite any values that the user has inserted when the back button is pressed.
Is there a way I can detect to see if the user has pressed the back button and prevent the auto population?
My guess would be you are filling these post load via JS? Because the value attribute should not override new values. If you are, I would just do a check to see if the field is empty before loading in the text.
Also, I would look into the placeholder attribute.
Pass a parameter as part of the URL that your back button sends the user to, then check to see if that parameter is part of the URL of the current page in your Javascript. If it's present simply don't populate with the default values.

Categories

Resources