passing argument to user control when creating it

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.
 
C

Cowboy \(Gregory A. Beamer\)

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.
 

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,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top