call javascript from code behind

M

Mike

Ok, here is my story. I took over a web site that is using the asp.net wizard. This wizard has buttons within it, now, on one of the steps in the wizard I have a drop down. Depending if 'Delete' is selected in the drop down, I need to show a message box to ask the user 'you sure you want to delete?" if they click 'OK' it deletes, if they click cancel it does nothing.

So within the button click event, how can I show an confirmation box only if 'Delete' is selected in the drop down?

so I need something like this

if (dd.selecteditem.text == "delete")
{
I need to show a confirmation box;
}

then call my delete method only if 'OK' is clicked in the confirmation box, if cancel then nothing happens.

any suggestions on how this can be done?
 
D

David R. Longnecker

You could do something like this:

if (dd.selecteditem.text == "delete")
{

ClientScript.RegisterClientScriptBlock(this.GetType(), "DeletePopup", "<script>
confirm('Are you sure you want to delete?') </script>");

}

That'll pop up a JavaScript box with OK and Cancel as options and the text
can be programatically changed to include row information, etc.

HTH.

-dl
 
C

Collin Chung

Ok, here is my story. I took over a web site that is using the asp.net wizard. This wizard has buttons within it, now, on one of the steps in the wizard I have a drop down. Depending if 'Delete' is selected in the drop down, I need to show a message box to ask the user 'you sure you want to delete?" if they click 'OK' it deletes, if they click cancel it does nothing.

So within the button click event, how can I show an confirmation box only if 'Delete' is selected in the drop down?

so I need something like this

if (dd.selecteditem.text == "delete")
{
I need to show a confirmation box;

}

then call my delete method only if 'OK' is clicked in the confirmation box, if cancel then nothing happens.

any suggestions on how this can be done?



Hi Mike,

You'd want to use the OnClientClick property of the button, which
calls a javascript function..

<asp:Button ID="Button1" runat="server" OnClientClick="return
MyFunction()" Text="My Button" />

and in your javascript

function MyFunction() {
if (dd.selecteditem.text != "delete") return true;
return confirm("you sure you want to delete?");
}

Some docs with examples here:
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top