really stuck on passing public variables between controls

D

Darrel

I'm really stuck on the concept of using public variables to pass
information between usercontrols. I'm pretty sure I'm just flubbing up the
syntax.

This is what I want:

page
usercontrol 1 - set variable
usercontrol 2 - read variable

I haven't been able to find a tutorial that addresses this issue. It has
been suggested that I use pulic variables, however.

Unfortunately, I'm stumped on the syntax.

I have this in usercontrol 1:

public globadlPageID as String = "2"
But I'm not sure how to access that from Usercontrol 2. Is there a way to
grab something from another control?

-Darrel
 
K

Karl

You have to go through their common element - the page:

Say your page is something like:

<uc1:control1 id="firstControl" runat="Server" />
<uc3:control2 id="secondControl" runat="Server" />

and your codebehind:

public class WebForm1
inherits System.Web.UI.Page

public firstControl as Control1
public secondControl as Control2

sub page_load
...
end sub
end class

you can access the "globalPageId" in firstControl from secondControl via
(this is secondControl):


sub page_load
dim pageId as string = ctype(Page, WebForm1).firstControl.globalPageId
end sub


that's it. Now, you might wanna make sure that Page is of type WebForm1 -
since user controls are meant to be used on multiple pages...but that's the
general idea...

Karl
 
D

Darrel

Karl:

THANK YOU! That saved me quite the headache. OK, I think that makes sense. I
can get it to work, but I don't quite get the cast statement. I'm casting
the parent page as my usercontrol? I'm not quite clear on how that logic
works.

Again, THANKS!

-Darrel
 
K

Karl

No,

ctype(Page, WebForm1)

you are casting Page (which is of type System.Web.UI.Page) to the specific
page instance, WebForm1. System.Web.UI.Page doesn't expose a "firstControl"
property, but WebForm1 does..

Karl
 
D

Darrel

No,

ctype(Page, WebForm1)

you are casting Page (which is of type System.Web.UI.Page) to the specific
page instance, WebForm1. System.Web.UI.Page doesn't expose a
"firstControl"
property, but WebForm1 does..

Thanks, Karl. That really helps clear things up for me!

-Darrel
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top