JavaScript 从 C# 后面的代码中确认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4859465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:08:18  来源:igfitidea点击:

JavaScript confirm from code behind C#

c#javascriptasp.net

提问by Null Pointer

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.

我的 aspx 页面上有一个按钮。单击该按钮时,我想在继续执行之前使用 javascript 确认。如果我在 aspx 页面本身中编写 javascript,我可以轻松完成。但我的问题是每次确认消息都可能不同。我需要检查各种条件以生成适当的确认消息。

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

回答by Robin Maben

 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

有关ClientScriptManager 的进一步参考

回答by programmer

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

我刚刚得到了这个链接,它描述了调用 javascript 的不同方式

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

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

may be this will help..

可能这会有所帮助..