Casting to "something that can do a postback"

H

Helen

I've written a web control that allows you to hide certain bits of
your page when the specified selectbox has a certain value. It's a
simple enough bit of logic, but I got sick of coding it again and
again in different pages. Now I have a page that needs the same
functionality except with a checkbox rather than a dropdown. The code
I need will be mostly the same, so I decided to make a subclass of the
HtmlHider specially for hiding checkboxes.

My problem is that to get the best value out of the existing code, I
need to be able to set the type of the hider control to be "something
that can do a postback" (since this is the only thing that the base
class really needs the hider control to be able to do).

The common parent class of checkbox and dropdown share is WebControl,
but that doesn't support any of the postback functionality
(specifically the autopostback property). I then tried casting the
hider control to the IPostBackDataHandler interface that both dropdown
and checkbox implement, but that didn't give me access to the
autopostback property that I need to access.

Is there a way to do what I want to do or will I just have to rewrite
most the code for the checkbox?

Helen
 
J

John Saunders

Helen said:
I've written a web control that allows you to hide certain bits of
your page when the specified selectbox has a certain value. It's a
simple enough bit of logic, but I got sick of coding it again and
again in different pages. Now I have a page that needs the same
functionality except with a checkbox rather than a dropdown. The code
I need will be mostly the same, so I decided to make a subclass of the
HtmlHider specially for hiding checkboxes.

My problem is that to get the best value out of the existing code, I
need to be able to set the type of the hider control to be "something
that can do a postback" (since this is the only thing that the base
class really needs the hider control to be able to do).

The common parent class of checkbox and dropdown share is WebControl,
but that doesn't support any of the postback functionality
(specifically the autopostback property). I then tried casting the
hider control to the IPostBackDataHandler interface that both dropdown
and checkbox implement, but that didn't give me access to the
autopostback property that I need to access.

Is there a way to do what I want to do or will I just have to rewrite
most the code for the checkbox?

Sorry, there isn't any common base class which contains AutoPostback.

If you were doing this in ASP.NET 2.0, you could use generics. Which gives
me an idea. Maybe you could isolate the piece which has to touch
AutoPostBack into a class, make a version for each control type you're
interested in, then give all the classes a common base class. The rest of
your code would be the same, but you could use the appropriate derived class
for the control of interest.
 
S

Scott G.

Use reflection to determine if the control has an AutoPostback property (and you could the reflected info to call the property); since, there are only of few classes you are interested, you could also do something less clever and just test the object instance to see if it's an instance of the class you are after.

Scott
I've written a web control that allows you to hide certain bits of
your page when the specified selectbox has a certain value. It's a
simple enough bit of logic, but I got sick of coding it again and
again in different pages. Now I have a page that needs the same
functionality except with a checkbox rather than a dropdown. The code
I need will be mostly the same, so I decided to make a subclass of the
HtmlHider specially for hiding checkboxes.

My problem is that to get the best value out of the existing code, I
need to be able to set the type of the hider control to be "something
that can do a postback" (since this is the only thing that the base
class really needs the hider control to be able to do).

The common parent class of checkbox and dropdown share is WebControl,
but that doesn't support any of the postback functionality
(specifically the autopostback property). I then tried casting the
hider control to the IPostBackDataHandler interface that both dropdown
and checkbox implement, but that didn't give me access to the
autopostback property that I need to access.

Is there a way to do what I want to do or will I just have to rewrite
most the code for the checkbox?

Helen
 

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

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top