Solved

Javascript confirm window in C# code behind

Posted on 2012-04-11
6
609 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

786 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