本文介绍了从codebehind调用JavaScript函数的按钮点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想点击一个按钮执行一个JavaScript函数,如果结果是只有服务器端code应该按钮点击执行的sucess。

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.

下面是什么,我试图做

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

和下面是我的javascript函数

and below is my javascript function

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

}

我不能够理解如何捕捉JavaScript函数的结果。我想这是第一次。请帮帮我吧。

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.

推荐答案

我觉得最简单的方法就是建立验证功能在您的标记按钮,​​点击调用。所以,你的代码看起来应该像下面的。

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" />

注意的OnClientClick属性。您可以使用该属性来调用服务器端code之前执行JavaScript函数。

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

这篇关于从codebehind调用JavaScript函数的按钮点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 19:00
查看更多