ASP Button OnClick Confirm Based On Other Variable?

Posted on 2012-04-10
Last Modified: 2012-04-28
I use the javascript:return on typical Gridview buttons. I have not used it on a common page button. I need to open a java Confirm window only if a flag is set like so:

private void checkBox1_OnCheckChanged(object sender, EventArgs e)
        Session["isChanges"] == true;

private void btnSearch(object sender, EventArgs e)
        if(Session["isChanges"] == true)
             Session["isChanges"] = false; //reset flag
            //popup java confirm window

Question by:pointeman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 20

Accepted Solution

BuggyCoder earned 300 total points
ID: 37829198
you can register client script block from code behind like this:-

// Form the script that is to be registered at client side.
       String scriptString = "<script language=JavaScript> function DoClick() {";
       scriptString += "alert('Are you sure you want to close me??');}<";
       scriptString += "/";
       scriptString += "script>";

              this.RegisterClientScriptBlock("clientScript", scriptString);

Open in new window


Author Comment

ID: 37829808
Where do I place your code in my code example?
LVL 20

Expert Comment

ID: 37829827
if(Session["isChanges"] == true)
             Session["isChanges"] = false; //reset flag
            //place the code here....

Author Comment

ID: 37832385
I cannot get your code example to work dymanicaly. I'm now using button attributes in the Page_Load().

    btnSearch.Attributes.Add("onclick", "return confirm('Are you sure you want to proceed?')");

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RLDC Reporting in Visual studio 11 44
Easy filter aspnet 2 35
Can Angular be used with classic 3 32
Header Font Size in Grid View 6 26
Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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