ASP.NET动态加载用户控件的实现方法
第一步:例如用户控件放在MyList.Ascx,然后其Control指令是:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ViewComment.ascx.cs" Inherits="Control_ViewComment"%>
这时候已经有了Inherits,自带了ClassName就是其名称,如果没有,则必须创建ClassName属性。
假设其有公共属性ID。
第二步:在某一个ASPX文件需要动态加载的话首先使用
<%@ Reference Control="MyList.Ascx" %>
<%@ Page Language="C#" CodeFile="GetAscx.aspx.cs" Inherits="AdEntity_GetAscx" %>
引用,这时候在代码页GetAscx.aspx.cs可以动态加载ASCX控件了:
Control_ViewComment ctrl = (Control_ViewComment)Page.LoadControl("~/Control/ViewComment.ascx");
ctrl.ID = Request["AdentityId"];
base.Controls.Add(ctrl);
另:
<%@ Register Assembly="Business" Namespace="Business" TagPrefix="My" %>
完成。
相关文档:
C# ASP.NET里@的妙用
ASP.NET C# string 字符串的前面可以加 @ 可以将转义字符(\)当作普通字符对待。
比如:string str = @"C:\Windows";
如果我们不用 @ 的话,应该是:string str = "C:\\Windows";
@ 字符串中,我们用两个连续英文双引号表示一个英文双引号,如下字符串的实际内容为:="=,字符串长度为 3 ......
/// <summary>
/// 写Cookies
/// </summary>
/// <param name="response"></param>
/// <param name="request"></param>
&n ......
Javascript 在ASP.net 母板页下访问 控件ID:
对于 html control : 直接访问ID
document.getElementById("hfRespondID");
对于 Web control :
document.getElementById("<%= this.hfRespondID.ClientID %>") [注意大小写
]
&nb ......
<%@ Page Language="C#" %>
<%@ import Namespace="System.Collections" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e) {
if(!Page.IsPostBack){
ArrayList data = new ArrayList();
data.Add(new Person("Tom",33,true));
data.Add(new Person("Jhon",39,false));
da ......