Solved

Javascript confirm window in C# code behind

Posted on 2012-04-11
6
608 Views
Last Modified: 2012-04-11
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.
0
Comment
Question by:zintech
  • 4
  • 2
6 Comments
 

Author Comment

by:zintech
ID: 37832513
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
        }
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37832732
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

0
 

Author Comment

by:zintech
ID: 37832872
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37832998
The condition below will check for your value and display the confirm box if the value matches with the one that you specify in place of YourSelectedText;
Otherwise it will simply return true and page will post pack;
if($('#ddlList option:selected').text() == "YourSelectedText")
{
return confirm("Are you Sure?");
}
else
{
return true;
}

Open in new window

0
 

Author Comment

by:zintech
ID: 37833905
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
0
 

Author Comment

by:zintech
ID: 37834013
I got it working.  Thank you
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now