【问题标题】:ASP Webforms JQueryASP Webforms JQuery
【发布时间】:2016-08-22 01:50:10
【问题描述】:

我正在使用 VS 2015 并开始一个新的 ASP C# Webforms 项目。该模板包括 ScriptManager。问题是我无法让 JQuery 在内容页面上工作。独立页面工作正常。请参考我的代码。

谢谢,

母版页代码

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="PayrollTimeManagement.SiteMaster" %>

<!DOCTYPE html>    

<html lang="en">
<head runat="server">
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title><%: Page.Title %> - My ASP.NET Application</title>    

    <asp:PlaceHolder runat="server">
         <%: Scripts.Render("~/bundles/modernizr") %>

    </asp:PlaceHolder>
    <webopt:bundlereference runat="server" path="~/Content/css" />
    <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />    

    <script src="Scripts/jquery-ui-1.12.0/external/jquery/jquery.js"></script>
        <script src="Scripts/jquery-ui-1.12.0/jquery-ui.js"></script>
    <script src="Scripts/jquery-ui-1.12.0/jquery-ui.min.js"></script>    

</head>
<body>
    <form runat="server">
        <asp:ScriptManager runat="server">
            <Scripts>
                <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=301884 --%>
                <%--Framework Scripts--%>
                <asp:ScriptReference Name="MsAjaxBundle" />
                <asp:ScriptReference Name="jquery" />
                <asp:ScriptReference Name="bootstrap" />
                <asp:ScriptReference Name="respond" />
                <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
                <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
                <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
                <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
                <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
                <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
                <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
                <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
                <asp:ScriptReference Name="WebFormsBundle" />
                <%--Site Scripts--%>
            </Scripts>
        </asp:ScriptManager>

        <div class="navbar navbar-inverse navbar-fixed-top">
            <div class="container">
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" runat="server" href="~/">Application name</a>
                </div>
                <div class="navbar-collapse collapse">
                    <ul class="nav navbar-nav">
                        <li><a runat="server" href="~/">Home</a></li>
                        <li><a runat="server" href="~/About">About</a></li>
                        <li><a runat="server" href="~/Contact">Contact</a></li>
                    </ul>
                    <p class="nav navbar-text navbar-right">Hello, <asp:LoginName runat="server" />!</p>
                </div>
            </div>
        </div>
        <div class="container body-content">
            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>
            <hr />
            <footer>
                <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
            </footer>
        </div>

    </form>
</body>
</html>

以下是我的内容页面的代码 - 代码不起作用。我在我的母版页和内容页中都引用了 jquery,但不起作用

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="PayrollTimeManagement.WebForm2" %>    

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">     

    <script src="Scripts/jquery-ui-1.12.0/external/jquery/jquery.js"></script>
   <script src="Scripts/jquery-ui-1.12.0/jquery-ui.js"></script>
    <script src="Scripts/jquery-ui-1.12.0/jquery-ui.min.js"></script>

    <script src="Scripts/select2.js"></script>
    <link href="Content/css/select2.css" rel="stylesheet" />       

     <script type="text/javascript">
         $(document).ready(function () { $("#DropDownList1").select2(); });
</script>

    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>aawe3wrw4t34t</asp:ListItem>
        <asp:ListItem>ss</asp:ListItem>
        <asp:ListItem>ff</asp:ListItem>
        <asp:ListItem></asp:ListItem>
        <asp:ListItem></asp:ListItem>
        <asp:ListItem>rye</asp:ListItem>
        <asp:ListItem></asp:ListItem>
        <asp:ListItem>43t5</asp:ListItem>
        <asp:ListItem></asp:ListItem>
        <asp:ListItem>rey</asp:ListItem>
        <asp:ListItem></asp:ListItem>
        <asp:ListItem></asp:ListItem>
        </asp:DropDownList>

</asp:Content>

【问题讨论】:

  • 您的内容页面在哪里?为什么不使用 ContentPlaceholder 和 MasterPageFile?

标签: c# jquery asp.net webforms


【解决方案1】:

仅在母版页中声明主要的 jQuery 文件就足够了。继承母版页的内容页面会自动获取这些 jquery 文件。不需要在两个页面中声明。

【讨论】:

    【解决方案2】:

    添加 MasterPageFile 属性:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="PayrollTimeManagement.WebForm3" MasterPageFile="~/Master/Site.master"  %>
    

    还有你的内容:

    <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
    </asp:Content>
    

    请在您的内容中删除此代码:

    <script src="Scripts/jquery-ui-1.12.0/external/jquery/jquery.js"></script>
    <script src="Scripts/jquery-ui-1.12.0/jquery-ui.js"></script>
    <script src="Scripts/jquery-ui-1.12.0/jquery-ui.min.js"></script>
    

    你不应该多次输入 jquery js lib.. 你已经把它放在母版页上,所以,不需要再把它放在内容页上。

    然后要获取组件 ID,您应该使用 ClientId 即:

    document.getElementById('<%=DropDownList1.ClientID%>').value
    

    可能是这样的:

    $(document).ready(function () { 
        $("#<%=DropDownList1.ClientID %>").select2(); 
    });
    

    【讨论】:

    • 我粘贴了错误的代码,请查看我的内容页面中的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2021-03-15
    相关资源
    最近更新 更多