Ravi666

Hi there,

I have a combo box and button in a aspx page. I want a confirm message to be asked if the user closes the page or if a browser back button press is made. For this i am using onformbeforeunload event. First, the problem i faced was, the confirm message pops up for every post backs due to button click or combo text changed. I set a flag "locked=1" on button client click event and made the message to be prompted only if "lock=0". So, for button click, it got corrected.

Now, the problem is with combo box chaged event (I have postback on the same). I tried setting the flag in onTextChanged event (client-side) of combobox. But, its throwing error like "lock is not a member of ASP.default_aspx". When i implemented it on server-side click (by registering script), the flag is set. But, the confirmation message is getting asked before it moves to server-side code.

Actually, I don't want this to happen. Can somebody suggest a way to set the javascript flag (lock=1) on combo textchanged before it postbacks, so that the confirm message won't be asked.

CODE:

---------

<aspBig SmileropDownList ID="cboUser" runat="server" AutoPostBack="true" OnTextChanged="lock=1;"></aspBig SmileropDownList>

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>&nbsp;<asp:Button ID="cmdOK"

runat="server" onclientclick="lock=1;" Text="Lock" />

// Javascript Function for Confirmation

function exitAlert(e) {

if (lock==0){

var msg = "If you leave this page your information will be lost.";

if (!e) { e = window.event; }

if (e) { e.returnValue = msg;}

return msg;}

}

Any comments would be appreciated.

Thanks,

Ravi.



Re: ASMX Web Services and XML Serialization How to set javascript flag on Combo box changed event before postback

Muhammad Akhtar Shiekh

You can use ComboBox onChange Event to set the flag




Re: ASMX Web Services and XML Serialization How to set javascript flag on Combo box changed event before postback

Ravi666

Great!! It's Working... But, now the problem is: when i click the Back button or Close the Window after combobox change, the message is NOT getting asked. Why is this

This is my JavaScript:

<script language="JavaScript" type="text/javascript">

var lock=0;

// Cross browser event handling for IE 5+, NS6+ and Gecko

function addEvent(elm, evType, fn, useCapture) {

if (elm.addEventListener) {

// Gecko

elm.addEventListener(evType, fn, useCapture); return true; }

else if (elm.attachEvent)

{

// Internet Explorer

var r = elm.attachEvent('on' + evType, fn); return r; }

else {

// Netscape

elm['on' + evType] = fn; }}

// Add Listeners

function addListeners(e) {

// Before unload listener

addEvent(window, 'beforeunload', exitAlert, false); }

// Exit Alert

function exitAlert(e) {

// default warning message

if (lock==0){

var msg = "If you leave this page your information will be lost.";

// set event

if (!e) { e = window.event; }

if (e) { e.returnValue = msg;}

// return warning message

return msg;}

}

addEvent(window, 'load', addListeners, false);

</SCRIPT>





Re: ASMX Web Services and XML Serialization How to set javascript flag on Combo box changed event before postback

Ravi666

Thanks, mate!! It's Done...

I had set the flag in code-behind also. That's y it happened.