Link to home
Start Free TrialLog in
Avatar of zintech
zintech

asked on

Javascript confirm window in C# code behind

I realize that a Javascript confirm() box can be accomplished in the following way:

<asp:button ID="AddRecordbutton" runat="server" Text="Add Record"  onclick="AddRecordButton_Click" onclientclick="return confirm('add record?');" />

However, I have a dropdownlist with 4 possible options, and when the user clicks a button I have to perform the action selected in the dropdownlist, only if one of the items in the dropdownlist is selected would I like to display a confirm message, not all of the time.  I did a lot of searching and trial and error but can't seem to figure out how to accomplish this.
Avatar of zintech
zintech

ASKER

I have gotten close to accomplishing what I want.  Here is what I have so far.  It successfully creates the pop up window, but even if the user clicks "Cancel", it still executes the code:

else if (DropDownList4.SelectedIndex == 4)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "DeletePopup", "<script> confirm('Are you sure you want to delete?') </script>");
            //Execute code
        }
Use this function in client click, with JQuery in your client side:-

OnClientClick="return Validate()";

function Validate()
{
if($('#ddlList').val() == "YourValue")
{
return confirm("Are you Sure?");
}
else
{
return true;
}
}

Open in new window

Avatar of zintech

ASKER

I understand this solution you are proposing.  However, I only want the confirm box to show if only one of the four dropdownlist options are selected.  Your proposed solution would display the pop up box every time, when any option is selected
ASKER CERTIFIED SOLUTION
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zintech

ASKER

It appears to me that the method Validate() is not being reached or properly called.  In the else() part of the if statement, I also did a confirm() alert box to make sure the method is being reached and it is not
Avatar of zintech

ASKER

I got it working.  Thank you