最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript confirm from code behind C# - Stack Overflow

programmeradmin9浏览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
          }
 }

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
          }
 }
Share Improve this question edited Feb 1, 2011 at 6:05 Michael Petrotta 61k27 gold badges152 silver badges181 bronze badges asked Feb 1, 2011 at 6:02 Null PointerNull Pointer 9,30926 gold badges75 silver badges119 bronze badges 2
  • done .thank you for notifying that :) – Null Pointer Commented Feb 1, 2011 at 6:05
  • possible duplicate of call js confirm from code behind – Randy Levy Commented Feb 1, 2011 at 6:35
Add a ment  | 

2 Answers 2

Reset to default 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

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

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

may be this will help..

发布评论

评论列表(0)

  1. 暂无评论