一、简介
PlaceHolder为asp.net服务器控件用于为代码添加的控件预留空间,下面将介绍下其实际的应用场景。
<asp:PlaceHolder ID="PH" runat="server"></asp:PlaceHolder>
二、实际应用场景
1、动态添加js
cs文件:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Text; namespace UI { public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Literal Li = new Literal(); StringBuilder sb = new StringBuilder(); sb.AppendFormat("<script src="Scripts/Test.js"></script>"); Li.Text = sb.ToString(); PH.Controls.Add(Li); } } }
Html文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="UI.Test" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <asp:PlaceHolder ID="PH" runat="server"></asp:PlaceHolder> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
效果图:
说明: