本文介绍了锄头在按钮点击中调用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.

推荐答案

window.onload = function() { ...



然后从Page_Load中删除调用。


and then remove the call from Page_Load.


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

10-30 22:25