0

I have a button on my aspx page. I want to use javascript confirm before continuing execution when clicking on that button. I can do it easily if i am writing javascript in aspx page itself . But my problem is each time the confirm message may be different. I need to check various condition to generate appropriate confirm message.

Can I call confirm in my code behind, so that I can construct confirm message from there?

What I'm trying is:

 protected void Button1_Click(object sender, EventArgs e)
 {
        //just the algorithm given here
       string message=constructMessage(); \\ its just a function to construct the confirm message
       if(confirm(message)) // i know i cant use  javascript in code behind direct. How can i do this
          {
             //do something
          }
          else
          {
             // do nothing
          }
 }
2

2 Answers 2

2
 protected void Button1_Click(object sender, EventArgs e)
 {
           string message= 
           "if(confirm("+message+")) 
              {
                  //do something
              }
              else
              {
                 // do nothing
           }";
           this.ClientScriptManager.RegisterStartupScript(typeof(this.Page), "warning",         message, true);
                 //Prints out your client script with <script> tags

  }    

For further reference on ClientScriptManager

Sign up to request clarification or add additional context in comments.

1 Comment

@Null Pointer : You could mark an answer if something has helped you find your solution.
1

I just got this link which describes different ways of calling javascript

http://www.codedigest.com/Articles/ASPNET/314_Multiple_Ways_to_Call_Javascript_Function_from_CodeBehind_in_ASPNet.aspx

may be this will help..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.