“ 用户控件中有个label控件,需要根据用户控件被引用后的ID值来未其赋值,请问如何才能在ASCX中得到引用后的用户控件ID”
这是来自某论坛的问题,不过标题Insus.NET有所更改。
用户控件,将有可能被aspx或是masterPgae所应用。用户控件就是象打工仔,有可能被雇主聘用。每位打工仔都想赚钱,谁会给自己钱,也许不清楚;而雇主聘请人才或是投资,他只管付钱,付给谁也不一定清楚,因此Insus.NET在此创建一个接口,接口中有一个方法,是付钱。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for IPayable
/// </summary>
namespace Insus.NET
{
public interface IPayable
{
void Pay(object value);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for IPayable
/// </summary>
namespace Insus.NET
{
public interface IPayable
{
void Pay(object value);
}
}
打工仔(用户控件)没有钱用,就是去打工赚钱,也就是实作这个接口:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class InsusUserControl : System.Web.UI.UserControl,IPayable
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Pay(object value)
{
this.Label1.Text = value.ToString();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class InsusUserControl : System.Web.UI.UserControl,IPayable
{
protected void Page_Load(object sender, EventArgs e)
{
}
public void Pay(object value)
{
this.Label1.Text = value.ToString();
}
}
接下来,是什么样的老板聘请到你呢,如果是page的,也就是说,用户控件被拉至aspx页面中。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在老板发工资时,他要知道有谁努力工作,才付薪水。
IPayable uc = (IPayable)this.InsusUserControl1;
uc.Pay("$15k");
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// 在老板发工资时,他要知道有谁努力工作,才付薪水。
IPayable uc = (IPayable)this.InsusUserControl1;
uc.Pay("$15k");
}
}