单击按钮时从代码隐藏调用 Javascript 函数

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

Call Javascript function from codebehind on button click

javascriptasp.net

提问by Hymaney

I want to execute a javascript function on a button click and if the result is a sucess only then the server side code should be executed on that button click.

我想在单击按钮时执行 javascript 函数,如果结果是成功,则应在单击该按钮时执行服务器端代码。

Below is what I am trying to do

以下是我正在尝试做的

 protected void btnAdd_Click(object sender, EventArgs e)
    {
         if (btnAddItem.Text == "Add")
            {
                string script = string.Format(@"validate()"); 

                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true); 

                if (text1.text== null)
                {
                    Addtogrid();
                                        }

and below is my javascript function

下面是我的javascript函数

function validate() {
    var arrTextBox = document.getElementsByTagName("input");
           var retVal = 1;
    for (i = 0; i < arrTextBox.length; i++) {
        if (arrTextBox[i].type == "text" && arrTextBox[i].value == "") {
            retVal = 0;

        }
    }

    if (retVal == 0) {
        alert("Validation Failed");
        return false;
    }
    else {
        alert("Validation Success");
        return true;
    }

}

I am not able to understand how to capture of the result of the javascript function . I am trying this for the first time. Kindly help me.

我无法理解如何捕获 javascript 函数的结果。我是第一次尝试这个。请帮助我。

回答by Ronnie

I think the simplest way is to set up the validation function to be called on button click in your markup. So your markup should look like the following..

我认为最简单的方法是设置要在标记中单击按钮时调用的验证函数。所以你的标记应该如下所示..

<asp:Button runat="server" ID="btnAdd" Text="My Button" OnClientClick="return validate()" OnClick="btnAdd_Click" />

Note the OnClientClick attribute. You can use that attribute to call a javascript function that executes before the server side code.

请注意 OnClientClick 属性。您可以使用该属性来调用在服务器端代码之前执行的 javascript 函数。

回答by Kanishka

The following way u can call javascript function after server side operation

服务器端操作后,您可以通过以下方式调用javascript函数

 <script language="JavaScript" type="text/javascript">
       function validate(msg) {
    alert(msg);
   }
</script>
   <asp:Button ID="btnValidateUSR" runat="server" Text="Validate USR" CssClass="butn"  OnClick="btnValidateUSR_Click"/>

    protected void btnValidateUSR_Click(object sender, EventArgs e)
    {
        ClientScriptManager cs = Page.ClientScript;
            cs.RegisterStartupScript(this.GetType(), "ValError", "validate('"  +Resources.ErrorDescription.BalancingErrorMsg+ "');", true);

     }