Dyanmically calling a function in usercontrol

P

phl

hi,

I have a number of usercontrols which inherits a interface. the
function in this interface is used to provide information about the
usercontrol. I would like to call this function say when a button is
clicked.

I can do this by lot of hardcoding, as I have the usercontrol names
stored and I can call this obligatory function like this:

//loop through list of usercontorls
foreach(BLL.TicketInfo ti in TicketInfo)
{
if(ti.UserControlName.ToLower() == "EditProfile.ascx".ToLower())
{
EditProfile uc = (EditProfile)CurPage.FindControl("EditProfile");
dv.AddRange(uc.GetDisabledValidators());
}

//etc
}

while this works, it's crap because it makes adding usercontrols to my
app inconvienientm as I have to add hardcoded stuff everywhere. Just
wondering if you guys knows of a smarter way of doing this, like
dynamically. If I somehow loaded the usercontorl with the string I
have, should I be able query the function it supports by the interface
it's has inherited?

Cheers
 
J

John Saunders

phl said:
hi,

I have a number of usercontrols which inherits a interface. the
function in this interface is used to provide information about the
usercontrol. I would like to call this function say when a button is
clicked.

I can do this by lot of hardcoding, as I have the usercontrol names
stored and I can call this obligatory function like this:

//loop through list of usercontorls
foreach(BLL.TicketInfo ti in TicketInfo)
{
if(ti.UserControlName.ToLower() == "EditProfile.ascx".ToLower())
{
EditProfile uc = (EditProfile)CurPage.FindControl("EditProfile");
dv.AddRange(uc.GetDisabledValidators());
}

//etc
}

while this works, it's crap because it makes adding usercontrols to my
app inconvienientm as I have to add hardcoded stuff everywhere. Just
wondering if you guys knows of a smarter way of doing this, like
dynamically. If I somehow loaded the usercontorl with the string I
have, should I be able query the function it supports by the interface
it's has inherited?

You can loop through all the controls on your page (or panel, or whatever).
You can test to see if you have one of your user controls by checking for
the interface:

foreach (Control c in this.Controls)
{
if (c is IMyInterface)
{
((IMyInterface) c).MyFunction();
}
}

John Saunders
 
K

Karl Seguin

You can loop through them and do:

if (control is IUserControlDetail){
string value = ((IUserControlDetail)control).GetDetails();
}

where IUserControlDetail is the interface and GetDetails() is defined by
said interface..

Karl
 
B

bruce barker

the control is passed to the event as sender. you can just cast to the
interface and call the methods.

protected void button1_click(object sender, EventArgs e)
{
myInterface mybtn = sender as myInterface;
if (mybtn != null)
{
// call interface methods
}
}

-- bruce (sqlwork.com)


| hi,
|
| I have a number of usercontrols which inherits a interface. the
| function in this interface is used to provide information about the
| usercontrol. I would like to call this function say when a button is
| clicked.
|
| I can do this by lot of hardcoding, as I have the usercontrol names
| stored and I can call this obligatory function like this:
|
| //loop through list of usercontorls
| foreach(BLL.TicketInfo ti in TicketInfo)
| {
| if(ti.UserControlName.ToLower() == "EditProfile.ascx".ToLower())
| {
| EditProfile uc = (EditProfile)CurPage.FindControl("EditProfile");
| dv.AddRange(uc.GetDisabledValidators());
| }
|
| //etc
| }
|
| while this works, it's crap because it makes adding usercontrols to my
| app inconvienientm as I have to add hardcoded stuff everywhere. Just
| wondering if you guys knows of a smarter way of doing this, like
| dynamically. If I somehow loaded the usercontorl with the string I
| have, should I be able query the function it supports by the interface
| it's has inherited?
|
| Cheers
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top