【问题标题】:How to correct this Confirm box?如何更正此确认框?
【发布时间】:2013-11-18 12:51:30
【问题描述】:

我对确认框使用了 javascript 编码,它在读取该文本框的值时显示“javaScript 运行时错误”...此确认框是否有任何替代选项可以工作...

<script type="text/javascript">        
    function test() {
        var m = document.getElementById('<%=textbox1.ClientID%>').value;

        if (m != "") {
            var cBox = confirm("Are you sure want to add new user?");
            if (cBox == true)
                return true;
            else
                return false;
        }
    }
</script>

我在后面的代码中调用它...

ScriptManager.RegisterStartupScript(Page, this.GetType(), "CallJS", "test();", true);

【问题讨论】:

标签: javascript asp.net


【解决方案1】:

你不能这样做,你试图在绑定页面之前获取一个 clientId。

顺便试试更干净的东西

将此块添加到您的 *.aspx

<asp:Content ID="Content3" ContentPlaceHolderID="JavaScript" Runat="Server">
    <asp:PlaceHolder ID="contentScript" runat="server" />
</asp:Content>

*.aspx.cs

string scriptJS = @"function test() {
                        var m = document.getElementById('" + textbox1.ClientID + "').value;

                        if (m != '') {
                            var cBox = confirm('Are you sure want to add new user?');
                            if (cBox == true)
                                return true;
                            else
                                return false;
                        }
                    }";

HtmlGenericControl script = new HtmlGenericControl("script");
script = new HtmlGenericControl("script");
script.Attributes.Add("type", "text/javascript");
script.InnerHtml = scriptJS;
contentScript.Controls.Add(script);

请使用“string.Format”增加/重构此代码。

【讨论】:

  • 我们应该为此添加什么命名空间..?
  • 如果您使用 C# web.forms 进行编码,请将其放入 Page_Load 方法中以进行测试。在此之前,您可以在函数声明之后编写调用来测试您的函数。无需声明命名空间,使用相同的 aspx.cs 命名空间,将代码放在分部类中。
  • 看到源码的时候能看到页面中写的jsSript吗?你用萤火虫控制台做过测试吗?你调用函数了吗?
猜你喜欢
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多