[ASP.NET]動態Load UserControl – part 2

  • 15980
  • 0
  • 2009-03-11

[ASP.NET]動態Load UserControl – part 2

感謝Allen 郭老師的回應,讓我對自己的CODE再省思了一下,

的確像他說的,若是aspx有註冊user control了,那麼在動態新增user control設定屬性的部分,應該是可以直接宣告該user control的型別來設定屬性,

快超多的…原本的vobjControl.GetType().GetProperty(vstrPropertyName).SetValue(vobjControl, vobjValue, null);還是比較適合不知道control型別時使用。

這邊補上原本作法的改善版。後面會再補上針對郭老師對Unit與enum型別,在SetUserControlProperty()裡是否適用的測試結果。

1.直接使用ASP.usercontrol_custidname_ascx型別。

.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)
    {

    }
    private PublicEnum.CustType _Custtype;
    public PublicEnum.CustType CustType
    {
        get
        { return _Custtype; }
        set
        { _Custtype = value; }

    }
    private Unit _IDWidth;
    public Unit IDWidth
    {
        get
        { return _IDWidth; }
        set
        { _IDWidth = value; }

    }

    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; 
        }
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        this.JoeyTextBox1.Width = this.IDWidth;
    }
}

.aspx.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 LoopAddUserControl_2 : 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++)
        {
            //aspx上有註冊UserControl時
            ASP.usercontrol_custidname_ascx ctlNewTrial = (ASP.usercontrol_custidname_ascx)this.Page.LoadControl("UserControl/CustIDName.ascx");
            ctlNewTrial.ID = "Usrctrl" + index.ToString();
            ctlNewTrial.CustID = index.ToString();
            ctlNewTrial.CustName = index.ToString() + ":name";
            if (index < 5)
            { ctlNewTrial.CustType = PublicEnum.CustType.CompID; }
            else
            { ctlNewTrial.CustType = PublicEnum.CustType.CustID; }            
            ctlNewTrial.IDWidth = Unit.Pixel(16);
            //-----------------------------------------------------------------
            this.Panel1.Controls.Add(ctlNewTrial);
            Label br = new Label();
            br.Text = "index=" + index.ToString() + "<br/>";
            this.Panel1.Controls.Add(ctlNewTrial);
            this.Panel1.Controls.Add(br);
        }
    }


    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;
            this.TextBox3.Text = usrctl1.CustType.ToString();
        }
        else
        {
            this.TextBox1.Text = string.Empty;
            this.TextBox2.Text = string.Empty;
            this.TextBox3.Text = string.Empty;
        }
    }
}

在Page_Load的部分根據老師的建議,改成這樣,方便也快很多,相信效率應該也會比較好。

2.使用Control型別,呼叫SetUserControlProperty()設定屬性。

.aspx.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 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++)
        {
            //aspx上有註冊UserControl時
            //ASP.usercontrol_custidname_ascx ctlNewTrial = (ASP.usercontrol_custidname_ascx)this.Page.LoadControl("UserControl/CustIDName.ascx");
            //ctlNewTrial.ID = "Usrctrl" + index.ToString();
            //ctlNewTrial.CustID = index.ToString();
            //ctlNewTrial.CustName = index.ToString() + ":name";
            //if (index < 5)
            //{ ctlNewTrial.CustType = PublicEnum.CustType.CompID; }
            //else
            //{ ctlNewTrial.CustType = PublicEnum.CustType.CustID; }
            //-----------------------------------------------------------------
            
            //不知道control型別時
            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");
            //測試enum型別
            if (index < 5)
            { SetUserControlProperty(ctlNewTrial, "CustType", PublicEnum.CustType.CompID); }
            else
            { SetUserControlProperty(ctlNewTrial, "CustType", PublicEnum.CustType.CustID); }
            //測試Unit型別
            SetUserControlProperty(ctlNewTrial, "IDWidth", Unit.Pixel(16));
            this.Panel1.Controls.Add(ctlNewTrial);
            Label br = new Label();
            br.Text = "index=" + index.ToString() + "<br/>";
            this.Panel1.Controls.Add(ctlNewTrial);
            this.Panel1.Controls.Add(br);
        }
    }

    /// <summary>
    /// 設定user control的屬性值
    /// </summary>
    /// <param name="vobjControl">usercontrol by page.loadcontrol()</param>
    /// <param name="vstrPropertyName">usercontrol's property</param>
    /// <param name="vobjValue">setting property value</param>
    /// <remarks></remarks>
    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;
            this.TextBox3.Text = usrctl1.CustType.ToString();
        }
        else
        {
            this.TextBox1.Text = string.Empty;
            this.TextBox2.Text = string.Empty;
            this.TextBox3.Text = string.Empty;
        }
    }
}

結果1,選擇index<5的:Unit的部分,可以在動態產生的UserControl,CustID textbox的width獲得證明。

選index<5的,則是CustType應為CompID。

1

結果2,選擇index>5的:選index>5的,則是CustType應為CustID。

2

 

 

 

 

 

疑問:如果不想再aspx上註冊UserControl,那讀值的時候,除了用Control型別的GetValue,還有其他方式嗎??

補上User control互動的參考:WebUserControl之間值得傳遞(使用Interface)


blog 與課程更新內容,請前往新站位置:http://tdd.best/