如何在子页面上设置javascript

本文关键字:设置 javascript | 更新日期: 2023-09-27 18:16:19

我有两个页面,一个母版页和子页。如何在子页面上设置javascript ?

我想实现textchange功能的子页面使用javascript

如何在子页面上设置javascript

主页:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="Site" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder ID="ContentPlaceHolderHead" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolderMain" runat="server">       
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>

子页:

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebForm1" %>
<asp:Content ID="ContentHead" ContentPlaceHolderID="**ContentPlaceHolderHead**" runat="server" >
<script language="javascript" type="text/javascript">
    // You can write a javascript code HERE...
</script>
</asp:Content>
<asp:Content ID="ContentMain" ContentPlaceHolderID="ContentPlaceHolderMain" runat="server" >
    <!-- Content -->
</asp:Content>

或者,你可以在子页面的代码隐藏文件中添加一个javascript文件。

protected void Page_Load(object sender, EventArgs e)
{
            HtmlGenericControl somejs = new HtmlGenericControl("script");
            somejs.Attributes.Add("type", "text/javascript");
            somejs.Attributes.Add("src", ResolveClientUrl("~/Content/js/something.js"));
            this.Page.Header.Controls.Add(somejs);
}

只需在内容占位符中添加脚本,并确保您设置了子页面的母版页文件,例如:MasterPageFile="~/Mymaster.Master"。

 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<script type="text/javascript" language="javascript">
      //Your Script HERE
</script>

//YOUR HTML TAG HERE
</Content>

您可以使用ClientScriptManager。在子页面中注册startupscript