passing argument to user control when creating it

  • Thread starter Thread starter brixdotnet
  • Start date Start date
B

brixdotnet

Hi there!

I made own user control for displaying and editing content. In
Page_Load of this control is checked if user is authorized for editing
if so, button for editing is shown, otherwise controll works in
display only mode. I want now use this control to add new content. The
idea is that in another web form, let's call AddNewContent.aspx there
is placed MyContentControl and I want to run in Editing mode with all
fields reset to defaults. How to do this with asp.net?

Normally I would do this using constructor and passing desired option
to a object during creation, but there are two things:
1. I got strange error when I defined contructor in MyContentControl,
2. And what's more important, where I am supposed to use this
constructor? In Page_Load of AddNewContent.aspx form it is too late.

Can you help me how to customize control apperance and behaviour
during user controll creation?

Best regards
B.
 
Hi there!

I made own user control for displaying and editing content. In
Page_Load of this control is checked if user is authorized for editing
if so, button for editing is shown, otherwise controll works in
display only mode. I want now use this control to add new content. The
idea is that in another web form, let's call AddNewContent.aspx there
is placed MyContentControl and I want to run in Editing mode with all
fields reset to defaults. How to do this with asp.net?

There are a couple of ways to play this game. Depending on your exact needs,
here they are:

1. Add a constructor that contains the properties you need to add. Then,
fire that constructor and add the control at runtime.

2. Change the binding/display at runtime by adding the behavior to the
property you are changing. You can also set properties and create a "re"
render method.
Normally I would do this using constructor and passing desired option
to a object during creation, but there are two things:
1. I got strange error when I defined contructor in MyContentControl,

What error are you getting?
2. And what's more important, where I am supposed to use this
constructor? In Page_Load of AddNewContent.aspx form it is too late.

In some cases, Page_Load is fine. In others, you have to go to Init. It
really depends on what you are doing. If you use a constructor, it does not
make sense to anchor the control to the page in the tags. This leads to less
than WYSIWYG in the UI, however.

If you add properties, you can set them in backend events, if that is your
desire. You just end up rebinding. Not as efficient, but unless you are
performing serious calculations, the extra cycles should not affect you too
much. If they do, consider a server control, where you can use it a lot more
like a windows control.
Can you help me how to customize control apperance and behaviour
during user controll creation?

The best way, ultimately, is make this a server control. It is a bit of a
pain, but if you have the user control created, it is not a huge amount of
coding to duplicate the look. Makes it far more reusable as well.

Hope this helps.

--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#

or just read it:
http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box! |
********************************************
 
Back
Top