Setting a serverside session object in javascript in asp.net - javascript

I searched SO and google, but did not find anything that would resolve my issue.
I have a popup window where the user can click "Save", "Delete" or "Close". Each of these three functions will eventually close the popup window. On the parent window codebehind, I need to know which of the 3 buttons the user clicked.
Currently, on the popup window (if the user clicks Delete), I have (pre-existing code) :
function CloseDelete() {
var agree = confirm("Are you sure you want to delete this record?");
if (agree)
__doPostBack("<%=btnDelete.UniqueID %>", "");
// create session object here //
else
return false;
GetRadWindow().Close();
}
I was thinking of creating a session object in javascript on the popup window, which I could check on the parent window codebehind. How would I create a session object in the function above ? Or how can I determine which button the user clicked?

Related

How do I trigger a JavaScript confirm box without using a click event?

I'm working on an application that handles complaints. The user can't "close" the complaint (i.e. mark it as completed) until a batch of conditions are met. Previously, once every condition had been met, a "Close the Complaint" button would appear, but I've been asked instead to generate a confirmation window (asking, "Are you ready to close this complaint?") that would pop up as the user saves the last necessary item.
No problem, I figured. I set up JavaScript to generate the confirm window, and added attributes to the save buttons (when all of the other conditions for closure have been met) on any of the items that might be the final item necessary for closure. Except...
Once they click to save the record, they at least want to do that, whether they're ready to close the complaint or not. But currently, if they confirm "Yes," the record is saved and the complaint is closed, while if they confirm "No," then the complaint isn't closed, but neither is the record saved.
I'm working in vb.net, using Visual Studio 2008, and what I'd like to find is a way to trigger the confirm window after the record is saved (in the ItemInserted sub for the DetailsView). That way, it could get the confirmation and close or not, but the record would be saved either way.
Every bit of advice I can find uses button clicks to generate JavaScript confirm windows; does anybody know another way to do it?
EDIT (adding a bit more background):
The way I originally approached it was to make two identical save buttons. One is the ordinary button that saves the record ("ibInsert"), and the other ("ibInsertAndClose") saves, then closes the record. When the DetailsView databinds in Insert mode, I check the "ready for closure" status, then set the visibility of the buttons.
If ReadyToClose() Then
Dim ibInsertAndClose As ImageButton = CType(dvResponseDetail.FindControl("ibInsertAndClose"), ImageButton)
Dim ibInsert As ImageButton = CType(dvResponseDetail.FindControl("ibInsert"), ImageButton)
If Not ibInsert Is Nothing AndAlso Not ibInsertAndClose Is Nothing Then
ibInsert.Visible = False
ibInsertAndClose.Visible = True
ibInsertAndClose.Attributes.Add("onclick", "javascript: return confirm(""\nAre you ready to close this Complaint?"")")
End If
End If
Sounds like on click event you should save the record regardless then, once the save operation is complete, use a callback function to display the confirm dialog. Here's an example using setTimeout() instead of an actual save operation.
var closureConditionsMet = true;
document.getElementById('closureRecord').addEventListener('click', function() {
//the user clicked the record, so let's save it
simulateSaveRecord(function() {
if (closureConditionsMet) {
confirm('Are you ready to close this complaint?') ? console.log('After save user clicked "OK"') : console.log('After save user clicked "Cancel"');
} else {
console.log('Record saved but cloure conditions have not been met');
}
});
});
function simulateSaveRecord(callback) {
console.log('Waiting a few seconds for \'save\'');
setTimeout(function() {
callback();
}, 3000);
}
<div id="closureRecord">Pretend this is the closure record
</div>
If you are able to get the return from your JavaScript confirm() and you are able to save the record, it sounds like you have everything you need but you just need some guidance on the workflow?
Here's some pseudo-code (since I don't do .NET)
function saveRecord() 'saves the record
'database code here
end function
function closeComplaint() 'closes the complaint
'database code here
end function
function saveButtonClick() 'call this when the save button is clicked
saveRecord() 'fires no matter what the response is
dim alertConfirmedTrue = ??? 'response from JavaScript confirm() function
if alertConfirmedTrue
then closeComplaint() 'only called if response is "ok"
end function

How to get data from JS popup window when window is closing

I'm trying to capture when a form field from a popup window exists when the window is closed. I currently do not have control over the child window, but it is on the same domain as the parent window.
I've looked into using the timer functionality:
var child = window.open(...);
var timer = setInterval(checkChild, 500);
function checkChild() {
if (child.closed) {
<DO SOMETHING HERE>
clearInterval(timer);
}
}
but that won't work for me because I don't think that'll give me access to the child's data.
My popup window has a few form fields for uploading a user's profile image. My question is how would I access whether the user SUBMITTED the form or just simply closed the window? There is javascript in the popup window for the submit button that will handle actually uploading the image, but I would need to somehow capture when they click the "upload" button and be able to see that the user actually had an image to upload submitted.
Thanks in advance!
EDIT:
I went ahead and just looked into the closed window event. I couldn't actually get any data from there, but I was able to read further from the DB on the server-side code to get the data I needed.
You can use the child.onbeforeunload event to get information from the child window before it closes.
As suggested by https://stackoverflow.com/a/15769556/1600851

How to use JavaScript variable from popup window in code behind?

I have ASP.NET web application that contains master page. Master page contains content page. Content page contains user control. User control contains Telerik grid with its context menu.
I'd like to click the item in grid's context menu and open new popup modal window. In that window there's drop down list. I pick up some option from drop down list and click OK. I'd like to get the value selected from drop down list and use it in my ASP.NET code to proceed further.
I've tried to use hidden field to store the value from drop down list but it doesn't work because I'm not sure where hidden field should be placed.
This is my code:
Open popup window:
function ClientItemClicked(sender, eventArgs)
{
if (eventArgs.get_item().get_value() == "excel")
{
var retVal = window.showModalDialog("ExportToExcelChoice.aspx", null, "dialogWidth: 400; dialogHeight: 200; center: yes; resizable: no;");
}
}
Click "OK" to close popup window:
function ReturnValue() {
var choice = document.getElementById("DropDownList1").value;
if ((window.opener != null) && (!window.opener.closed)) {
window.opener.document.getElementById("HiddenField1").value = choice;
}
window.close();
}
It fails on this line:
window.opener.document.getElementById("HiddenField1").value = choice;
Because hidden field is placed in user control and the code can't get the reference to hidden field.
Could someone help me to make it work?
If you're using window.open(), you can see into the parent window via the property window.opener, which will let you communicate between your parent page and the popup.
If you're using window.showModalDialog(), see the second answer to this question: window.opener alternatives
Try this
window.opener.document.getElementById('<%= HiddenField1.ClientID %>').value = choice;

How to determine when a user is leaving a webpage (excluding certain links)

The situation: I have a Grails webpage with two tables. One table displays a persons information (including certain flags), and the second table has a list of flags with an "add button" that allows the user to add a given flag to themselves.
Now, there is a save button that, when clicked, pushes the current "state" of the users flags to our database. So I want to be able to display a prompt if there is unsaved information being displayed when a user tries to navigate to another part of the site. This is easy enough by using an existing isDirty boolean that each flag stores. I can just loop through the persons active flags and check if it is dirty or not. If the person contains at least 1 dirty flag, I need to display a prompt if they try to leave, because that data won't be saved unless they explicitly hit the button.
The problem: There are many ways to navigate away from this page. I am using
<body onbeforeunload="checkForDirtyFlags();">, where checkForDirtyFlags() is a basic js function to check for any dirty flags. But here's the thing - when a user adds or removes a flag, that causes a page reload because the way the page is setup is to redirect to a url like this:
"http://my.url/addFlag/123456"
The controller then knows to add the flag with id 123456 to the current person. This does NOT change where the person is in the website however, because the same page is still rendered (it just contains updated tables). So basically, when I see a URL with addFlag or removeFlag, I do not want to prompt the user if they are sure they want to navigate away from the page, because in the eyes of the user they are not leaving the page.
The question: Is there any way to determine what the target is during an onbeforeunload? So that I can have something like this in my javascript:
function checkForDirtyFlag() {
if( justAdding ) { //We are just adding a flag. No prompt necessary
//Don't do anything
}
else if( justRemoving ) { //We are just removing a flag. No prompt necessary
//Don't do anything
}
else { // In this case, we want to prompt them to save before leaving
alert('You have unsaved data on the page. Leaving now will lose that data. Are you sure you want to leave?');
}
}
If any of this isn't clear, please let me know and I'll try and clear it up.
Thanks!
I don't think you can get the target location in unload event. What I'd do is bind the save/submit button to a function that disables the unload event if the button is pressed, therefore disabling the prompt. If the user tries to leave by pressing back etc, the unload event would fire.
Why don't you push the changes immediately to the database, without them having to press the Save Button, or store them in a temporary database so that they do not lose their unsaved changes when the navigate to a different part of the site.
I'm not quite sure if I get you right - but you actually wrote the solution already down there. Why don't you just return a string-message from within an onbeforeunload when necessary ?
For instance:
window.onbeforeunload = function() {
if( justAdding ) { //We are just adding a flag. No prompt necessary
//Don't do anything
}
else if( justRemoving ) { //We are just removing a flag. No prompt necessary
//Don't do anything
}
else { // In this case, we want to prompt them to save before leaving
return 'You have unsaved data on the page. Leaving now will lose that data. Are you sure you want to leave?';
}
};
If you return a string value from that event, the browser will take care of a modal dialog window which shows up. Otherwise nothing happens.

Intercept selection in window.onbeforeunload dialog

in my web application if the user leaves the current page without having saved changes in the form a pop up window is opened to alert him about that.
For the pop up I use some scripts code injected from codebehind (C#):
var Confirm = true;
window.onbeforeunload = confirmClose;
function confirmClose()
{
if (!Confirm) return;
if(/*CHECK CHANGE CONDITION IS TRUE*/)
{ return " + WARN_message + "; }
}
I would need to intercept whether the user click on cancel or ok button.
I tried like:
var button_pressed = window.onbeforeunload = confirmClose;
But it returns always true.
How can get which button was pressed?
Thanks
Not possible. There is no event associated with the buttons.
What you might be able to do was to see if the user came back by setting a value or perhaps a cookie in the page in the onbeforeunload and test if it is there after some time has passed
but see the duplicate Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?

Categories

Resources