G
Guest
Resending this as own topic as didnt get answer from original. Would be
grateful for a response from anyone that knows. Thanks.
Hi there, I found your post really helpful..but i wondered if, once I have
exposed a public property containing the value of a textbox in a user
control..how do I grab this from the calling page? I cant think of the
syntax, since my page doesnt know the contents of the class (and therefore,
the public properties) of the user control??
Sorry if its blatently obvious!
I am currently getting a value using
Page.FindControl(usercontrol);usercontrol.FindControl...etc etc and am told
this is much slower than the public property route.
Many Thanks, Louise.
Why should I rate a post?
Manage Your Profile |Legal |Contact Us |MSDN Flash Newsletter
© 2005 Microsoft Corporation. All rights reserved. Terms of Use |Trademarks
|Privacy Statement
grateful for a response from anyone that knows. Thanks.
Hi there, I found your post really helpful..but i wondered if, once I have
exposed a public property containing the value of a textbox in a user
control..how do I grab this from the calling page? I cant think of the
syntax, since my page doesnt know the contents of the class (and therefore,
the public properties) of the user control??
Sorry if its blatently obvious!
I am currently getting a value using
Page.FindControl(usercontrol);usercontrol.FindControl...etc etc and am told
this is much slower than the public property route.
Many Thanks, Louise.
Was this post helpful to you?Bruce Wood said:You should avoid exposing controls within your user control as public
members. I almost wrote "You should never expose..." but I'm not
feeling that cocky.
The way to handle this problem (and it's a common one), is to think
about the user control as a whole. Obviously, it has 30 text boxes for
a reason. What is the sum functionality of the entire user control? Is
it, for example, an application form?
Assuming that it's an application form (for the sake of argument), then
you don't _really_ want to be able to get at the text boxes. What you
_really_ want is to get at the information on the form. So, you could
do that by exposing one public property for each item on the form:
public string LastName
{
get { return this.txtLastName.Text; }
set { this.txtLastName.Text = value; }
}
Or, you could create a class that would hold all of the application
information, and expose a single property:
public ApplicationInformation ApplicationInformation
{
get { return new ApplicationInformation(this.txtLastName.Text, ...
); }
set
{
this.txtLastName.Text = value.LastName;
...
}
}
Either way, what you've done is expose properties that "talk in the
language of" your user control as a whole. This does two very good
things: 1) it makes the calling code more readable, because callers can
now write code in terms of what the user control does, not in terms of
how it's put together, and 2) it makes the calling code ignorant of the
structure of your user control (loose coupling), so that you can
change, for example, a radio button group to a combo box if that makes
more sense from an interface point of view... all without changing any
calling code. The less your calling code knows about the guts of your
user control, the better.
You can do this with events, too (which is also common). WIthin your
user control you subcribe to events from the text boxes, and then in
the event handler you raise an event particular to the user control.
For example:
this.userControl.ApplicationInformationChanged += new
System.EventHandler(...);
and then inside your user control:
this.txtLastName.TextChanged += new
System.EventHandler(this.txtLastName_TextChanged);
.... etc ...
private void txtLastName_TextChanged(System.EventArgs ea)
{
OnApplicationInformationChanged();
}
.... etc ...
protected void OnApplicationInformationChanged()
{
if (ApplicationInformationChanged != null)
{
ApplicationInformationChanged(System.EventArgs.Empty);
}
}
Again, this isolates your caller from knowing about the controls within
your user control, and creates events that are meaningful at the level
of the user control rather than events that depend upon how the user
control is constructed.
Why should I rate a post?
Manage Your Profile |Legal |Contact Us |MSDN Flash Newsletter
© 2005 Microsoft Corporation. All rights reserved. Terms of Use |Trademarks
|Privacy Statement