如何在后面的 C# 代码中编写此 Javascript 编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19288428/
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-08-10 14:38:59  来源:igfitidea点击:

How to write this Javascript coding in C# code behind?

c#javascriptasp.net

提问by sifeee

<%--Confirmation Box--%>
<script type="text/javascript" language="javascript">
    function alertbox() {

      if (confirm("Are you sure?") == true) 
          {
              document.getElementById('<%= hdnYesNo.ClientID %>').value = "YES";
          }
          else 
          {
              document.getElementById('<%= hdnYesNo.ClientID %>').value = "NO";
          }

        }
    </script>

How to rewrite this code in C# as codebehind? I would like have a confirm box with yes or no buttons.

如何在 C# 中将此代码重写为代码隐藏?我想要一个带有是或否按钮的确认框。

采纳答案by kiran

you can use like this way

你可以这样使用

Page.ClientScript.RegisterStartupScript(this.GetType(), "Confi", "if(confirm('Are you sure?') == true){  document.getElementById('txtValue').value ='YES';}else{document.getElementById('txtValue').value ='NO';}", true);

回答by Maverick

You need javascript for this, it's not possible in code behind. Code behind is run on the server before the page is sent to the user, javascript is run on the user's computer.

您需要为此使用 javascript,这在后面的代码中是不可能的。后台代码在页面发送给用户之前在服务器上运行,javascript 在用户的计算机上运行。

If you want to get access to their answer in code behind (possible and straightforward), you can use ajax or you can postback.

如果您想在后面的代码中访问他们的答案(可能且直接),您可以使用 ajax 或回发。

If you want to have this popup to come up when you press on a .Net asp:button control, then you can put a javascript function in the "OnClientClick" attribute of the control.

如果您希望在按下 .Net asp:button 控件时出现此弹出窗口,则可以在控件的“OnClientClick”属性中放置一个 javascript 函数。

EDIT: If you need help with any of the above, let us know and help will be provided :).

编辑:如果您需要上述任何方面的帮助,请告诉我们,我们将提供帮助:)。

EDIT2: Due to the discussion below, I guess I should clarify: You can (obviously) construct javascript on the server side before passing it to the client, but the example you gave is NOT a case where you should be doing that (an example of where this might be a good idea would be a script that has variables read from a database or something similar that doesn't need to be dynamic between page loads).

EDIT2:由于下面的讨论,我想我应该澄清一下:您可以(显然)在将 javascript 传递给客户端之前在服务器端构造 javascript,但是您给出的示例不是您应该这样做的情况(示例这可能是一个好主意的地方是一个脚本,它具有从数据库读取的变量或类似的东西,不需要在页面加载之间动态)。

回答by Robert

You can use ClientScriptManagerclass and its methods, for example RegisterClientScriptBlock. Depends on whenyou want the javascript to execute.

例如,您可以使用ClientScriptManager类及其方法RegisterClientScriptBlock。取决于您希望 javascript何时执行。

See details here:

在此处查看详细信息:

http://msdn.microsoft.com/en-us/library/System.Web.UI.ClientScriptManager_methods.aspx

http://msdn.microsoft.com/en-us/library/System.Web.UI.ClientScriptManager_methods.aspx

回答by Brian Ogden

protected void Page_Load(object sender, System.EventArgs e)
{    
    string csName = "PopupScript";
    Type csType = this.GetType();
    ClientScriptManager csm = Page.ClientScript;

    if (!csm.IsStartupScriptRegistered(csType, csName)) {
        StringBuilder sb = new StringBuilder();
        sb.Append("<script>");
        sb.Append("function alertbox() {");
        sb.Append("if (confirm('Are you sure?') == true) ");
        sb.Append("{");
            sb.Append("document.getElementById('" + hdnYesNo.ClientID + "').value = 'YES';");
        sb.Append("}");
        sb.Append("else");
        sb.Append("{");
        sb.Append("document.getElementById('" + hdnYesNo.ClientID + "').value = 'NO';");
        sb.Append("}");
        sb.Append("</script>");


        csm.RegisterStartupScript(csType, csName, sb.ToString());
    }
}

回答by Giuseppe B

another option is to create the script in the /View folder and user razor for generating the script.

另一种选择是在 /View 文件夹和用户 razor 中创建脚本以生成脚本。

then you could point to the page in the tag like

然后你可以指向标签中的页面,如

<script src="~/ScriptGenerator/MyScript" />

for pointing the controller ScriptGeneratorController that expose the action MyScript

用于指向暴露动作 MyScript 的控制器 ScriptGeneratorController