如何在c# web表单应用程序的内容页中添加脚本

How to add script in content page of c# web form application

本文关键字:脚本 添加 应用程序 web 表单      更新时间:2023-09-26

我需要在asp.net应用程序的内容页添加java script标签。该脚本工作良好的html标签,但在内容它不工作这里是代码。

    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <div class="panel panel-primary" id="form1" runat="server">
        <div class="panel panel-heading">Registration</div>
        <div class="panel-body">
            <label>
                From:
                <asp:TextBox ID="datepicker" runat="server" CssClass="form-control"></asp:TextBox>
                <asp:Timer runat="server"></asp:Timer>
            </label>
        </div>
    </div>    
    <script src="Scripts/jquery-1.10.2.js"></script>
    <script src="Scripts/jquery-ui.js"></script>
    <script>
        $(function () {
            $("#datepicker").datepicker();
        });
    </script>
</asp:Content>

我认为问题是webforms的命名容器

在运行时被Htmlwriter翻译

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

使用这种行为时,jquery无法找到元素,因为它正在检查完整的id字段

所以如果你添加ClientIdMode="static"到

<asp:TextBox ID="datepicker" ClientIdMode="Static"

或者像这样称呼元素

$(function () {
            $("input[name*='datepicker']").datepicker();
        });
https://api.jquery.com/category/selectors/