ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CustIDName.ascx.cs" Inherits="UserControl_CustIDName" %>
<%@ Register Assembly="Joey" Namespace="Joey" TagPrefix="cc1" %>
.ascx.cs
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 UserControl_CustIDName : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string CustID
{
get
{ return this.JoeyTextBox1.mTextBox.Text; }
set
{ this.JoeyTextBox1.mTextBox.Text = value; }
}
public string CustName
{
get
{ return this.JoeyTextBox2.mTextBox.Text; }
set
{ this.JoeyTextBox2.mTextBox.Text = value; }
}
public string ValidationGroup
{
get
{ return this.JoeyTextBox1.ValidationGroup; }
set
{
this.JoeyTextBox1.ValidationGroup = value;
this.JoeyTextBox2.ValidationGroup = value;
}
}
}
接著我們在一個空白頁面上,加入一個panel
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 LoopAddUserControl : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.DropDownList1.Items.Add(new ListItem("user control index"));
for (int i = 0; i < 10; i++)
{
this.DropDownList1.Items.Add(new ListItem(i.ToString()));
}
}
for (int index = 0; index < 10; index++)
{
Control ctlNewTrial = this.Page.LoadControl("UserControl/CustIDName.ascx");
SetUserControlProperty(ctlNewTrial, "ID", "Usrctrl" + index.ToString());
SetUserControlProperty(ctlNewTrial, "CustID", index.ToString());
SetUserControlProperty(ctlNewTrial, "CustName", index.ToString()+":name");
this.Panel1.Controls.Add(ctlNewTrial);
Label br = new Label();
br.Text = "index=" + index.ToString()+ "
";
this.Panel1.Controls.Add(ctlNewTrial);
this.Panel1.Controls.Add(br);
}
}
///
/// 設定user control的屬性值
///
/// usercontrol by page.loadcontrol()
/// usercontrol's property
/// setting property value
///
public void SetUserControlProperty(Control vobjControl, string vstrPropertyName, object vobjValue)
{
vobjControl.GetType().GetProperty(vstrPropertyName).SetValue(vobjControl, vobjValue, null);
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ASP.usercontrol_custidname_ascx usrctl1 = (ASP.usercontrol_custidname_ascx)this.Panel1.FindControl("Usrctrl" + this.DropDownList1.SelectedValue);
if (usrctl1 != null)
{
this.TextBox1.Text = usrctl1.CustID;
this.TextBox2.Text = usrctl1.CustName;
}
else
{
this.TextBox1.Text = string.Empty;
this.TextBox2.Text = string.Empty;
}
}
}
參考文件
[ASP.NET]動態Load UserControl – part 2 - In 91- 點部落
WebUserControl之間值得傳遞(使用Interface) - topcat 姍舞之間的極度凝聚- 點部落
[ASP.NET]動態新增User Control,並設定屬性值與取屬性值 - In 91- 點部落
沒有留言:
張貼留言