- 建立 MyControl Project 並產生三個 class (MyTextBox、MyDropDownList 和 MyCheckBox),接實作 Interface (IDemo)
- 建立 IntertfaceDemo Project 並參考 MyControl.dll
MyControl Project
MyTextBox
namespace MyControl
{
public class MyTextBox:TextBox,IDemo
{
public string GetValue()
{
return this.Text;
}
}
}
MyDropDownListnamespace MyControl
{
public class MyDropDownList : DropDownList,IDemo
{
public string GetValue()
{
return this.SelectedValue;
}
}
}
MyCheckBoxnamespace MyControl
{
public class MyCheckBox:CheckBox,IDemo
{
public string GetValue()
{
return this.Checked.ToString();
}
}
}
IDemonamespace MyControl
{
public interface IDemo
{
string GetValue();
}
}
InterfaceDemo Projectusing MyControl;
namespace InterfaceDemo
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
lblResult.Text = string.Empty;
foreach (Control item in Form.Controls)
{
var ctrl = item as IDemo;
if (ctrl == null) continue;
lblResult.Text += string.Format("{0} = {1}", item.ID, ctrl.GetValue()) + "<br/>";
}
}
}
}
畫面上把 MyTextBox、MyDropDownList 和 MyCheckBox 拉進來使用,並新增一個 Button 和 Label 來顯示結果執行結果
- 參考資料
- 論壇問題討論
沒有留言:
張貼留言