Solved

Javascript confirm window in C# code behind

Posted on 2012-04-11
6
607 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I got it working.  Thank you
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

771 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

14 Experts available now in Live!

Get 1:1 Help Now