How to use JQuery in header.master page

2019-09-07 01:49发布

I have a header.master ASP page and I am trying to handle a button click.

My MasterPage.master has the following codes:

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>

In Default.aspx page I have the following code:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TextBox ID="TextBox1" runat="server"/>
<asp:Button ID="Button2" text="Search" runat="server"/>
    </div>
        <script>
            $(document).ready(function () {
                //write your code here
                var txt = $('input[id$=TextBox2]').val();
                $("input[id$=Button2]").click(function () {
                    alert("Hello world!");
                });
            });
</script>
    </form>
</body>
</html>

Everytime I click the button I get the following error: 0x800a1391 - JavaScript runtime error: '$' is undefined

My HTML source code has this:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>

</title>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>
    <form method="post" action="Default.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="7dUu7W1yzgwgCERTGi4lbDb7UGbWC6oigS9K1dTKxNLxOKUQzVp9hkyhbKT/e5Ve22nU2D7eaALUFm2bnAuOroUPGHMRph/IDyBFloPmn0I=" />
</div>

<div class="aspNetHidden">

    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="QCYR7lPBzDC4MzimaH8vIZhF02sSXZE7Cf62VB3M7wEBPRqcglslWJOpXkEEhd9Uw5st2qukgt5t0k0g4Wht3U04fUZzVjfVER5iHbnrs9HEnjwqYebT1BCrLysKW9hhIPI4IIcy1JzrwgDvl0Yvpw==" />
</div>
    <div>
    <input name="TextBox1" type="text" id="TextBox1" />
<input type="submit" name="Button2" value="Search" id="Button2" />
    </div>
        <script>
            $(document).ready(function () {
                //write your code here
                var txt = $('input[id$=TextBox2]').val();
                $("input[id$=Button2]").click(function () {
                    alert("Hello world!");
                });
            });
</script>
    </form>
</body>
</html>

1条回答
够拽才男人
2楼-- · 2019-09-07 02:39

you can not use the asp.net control id using # in javascript.

so try this code to use asp.net control id in javascript as below.

    $("#<%=Button2.ClientID %>").click(function() 
    {
        alert("Hello World.");
    });

i hope it will help you.

查看更多
登录 后发表回答