将 jQuery 代码添加到 Index.aspx 页面

Adding jQuery code to Index.aspx pages

本文关键字:Index aspx 页面 添加 jQuery 代码      更新时间:2023-09-26
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<script runat="server">   
    void Page_Load(object sender, EventArgs e) {
       Page.RegisterClientScriptBlock("MyScript",
       "<script language=javascript>"+ 
       "alert(Date());"+ 
       "</script>");
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: ViewBag.Message %></h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p>
</asp:Content>

这给了我以下错误:

Only Content controls are allowed directly in a content page that contains Content controls.

只需从旧索引页复制并粘贴到新索引页(或复制并粘贴脚本 src 引用(。 这与ASP完全无关。

包含jquery指向您页面的链接(在jquery使用代码之前(...

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.1.min.js"></script>

如果使用母版页,请将其添加到其中,以便所有页面都已包含它。然后,您所需要的只是按原样复制代码。