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

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

programmeradmin7浏览0评论
<%--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.

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

Share Improve this question asked Oct 10, 2013 at 6:19 sifeeesifeee 131 gold badge1 silver badge7 bronze badges 3
  • 1 You know you get points for marking a correct answer ;) – Brian Ogden Commented Oct 10, 2013 at 7:56
  • Though there are scenarios that you need to build javascript server side,those scenarios are plex requirements and plex conditions. My answer and others here explained to you how to do what you wanted.I defended my answer under the basic pretense that you might be doing more advanced JS and your question merely uses something simple for the sake of clarity.After seeing the ment you just made below,and that you couldn't even fix the syntax of the selected answer code on your own,I know your a novice.Knowing that I can now say,why would you inject this Javascript server side? – Brian Ogden Commented Oct 11, 2013 at 6:38
  • It isn't right,you don't understand what you are doing.You should be keeping this <script> exactly where it is, on the aspx page or in a js file that you reference. ASP.NET is actually a beautiful framework, faster to code in than Ruby on Rails and more solid than PHP.But it is people like you who gives us a bad name because you don't understand the basics of web application coding and ASP.NET happens to have crazy nice tools that you can just slap stuff together if you hammer long enough. Try starting in PHP so that you cannot do something like you are currently trying to do. – Brian Ogden Commented Oct 11, 2013 at 6:43
Add a ment  | 

5 Answers 5

Reset to default 11
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());
    }
}

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);

You can use ClientScriptManager class and its methods, for example RegisterClientScriptBlock. Depends on when you want the javascript to execute.

See details here:

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

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

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

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

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

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

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

发布评论

评论列表(0)

  1. 暂无评论