如有不明白的地方欢迎加QQ群14670545 探讨
主要是通过System.Web.UI.UserControl的LoadControl方法
1.模版型
我们新建一个txt文件,里面写入一个用户控件的代码
<%--用户控件模板--%> <%@ Control Language="C#" ClassName="MyPage" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Button1_Click(object sender, EventArgs e) { Calendar1.BackColor = System.Drawing.Color.LightBlue; } protected void Calendar1_DayRender(object sender, DayRenderEventArgs e) { if (e.Day.Date == DateTime.Now.Date) { e.Cell.Controls.Clear(); var img = new Image { ImageUrl = "http://avatar.profile.csdn.net/1/8/D/2_sp1234.jpg", AlternateText="今日" }; e.Cell.Controls.Add(img); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:Calendar ID="Calendar1" runat="server" ondayrender="Calendar1_DayRender"></asp:Calendar> aslkdfka dsfasdfasdf<br /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /> </form> </body> </html>
页面代码如下
<%@ Page Language="C#" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { var tar = Server.MapPath("~/Manager/MyPage.ascx"); if (!System.IO.File.Exists(tar)) System.IO.File.Copy(Server.MapPath("~/Manager/MyPage.txt"), tar); var uc = this.LoadControl("~/Manager/MyPage.ascx"); this.Controls.Add(uc); } </script>
这么做的好处是,我们可以在后台随时编辑这个txt文件。当然,如果这个用户控件有服务器控件事件的话,这点不好处理,需要专门写一些规则来定义,然后按照规则来编辑事件,其实也好处理,因为我们可以自定义控件,然后按规则来生成,相当于cms的模板一样。也就是说
if (!System.IO.File.Exists(tar)) System.IO.File.Copy(Server.MapPath("~/Manager/MyPage.txt"), tar);这一段我们做加工处理啦
2.重写Render方法
写一个基类,比如WrapperPageBase .cs,它继承于System.Web.UI.Page
代码如下:
namespace MyWebSiteTest { public class WrapperPageBase : System.Web.UI.Page { /// <summary> /// /// </summary> public System.Web.UI.UserControl control { get; set; } protected override void Render(System.Web.UI.HtmlTextWriter writer) { if (control == null) control.LoadControl("~/Controls/default.ascx"); control.RenderControl(writer); base.Render(writer); } } }
下面做简单的测试,代码也就简单的写了下:
ccA:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ccA.ascx.cs" Inherits="MyWebSiteTest.Controls.ccA" %> 我是ccA控件呈现的
ccB:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ccB.ascx.cs" Inherits="MyWebSiteTest.Controls.ccB" %> 我是ccB控件呈现的
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="default.ascx.cs" Inherits="MyWebSiteTest.Controls.default" %> 我是default控件呈现的
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="JustWant.ascx.cs" Inherits="MyWebSiteTest.Controls.JustWant" %> 我是ccA控件呈现的
namespace MyWebSiteTest.Manager { public partial class Ajax_Test : WrapperPageBase { protected void Page_Load(object sender, EventArgs e) { var str = Request["Want"]; if (str == "IWantA") this.control = (UserControl)this.LoadControl("~/Controls/ccA.ascx"); // 用户控件ccA else if (str == "IWantB") this.control = (UserControl)this.LoadControl("~/Controls/ccB.ascx"); // 用户控件ccB else if (str == "JustWant") this.control = (UserControl)this.LoadControl("~/Controls/JustWant.ascx");//用户控件JustWant else this.control = (UserControl)this.LoadControl("~/Controls/default.ascx"); } } }
到此,自己测试下吧。