锄头在按钮点击中调用asp.net c#中的javascript [英] hoe to call javascript in asp.net c# in button click

查看:71
本文介绍了锄头在按钮点击中调用asp.net c#中的javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这里我在.aspx页面完成了一些脚本,我在pageload中调用了javascript。但如果我在页面加载中调用一些按钮事件没有正确启动就会出现错误,例如未捕获的ReferenceError:Showalert未定义



.aspx页面: -

hi to all,
here i done some script in .aspx page here i call that javascript in pageload. but if i call in page load some button event not fire correctly got error like that "Uncaught ReferenceError: Showalert is not defined"

.aspx page:-

<asp:Content ID="Content2" ContentPlaceHolderID="cplhControlPanel" runat="Server">



    <script language="javascript" type="text/javascript">


        window.onload = function Showalert() {

            var txt = document.getElementById("<%= txtSearch.ClientID %>");
            var btn = document.getElementById("<%= BtnClearFilter.ClientID %>");
            if (txt.value == "") {
                // alert(txt.value);
                btn.style.visibility = "hidden";
                // when the window is loaded, hide the button if the textbox is empty
            }

        }

        function EnableDisableButton(sender, target) {
            var first = document.getElementById('<%=txtSearch.ClientID %>');

            if (sender.value.length >= 1 && first.value.length >= 1) {
                document.getElementById('<%=BtnClearFilter.ClientID %>').style.visibility = "visible";

            }

            if (sender.value.length < 1 && first.value.length < 1) {



                document.getElementById('<%=BtnClearFilter.ClientID %>').style.visibility = "Hidden";


            }
        }

        }





button clcik: -

< asp:Button ID =BtnClearFilterrunat =serveronkeyup =EnableDisableButton(this,'BtnClearFilter')OnClick =BtnClearFilter_ClickEnableTheming =falseText =CssClass =ClearFilter6/>





.cs页面: -



button clcik:-
<asp:Button ID="BtnClearFilter" runat="server" onkeyup="EnableDisableButton(this,'BtnClearFilter')" OnClick="BtnClearFilter_Click" EnableTheming="false" Text="" CssClass="ClearFilter6" />


.cs page:-

protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "displayalertmessage", "Showalert();", true);
}
}







以上脚本我想在按钮clcik中调用事件,意思是onclickevent。




above script i want to call in button clcik event that means onclickevent.

推荐答案

1。你已经把它连接到window.onload事件,所以在Page Load中调用它会调用它两次。

2.当你按照你的方式连接window.onload事件时,你不要不要命名这个功能。请参阅 http://www.w3schools.com/jsref/event_onload.asp [ ^ ]

所以,改为

1. You already have it wired up to the window.onload event so calling it in Page Load would be calling it twice.
2. When you wire up the window.onload event the way you have, you don't name the function. See http://www.w3schools.com/jsref/event_onload.asp[^]
So, change to
window.onload = function() { ...



然后从Page_Load中删除调用。


and then remove the call from Page_Load.


这篇关于锄头在按钮点击中调用asp.net c#中的javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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