What is the best practice to have a user control placed multiple times on a page?

G

Gummy

Hello,



I created a user control that has a ListBox and a RadioButtonList (and other
stuff). The idea is that I put the user control on the ASPX page multiple
times and each user control will load with different data (locations,
departments, etc.).



When I click the RadioButtonList the Event is raised on the user control and
I can consume(?) it on the ASPX page. However, I needed to give each user
control its own ID, add the "protected UserControlType userControlID" to the
declarations, and then wire each of them to an Event to get them to work or
to even recognize their ID when the Event is raised.



It works, but my understanding that this is a poor way of doing things. Is
there a better way to approach this?



Here's some of the code in the ASPX page, to recognize the Event:



private void InitializeComponent(){

this.Load += new
System.EventHandler(this.Page_Load);

ucLocation.rblCodeDescrChanged += new
System.EventHandler(this.ChangedSelectionUCRadioButtons);

ucDepartment.rblCodeDescrChanged += new
System.EventHandler(this.ChangedSelectionUCRadioButtons);

}



private void ChangedSelectionUCRadioButtons(object sender, System.EventArgs
e)

{

ReLoadControls((Selection)sender);

}



Thank you for the help.



-Gummy
 
D

dkode

Is all you have to do is reload all the UserControls on the aspx page?

In this case, there is no need to wire up all of these controls on your
aspx page, you should defer these events into the UserControl itself.
Otherwise you have to wire up each and every event like you are doing.

If you wire the events within the UserControl, and have each of them
call a method on the aspx page to "ReloadControls" then that should do
the trick.

What advantage are you getting out of wiring the events on the aspx
page?

Maybe I'm missing something. Someone correct me if I am wrong please.

Thanks

Sean
 
G

Gummy

dkode,

Thank you for responding..

I think I may have mislead you by having the
ReLoadControls((Selection)sender) method. It should be more like
PutInNewDataBasedOnRadioButtonChoices((Selection)sender). So I am not
reloading each UserControl, they are placed statically on the page, I am
just reloading or changing the data that appears in the specific
UserControl.

Being new to this, I am not exactly sure what the difference is between
wiring up the Events in the UserControl or on the ASPX page. My assumption
is that I need to have an event in the UserControl that is raised when an
RadioButtonList is selected. Then that Event can only be recognized in the
ASPX page if I wire it, for each an every UserControl. To me, it seems like
there is a better way to do it (not that I have any clue). For example, if a
button is clicked on a UserControl, I thought the Event is raised and the
ASPX page should know that something was clicked on it and then be able to
find the specific UserControl that was clicked. All that without actually
having to manually wire each Event in the ASPX page. Maybe I am taking OOP
too far?

So if I understand you correctly, I may be doing this right. Right?

The issue, so to speak, was that I was talking to a programmer and he only
gave hints about ViewStates and other methodologies to be more in line with
proper OOP. For now, I figure it works and as I learn more I can improve
upon it.

Thanks again so much for the information.

-Gummy
 
D

dkode

Ok,

That gives me a better idea of what you are trying to do. Next
question:

Is the data that is changed in one UserControl change the data to be
displayed in other UserControls?

Secnario 1:
If you only need to change one UserControls data, then keep you all of
your processing within the UserControls on that level, don't bother
handling anything within the ASPX page, this will only make your
processing more complicated and unnecessary. In this sense, what you
would do is wire up the SelectedIndexChanged event, and handle it
within the appropraite UserControl

Secnario 2:
If one change on one UserControl changes data on all/other UserControls
on the page, it depends on what type of change is taking place.

If you need to reload/modify only a couple of UserControls it might be
best to have methods in your ASPX page that one UserControl calls and
then it can change other UserControls. If you need to reload ALL the
UserControls you could have a delegate setup in your ASPX page that all
of your usercontrols can subscribe to, then when one change is made,
you could fire the delegate that would in turn call methods on other
usercontrols. (this would be the true OOP approach to your problem,
using a multicast delegate)

Heres a tutorial on multicast delegates:
http://en.csharp-online.net/index.php?title=CSharp_Delegates_and_Events
 
G

Gummy

dkode,

Once again, thanks so much for the very detailed reply and link.

It is Scenario 1. Right now when a change is made to the UserControl, I have
that event in the UserControl and it is wired up in the ASPX.

Specifically, do I need to have every single UserControl wired in the ASPX
page? In the code below, the rblCodeDescrChanged is the Event in the
UserControl. So do I need to add the line for each UserControl (ucLocation,
ucDepartment, etc.)?

private void InitializeComponent(){
this.Load += new System.EventHandler(this.Page_Load);
ucLocation.rblCodeDescrChanged += new
System.EventHandler(this.ChangedSelectionUCRadioButtons);
ucDepartment.rblCodeDescrChanged += new
System.EventHandler(this.ChangedSelectionUCRadioButtons);
}

By the way, the code compiles just fine, but I do get the error saying that
the rblCodeDescrChanged event does not exist (or something like that), when
obviously it does. Any thoughts on that?

What if I have multiple events in the UserControl? One for a button click,
one for a dropdownlistbox, do I need to wire the user control for each
event. My hope and assumption is that I can use the Delegates for that (I'll
read the link you sent).

I really appreciate you taking the time to answer my questions so
thoroughly. You have helped me so much and I've learned a great deal. Thank
you.

-Gummy
 
S

Sean Chambers

Ok,

If changes inside one UserControl only affect that UserControl, then
you should have no event wiring in the aspx page. Keep all of your
event related wiring within the UserControl.

In each UserControl you will have to wire the events for that
UserControl via InitializeComponent.

Now, for my next question,
Is each UserControl exactly the same? or all they all different?

Because if they are all the same, you can dynamically add them to the
page which would save you a great deal of time.
 
G

Gummy

I think they are exactly the same - sometimes.
Each one has two listboxes, an ImageButton pointing to the right and one to
the left. The one on the left is populated by a datatable. The user selects
an item(s) in the left and clicks the right arrow to move the item to the
right listbox. Once the items are moved (in each UserControl) the user
clicks a button (on the ASPX) and a Where statement is made of all the
items, in each UserControl.
There are also radiobuttons that sometimes appear above the left listbox so
the user can choose how they want to see the data they want to select. By
either the code or the description.

So when each UserControl is created, I set about three properties to the
UserControl: radio buttons to choose code or description, the DataTable to
be used, the name of the field that the where statement will be queried on.

I thought about the dynamic adding of controls, but I can't figure out how
to do that if each one is loaded by a separate DataTable.

Thank you.
 
S

Sean Chambers

Well,

For beginning, you are on the right track, try not to bite off more
than you can chew.

In the future though, when you get more comfortable, you could easily
do what you are trying to do by dynamically adding the controls. You
would have one UserControl, say BaseControl.ascx, you could then assign
a unique id to the control and add it to a placeholder.

Things get more complex here though, you have to wire the events before
Page_Load, otherwise they will not fire on postback. You have to wire
them in Page_Init. there is also other "gotchas" when working with
dynamically created controls.

Like I said though, your on the right track to begin with, just depends
on how much you want to type and how much hair you want to keep =)

Hope I was helpful!

Sean
 
G

Gummy

Sean,

You have been amazingly helpful and I really appreciate all the time you
took to respond to all my questions.

I have been biting off way too much (and I think I am going to be sick).

I have now gone down the path of dynamically loading the controls and I've
just started learning about the Page_Init stuff. And you're right, I am
pulling out lots of hair.

I am in the middle of figuring out how when the dynamic controls are
reloaded not to make that so slow.

Thanks again for all your help.
 
S

Sean Chambers

No problem,

There are quiet a few tutorials on csharp-online.net and
thecodeproject.com as well as msdn.microsoft.com that talk about
dynamically created controls.

That should help you out.

Sean
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top