I have a couple of WebForms that all inherit from a custom base class
(which in turn inherits from System.Web.UI.P age). I would like to
locate a function in the base class that iterates through the control
collection of the child page, looks at the type (looking for
System.Web.UI.W ebControls.Labe l type), and pulls out the full
namespace and ID for that control (then goes out to a resouce file and
sets the text of the control for the specified locale amoungst some
other things). Everything is pretty simplistic except I cant figure
out how to extract the object namespace from the control (if that
information is even retained in the assembly).
In other words, I need to extract the namespace of a specific member
(NOT the namespace of the member "type"). I am looking for something
like "Company.Produc t.Module.SubMod ule.lblStreetAd dress3".
Below is some sample code. Thanks for your help!
Solomon Shaffer
Child WebForm:
namespace Company.Product .Module.SubModu le
{
/// <summary>
/// Summary description for CreateAddress .
/// </summary>
public class CreateAddress : Page
{
protected System.Web.UI.W ebControls.Labe l lblStreetAddres s1;
protected System.Web.UI.W ebControls.Labe l lblStreetAddres s2;
protected System.Web.UI.W ebControls.Labe l lblStreetAddres s3;
protected System.Web.UI.W ebControls.Labe l lblCity;
protected System.Web.UI.W ebControls.Labe l lblState;
protected System.Web.UI.W ebControls.Labe l lblZipCode;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
}
}
}
Base class:
namespace Company.Product .Module
{
/// <summary>
/// Summary description for Page.
/// </summary>
public class Page : System.Web.UI.P age
{
private void Page_Load(objec t sender, System.EventArg s e)
{
System.Web.UI.P age oPage = (System.Web.UI. Page)sender;
SetControlText( oPage );
}
private void SetControlText( Control oParentControl )
{
ResourceManager oResourceManage r = new ResourceManager ( "English",
typeof( Page ).Assembly );
if ( oParentControl. HasControls() )
{
foreach ( Control oControl in oParentControl. Controls )
{
if ( oControl.GetTyp e().ToString() ==
"System.Web.UI. WebControls.Lab el" )
{
Label oLabel = (Label)oControl ;
//This does not work!
string sNamespace = oControl.GetTyp e().Namespace.T oString();
oLabel.Text = oResourceManage r.GetString( sNamespace + "." +
oControl.ID );
}
SetControlText( oControl );
}
}
}
}
}
Resource File:
<data name="Company.P roduct.Module.S ubModule.lblStr eetAddress3">
<value>Addres s Line 3</value>
</data>
(which in turn inherits from System.Web.UI.P age). I would like to
locate a function in the base class that iterates through the control
collection of the child page, looks at the type (looking for
System.Web.UI.W ebControls.Labe l type), and pulls out the full
namespace and ID for that control (then goes out to a resouce file and
sets the text of the control for the specified locale amoungst some
other things). Everything is pretty simplistic except I cant figure
out how to extract the object namespace from the control (if that
information is even retained in the assembly).
In other words, I need to extract the namespace of a specific member
(NOT the namespace of the member "type"). I am looking for something
like "Company.Produc t.Module.SubMod ule.lblStreetAd dress3".
Below is some sample code. Thanks for your help!
Solomon Shaffer
Child WebForm:
namespace Company.Product .Module.SubModu le
{
/// <summary>
/// Summary description for CreateAddress .
/// </summary>
public class CreateAddress : Page
{
protected System.Web.UI.W ebControls.Labe l lblStreetAddres s1;
protected System.Web.UI.W ebControls.Labe l lblStreetAddres s2;
protected System.Web.UI.W ebControls.Labe l lblStreetAddres s3;
protected System.Web.UI.W ebControls.Labe l lblCity;
protected System.Web.UI.W ebControls.Labe l lblState;
protected System.Web.UI.W ebControls.Labe l lblZipCode;
private void Page_Load(objec t sender, System.EventArg s e)
{
// Put user code to initialize the page here
}
}
}
Base class:
namespace Company.Product .Module
{
/// <summary>
/// Summary description for Page.
/// </summary>
public class Page : System.Web.UI.P age
{
private void Page_Load(objec t sender, System.EventArg s e)
{
System.Web.UI.P age oPage = (System.Web.UI. Page)sender;
SetControlText( oPage );
}
private void SetControlText( Control oParentControl )
{
ResourceManager oResourceManage r = new ResourceManager ( "English",
typeof( Page ).Assembly );
if ( oParentControl. HasControls() )
{
foreach ( Control oControl in oParentControl. Controls )
{
if ( oControl.GetTyp e().ToString() ==
"System.Web.UI. WebControls.Lab el" )
{
Label oLabel = (Label)oControl ;
//This does not work!
string sNamespace = oControl.GetTyp e().Namespace.T oString();
oLabel.Text = oResourceManage r.GetString( sNamespace + "." +
oControl.ID );
}
SetControlText( oControl );
}
}
}
}
}
Resource File:
<data name="Company.P roduct.Module.S ubModule.lblStr eetAddress3">
<value>Addres s Line 3</value>
</data>
Comment