ASP Webforms JQuery
本文关键字:JQuery Webforms ASP | 更新日期: 2023-09-27 18:03:24
我正在使用VS 2015并开始一个新的asp.net 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>© <%: 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>
仅在母版页中声明jQuery主文件就足够了。继承母版页的内容页自动获取这些jquery文件。不需要在两个页面都声明。
添加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();
});