I have a master page and a content page in my web page(.net 2008).In the master page i am having some menus.Now after doing some operation in the page if i will click to any other menu then it should ask the conformation by javascript.On cancle it should close the window but in case of ok button click it should save the changes.(I mean want to call the save method in that ok button click)
So can u plz guide me how can i get the event of the javascript conformation ok button click in the c# file.(code behind).Reply me as soon as possible ....................
Thanks
Gitanjali
The JavaScript "confirm" box returns true if the user selects "ok" and false if the user clicks "cancel". If you set your button's JavaScript "onclick" event to return the value returned by the "confirm" box you can prevent a postback from happening if they click "cancel".
Eg:
-
myButton.Attributes.Add("onclick","javascript: return confirm('Do you wish to save your changes?');)
-
This will display a JavaScript confirm box asking "Do you wish to save your changes?". If they click cancel they will remain nothing will happen, if the click "ok" a postback will occur.
Alternatively, if you always want a postback to occur you could set the the onclick to a JavaScript function that saves the confirm value in a HiddenField which you can access on the server.
Eg
-
function save(hiddenFieldName)
-
{ if(confirm('Do you wish to save your changes?')==true)
-
{ document.getElementById(hiddenFieldName).value="True";}
-
else
-
{ document.getElementById(hiddenFieldName).value="False";}
-
return true;
-
}
-
-
myButton.Attributes.Add("onclick","javascript: save('"+myAspHiddenField.ClientID+"');")
-
Now in your server side code you can check if you want to save the changes by checking the "myAspHiddenField" value.
-Frinny