Solved

Javascript confirm window in C# code behind

Posted on 2012-04-11
6
610 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

832 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