Accessing values of one customer Control from another custom control

S

Suresh Kumar

Hi All,
I am a newbie in creating ASP.NET custom controls. I have created one custom
control say X.ascx for getting the location of the user like Country, State,
and City. And I have created another control Y.ASCX which has X.ascx control
also. The problem is how do I access the controls on X.ascx fromY.ascx
control.

Can anyone guide me on this???

Thanks for the help.
Suresh
 
J

John Saunders

Suresh Kumar said:
Hi All,
I am a newbie in creating ASP.NET custom controls. I have created one custom
control say X.ascx for getting the location of the user like Country, State,
and City. And I have created another control Y.ASCX which has X.ascx control
also. The problem is how do I access the controls on X.ascx fromY.ascx
control.


You don't.

ASP.NET is object oriented. Among other things this means that you shouldn't
have Y.ascx (which is a User Control, not a Custom Control) poking around in
the guts of X.ascx. What happens if X.ascx decides to change what controls
it uses?

Instead, you should create public properties in X.ascx which return the
values which other controls (including Y.ascx) might be interested in. For
instance:

X.ascx:

....
<asp:TextBox runat="server" id="txtCountry" />
....
X.ascx.cs:
public string Country
{
get {return txtCountry.Text;}
set {txtCountry.Text = value;}
}

Y.ascx:
....
<uc1:X runat="server" id="X1"></uc1:X>
....
Y.ascx.cs:
....
// you can now access X1.Country
 
S

Suresh Kumar

Hi John,
Thanks for the reply. I have created the public properties in X.ascx control
but how do I access it in Y.ascx?
Can you please paste some example here?
thanks
Suresh
 
J

John Saunders

Suresh Kumar said:
Hi John,
Thanks for the reply. I have created the public properties in X.ascx control
but how do I access it in Y.ascx?
Can you please paste some example here?
thanks

Suresh, I gave an example.
Y.ascx:
...
<uc1:X runat="server" id="X1"></uc1:X>
...
Y.ascx.cs:
...
// you can now access X1.Country
--
John


Suresh
John Saunders said:
You don't.

ASP.NET is object oriented. Among other things this means that you shouldn't
have Y.ascx (which is a User Control, not a Custom Control) poking
around
 
S

Suresh Kumar

I am sorry. I didnt see it. Thanks for your help.

Suresh
John Saunders said:
Suresh Kumar said:
Hi John,
Thanks for the reply. I have created the public properties in X.ascx control
but how do I access it in Y.ascx?
Can you please paste some example here?
thanks

Suresh, I gave an example.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top