如何在aspx.cs页面中调用javascript函数? [英] how to cal javascript function in aspx.cs page?

查看:72
本文介绍了如何在aspx.cs页面中调用javascript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的javascript代码如..





My javascript code like..


<script type="text/javascript">
      function Confirm() {
          var confirm_value = document.createElement("INPUT");
          confirm_value.type = "hidden";
          confirm_value.name = "confirm_value";
          document.forms[0].appendChild(confirm_value);
          if (confirm("Do you want procede?")) {
              confirm_value.value = "Yes";
          } else {
              confirm_value.value = "No";
          }
          document.forms[0].appendChild(confirm_value);
      }



  </script>







string js = "<script type='text/javascript'>Confirm();</script>";
                    string confirmValue = Request.Form["confirm_value"];
                    if (confirmValue == "Yes")
                    {
                        ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "insert", "alert('Saved successful');", true);
                    }
                    else
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked Cancel!')", true);
                    }







如果执行.Cs页面,则调用javascript函数Javascript功能确认是或否按钮..但它没有显示警告信息...




if .Cs page is executed then it call the javascript function..Javascript function with Confirmation Yes or no buttons..But it's not showing the alert message...

推荐答案

试试这个



Try this

<form id="frm" runat="server">
   <asp:Button ID="btnOk" runat="server" Text="ok" onclick="btnOk_Click" />
   <asp:Button ID="btnCancel" runat="server" Text="Cancel"

       onclick="btnCancel_Click" />
   </form>







protected void btnOk_Click(object sender, EventArgs e)
       {
           this.Page.ClientScript.RegisterStartupScript(this.GetType(), "ok", "alert('You clicked ok!')", true);
       }

       protected void btnCancel_Click(object sender, EventArgs e)
       {
           this.Page.ClientScript.RegisterStartupScript(this.GetType(), "cancel", "alert('You clicked cancel!')", true);
       }





注意:



this.Page.ClientScript.RegisterStartupScript - > 对于非Ajax更新面板

ScriptManager.RegisterClientScriptBlock - > 对于Ajax更新面板



Note:

this.Page.ClientScript.RegisterStartupScript --> For Non Ajax Update Panel
ScriptManager.RegisterClientScriptBlock --> For Ajax Update Panel


这篇关于如何在aspx.cs页面中调用javascript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆