Solved

ASP Button OnClick Confirm Based On Other Variable?

Posted on 2012-04-10
4
170 Views
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
        }

        SaveChanges();
}
0
Comment
Question by:pointeman
  • 2
  • 2
4 Comments
 
LVL 20

Accepted Solution

by:
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>";

           if(!this.IsClientScriptBlockRegistered("clientScript"))
              this.RegisterClientScriptBlock("clientScript", scriptString);

Open in new window

0
 

Author Comment

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

Expert Comment

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

Author Comment

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

if(...)
    btnSearch.Attributes.Add("onclick", "return confirm('Are you sure you want to proceed?')");
else
    btnSearch.Attributes.Remove("onclick");
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

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 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

12 Experts available now in Live!

Get 1:1 Help Now