Null Pointer
Null Pointer

Reputation: 9289

JavaScript confirm from code behind C#

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
          }
 }

Upvotes: 0

Views: 17237

Answers (2)

Robin Maben
Robin Maben

Reputation: 23044

 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

Upvotes: 2

programmer
programmer

Reputation: 918

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..

Upvotes: 1

Related Questions