2016-08-22 30 views
0

我使用VS 2015並開始一個新的ASP C#Webforms項目。該模板包含ScriptManager。問題是我無法讓JQuery在內容頁面上工作。適用於獨立頁面。請參閱我的代碼。ASP Webforms 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> 
+0

您的內容頁面在哪裏?爲什麼不使用ContentPlaceholder和MasterPageFile? – toha

回答

0

添加的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,你應該使用客戶端Id即:

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

也許是這樣的:

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

我粘貼了錯誤的代碼,請查看我內容頁面中的代碼 – user6741701

+0

請檢查我的答案.. – toha

0

這足以單獨申報母版頁的主要jQuery的文件。繼承母版頁的內容頁自動獲取這些jquery文件。不需要在這兩個頁面中聲明。