Solved

ASP Button OnClick Confirm Based On Other Variable?

Posted on 2012-04-10
4
176 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

765 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