ASP.Net:RegisterOnSubmitStatement没有调用指定的javascript函数

时间:2022-04-09 15:58:57

Upon failing validation the BackgroundSize of these TextBoxes sets from 0px to 20px. I got this technique from here.

验证失败后,这些TextBox的BackgroundSize设置为0px到20px。我从这里得到了这个技术。

Problem: The code in click event of the button doesn't run the specified javascript.

问题:按钮的单击事件中的代码不运行指定的javascript。

code-behind:

public static _Default d = new _Default();
public static ValidatorCollection signupValidators = d.GetValidators("signupGroup");
protected void btnSignup_Click(object sender, EventArgs e)
{  /***** i put a breakpoint here ******/
   Page.ClientScript.RegisterOnSubmitStatement(GetType(), "currentValidator", "onUpdateValidators();");
}

Javascript:

function onUpdateValidators() {
    alert("Alert! In Javascript");
    for (var i = 0; i < signupValidators.length; i++) {
        var currentValidator = signupValidators[i];
        var control = document.getElementById(currentValidator.ControlToValidate);
        if (control != null && control.style != null) {
            if (!currentValidator.isvalid) {
                control.style.backgroundSize = "20px";
                alert("in onUpdateValidators()");
            }
        }
    }
}

First time ever doing this so no idea what's wrong =(

第一次做这个,所以不知道什么是错的=(

[EDIT:] Could there be something wrong with my Button?

[编辑:]我的按钮可能有问题吗?

<asp:Button ID="btnSignup" runat="server"
                Style="width: 150px; height: 40px; border-radius: 2px; font-weight: bold; background-color: #499234; margin-left: 70px; margin-top: 10px"
                Text="Create New Account"
                CausesValidation="true"
                ValidationGroup="signupGroup"
                PostBackUrl="~/Page2.aspx" <%-- removing this doesn't work either --%>
                OnClick="btnSignup_Click" />

Because i put a breakpoint on its click event and the execution doesn't break there

因为我在其click事件上放置了一个断点,并且执行不会中断

1 个解决方案

#1


0  

For Page.ClientScript.RegisterOnSubmitStatement check this link

对于Page.ClientScript.RegisterOnSubmitStatement,请检查此链接

Else try this:

否则试试这个:

First Add AjaxControlToolKit reference and than from AJAX Extenions in Toolbox add ScriptManager in your page after <form runat="server">

首先添加AjaxControlToolKit引用,然后在工具箱中添加AJAX Extenions,在

之后在页面中添加ScriptManager>

<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        //your other logic

    </form>
 </body>

And than in code behind use this:

而在代码背后使用这个:

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

#1


0  

For Page.ClientScript.RegisterOnSubmitStatement check this link

对于Page.ClientScript.RegisterOnSubmitStatement,请检查此链接

Else try this:

否则试试这个:

First Add AjaxControlToolKit reference and than from AJAX Extenions in Toolbox add ScriptManager in your page after <form runat="server">

首先添加AjaxControlToolKit引用,然后在工具箱中添加AJAX Extenions,在

之后在页面中添加ScriptManager>

<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        //your other logic

    </form>
 </body>

And than in code behind use this:

而在代码背后使用这个:

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