原文发布时间为:2008-07-30 —— 来源于本人的百度文章 [由搬家工具导入]
Web用户控件test.ascx的源代码:
using System;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class test : System.Web.UI.UserControl
{
private string myname;
private int n;
public string Myname
{
get { return myname; }
set { myname = value; }
}
public int N
{
get { return n; }
set { n = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Text = myname;
for (int i = 0; i < n; i++)
{
ListBox1.Items.Add(i.ToString());
}
}
}
}
----------------------------
这时你可以看见它多了Myname与N这两个属性,你可以右击控件,属性,然后进行更改,还可以动态加载,代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
private ASP.test_ascx t;//页面上必须有一个test这样的控件时,才能有ASP.test_ascx类型
protected void Page_Load(object sender, EventArgs e)
{
Test1.Visible = false;//要动态添加,首先页面上必须有一个这样的控件,所以拉一个,然后不让它显示即可
t = (ASP.test_ascx)LoadControl("~/test.ascx");
t.Myname = "dongtai";
t.N = 3;
PlaceHolder1.Controls.Add(t);
}
}