Problem:
There is a page for normal list display and chart display.
Solution:
Define two user controls: chartlist. ascx and datagridlist. ascx.
Abstract A class factory, such as abstractfactory.
// The chartlist. ascx and datagridlist. ascx control classes are both derived classes of system. Web. UI. usercontrol and inherit an iview interface. There is only one method, bindlist ()
// The example is very simple, and the interface can be expanded...
Let's take a look at how abstractfactory provides us with the corresponding "product"
Public class abstractfactory
{
Public Enum viewtype
{
Chart, DataGrid
}
Private iview _ iview;
Public iview
{
Get {return _ iview ;}
}
Private system. Web. UI. webcontrols. placeholder _ placeholder = new system. Web. UI. webcontrols. placeholder ();
Public System. Web. UI. webcontrols. placeholder myholder
{
Get {return _ placeholder ;}
}
Public abstractfactory (INT viewtype)
{
Switch (viewtype)
{
Case (INT) viewtype. chart:
Chartlist c = new chartlist ();
_ Iview = C;
_ Placeholder. Controls. Add (C );
Break;
Case (INT) viewtype. DataGrid:
Datagridlist d = new datagridlist ();
_ Iview = D;
_ Placeholder. Controls. Add (d );
Break;
}
}
}
// Defined interface
Public interface iview
{
Void bindlist ();
}
// The specific code of datagridlist and chartlist is not provided here.
Then complete the final work in a page.
Public class documentinfo: system. Web. UI. Page
{
Protected system. Web. UI. webcontrols. placeholder placeholder1;
Protected system. Web. UI. webcontrols. dropdownlist dropdownlist1;
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Dropdownlist1.items. Add (New listitem (abstractfactory. viewtype. Chart. tostring (), abstractfactory. viewtype. Chart. tostring ()));
Dropdownlist1.items. Add (New listitem (abstractfactory. viewtype. DataGrid. tostring (), abstractfactory. viewtype. DataGrid. tostring ()));
Abstractfactory a1 = new abstractfactory (convert. toint32 (dropdownlist1.selectedvalue ));
Placeholder1 = a1.myholder;
A1.iview. bindlist ();
}
}