1

我使用一种 javascript,当我在没有母版页的页面中使用它时,它可以正常工作,但是当我在使用母版页的内容页面中使用它时,它不起作用我该怎么解决这个问题?

我没有在母版页中使用任何 JavaScript 代码,这是必要的吗?

//Page1 without MasterPage
....
<head runat="server">
    <link rel="stylesheet" href="StyleSheet.css" type="text/css" media="screen"/>
    <script src="jquery-1.4.4.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="jquery.maskedinput.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () 
        {
            $.mask.definitions['~'] = "[+-]";
            $("#date").mask("99/99/9999"); //Date
            $("input").blur(function () 
            {
                $("#info").html("Unmasked value: " + $(this).mask());
            }).dblclick(function () {
                $(this).unmask();
            });
        });
    </script>
</head>
....
<body>
    <form id="form1" runat="server">
    date :  <asp:TextBox ID="date"  runat="server" ></asp:TextBox>
.....

//page2 with MasterPage
...
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
   <link rel="stylesheet" href="StyleSheet.css" type="text/css" media="screen"/>
    <script src="jquery.maskedinput.js" type="text/javascript" charset="utf-8"></script>
    <script src="jquery-1.4.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () 
        {
            $.mask.definitions['~'] = "[+-]";
            $("#date").mask("99/99/9999"); //Date
            $("input").blur(function () 
            {
                $("#info").html("Unmasked value: " + $(this).mask());
            }).dblclick(function () {
                $(this).unmask();
            });
        });
    </script>
</asp:Content>
....
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
     date : <asp:TextBox ID="date"  runat="server" ></asp:TextBox>
</asp:Content>


//Master Page
...
<head runat="server">
    <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>
</html>
4

2 回答 2

1

In asp.net when a UserControl is placed in a container the unique IDs of its contained controls are recalculated to avoid problems with multiple controls of the same type. this is true even for pages inside master pages.

to retrieve the correct unique ID of the controls in your page you need to write something like this

$("#<%= date.ClientID %>").mask("99/99/9999");

the <%= ... %> will be evaluated at runtime and replaced with the correct id (ClientID is a property of asp.net control which holds the unique id of the control to be rendered)

于 2012-10-08T21:45:43.760 回答
0

呈现控件的 ID 与您调用它的 ID 不同。

如果您查看发送到浏览器的 HTML 源代码,您的date控件实际上将被称为ct100_date.

所以找到实际渲染的 ID 并更改$("#date")$("#ct100_date").

例如,如果您查看源代码并在呈现的 HTML 中找到您的文本框控件,它将如下所示:

<input type="text" id="ct100_date" />

Javascript \ jQuery running on the browser will only find the control if you search for the exact id of the control. So take the id of the control in the HTML and seach for it with (for example) $("#ct100_date")

于 2012-10-08T21:17:38.987 回答