简体   繁体   中英

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

I need to add java script tag in content page of asp.net application. The script works fine with html tags but in content it is not working here is the code.

    <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>

I think the problem is the namingcontainer of webforms

at runtime the Htmlwriter translates

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

with this behaviour jquery couldn't find the element as it is checking the complete id field

so wether you add the ClientIdMode="static" to

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

or you call the element with something like this

$(function () {
            $("input[name*='datepicker']").datepicker();
        });

https://api.jquery.com/category/selectors/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM