Hi guys,
First of all im trying to debug an application which has been already developed by someone else.
I have a form which has a few fields. there is a save button next to it which saves the data (this already works correctly)

The issue is that if I edit data in the fields and click somewhere else on the page then it allows the user to navigate without asking to save the data. ( Basically I want a pop up message saying 'do u want to save data ? ' with OK and Cancel button and when clicked on OK it first saves and navigates

var isDirty = false;

function setDirty(changeVal)
{
isDirty=changeVal;
}

window.onbeforeunload = checkExit;

function checkExit()
{
if(isDirty == true)
{
return confirmExit();

}
}
function confirmExit()
{
var answer = confirm("do you want to save record?");
if (answer){
__doPostBack('RememberTreeNodeState()','OnClientCl ick');
}

}

AND THEN USE THIS AS :

private TextBox AddTextBox(string strID, object strValue, bool bolDisabled)
{
TextBox txtBox = null;

Control ctlCurrentCtl = tblTree.FindControl(strID);
if (ctlCurrentCtl != null)
{
txtBox = (TextBox)ctlCurrentCtl;
}
else
{
txtBox = new TextBox();
}

try
{
txtBox.Text = Convert.ToString(strValue);
}
catch { }
txtBox.ID = strID;
txtBox.Width = Unit.Pixel(CONST_VALUE_FIELDWIDTH - 10);
txtBox.ReadOnly = bolDisabled;

if (this.Parent.FindControl("lblSaveMsg") != null)
{
//txtBox.Attributes.Add("onchange", "document.getElementById('" + this.Parent.FindControl("lblSaveMsg").ClientID + "').innerText = 'Note: You have unsaved changes';");
txtBox.Attributes.Add("onchange", "setDirty(true);"); 
}

If you have a <form> tag wrapping the form could you add an onblur attribute which calls the checkExit() function when the form loses focus?

Not tested but something like...

form1.Attributes.Add("onblur", "return checkExit();");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.