access controls on web user control x from web user control Y

I have 2 web users controls in a page and I would like to get or set 

the selected values of a few dropdown boxes on web user control X from web user control Y and I also would like to know how to access functions that are on a web user control X from a web
user control Y, is this possible? How can I do it?
0
Giorgio
2/25/2006 1:56:53 PM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 12 Replies
👁️‍🗨️ 2203 Views



            

User can access and set the values of the User Control from .aspx page through properties,using javascript and in code-behind of aspx page.The details of it are shown below

  1. Using Properties
    If the Sample.ascx control has two textboxes and submit button.You can access the values of the textboxes in the control from an .ascx page by declaring public property in the .ascx page.

    Public Property FirstName() As String
    Get
    Return txtFirstName.Text
    End Get
    Set
    txtFirstName.Text = Value
    End Set
    End Property

    In .aspx page,you can access FirstName using

    SampleControl.FirstName

    You can set the FirstName of the control from aspx page using

    SampleControl.FirstName = "Smith" 

  2. Using Javascript
    You can set the values of the controls declared in the .ascx page by

    Private document.forms(0)("SampleControl:txtFirstName").value ="Smith"

    You can get the values of the controls declared in the .ascx page by

    Private document.forms(0)("SampleControl:txtFirstName").value

  3. In ASPX code behind file
    Private objSampleControl As SampleControl = CType(Page.FindControl("SampleControl"), SampleControl)
    Private objTextBox As TextBox = objTestControl.FindControl("txtFirstName")Private strFirstName As String = objTextBox.Text



            
Thanks,
Smith
http://spaces.msn.com/johnysmith
0
JohnySmithy2k5
2/25/2006 2:53:04 PM

I think you didn't understand what I want to do. I don't want to do any code behind on the aspx page.
I have web user control X with 3 dropdowns (userctrlX.ascx) and function fnctX(str1, str2)
I have web user control Y (userctrlY.ascx)
I have both user controls userctrlX.ascx and userctrlY.ascx on page pageX.aspx
What I want is to get or set the values on the dropdowns on userctrlX.ascx from userctrlY.ascx and also use the function fnctX(str1, str2) on userctrlX.ascx from (userctrlY.ascx)
How can I do this?
0
Giorgio
2/26/2006 3:50:04 PM

Ummm, he told you exactly how to do that. If you need more info, look here:
http://forums.asp.net/718102/ShowPost.aspx
NC...
 
0
NC01
2/26/2006 6:28:50 PM
No he didn't!!

I said from an aspx page AND I want to access it from the other control NOT from the aspx page!
0
Giorgio
2/26/2006 10:52:24 PM

Well here you go, though I would NOT advise doing it like this.
Control c = UserControlName.FindControl("controlToFindID")
NC...
 
0
NC01
2/27/2006 12:55:05 PM
Thanks for trying to help but you still don't get it!! :-)

That works fine if i do it from an aspx page with the 2 web user controls!
BUT what I want is from webusercontrolX.ascx get or set the controls and functions on webusercontrolY.ascx.
What you are suggesting is to do it from the aspx page with the 2 controls on it! I don't want to do that! I want to get or set the controls on on webusercontrol from another webusercontrol. My webusercontrolX.ascx doesn't know anyhting about webusercontrolY.ascx so on webusercontrolX.ascx I cannot do webusercontrolY.FindControl("ControlToFindID")
I hope you understand it now and thanks for trying to help.
0
Giorgio
2/27/2006 1:34:35 PM

Thanks for trying to help but you still don't get it!! :-) Oh I get it, you want some magical process to allow you to do something exactly the way that you want, LOL...
Think about it! As you said, User Control-X.ascx doesn't know anyhting about User Control-Y.ascx, so how can you possibly expect Control-X.ascx to be able to set anything in User Control-Y.ascx?
The only thing that knows about both is the parent, so you really have to do it there.
NC...
0
NC01
2/27/2006 3:09:36 PM

Well, I ws just trying to get some help and see if someone knows if it is possible to do this but obviously you didn't know and suggested me to do it on the page.
When I don't know I always research and if i can't find the answer I awlays ask. I don't go on about leading people to do things in a different way! If I don't know I won't say anyhting!
I just thought that might be a way on .NET 2 of doing this and was just asking if someone knows.
0
Giorgio
2/27/2006 3:19:42 PM

Well let me say it stronger: There is NO way other than what has been posted.
NC...
 
0
NC01
2/27/2006 4:25:00 PM
Acccording to you... but every developer thinks that knows everyhting and some always find a way of doing things others limit their knowledge themselves! Anyway, who are you to say that it's the only way? Are you a ASP.NET team member? Arer you a MCSD?

I'm sure that there's a way of doing what I want to do...
0
Giorgio
2/27/2006 4:58:46 PM

OK guys, let's take the heat out of this.
Giorgio, for the avoidance of doubt I am an MCSD.NET and have been building OO systems for the past 20 years. As you can see, I'm also a moderator here, so generally people think that I know what I'm talking about.
Sadly for you, Giorgio, there's no way of arbitrarily accessing a randomly (well not quite, but you know what I mean) generated object from another randomly generated object. How could there be? And two user control objects placed on the same page will have absolutely no knowledge of each other. How could they, without some programmer magic?
We can demonstrate this from your very question. How could NC01 answer your question (or how could you ask NC01 the question) without the warm embrace of the forum. You (and NC01) represent your user controls: the forum represents the page.
So how can you make two user controls work together?
Well, in the Page you can write code that will connect usercontrol a to usercontrol b. Remember, the page knows about the two user controls, so it can join them together (much like a priest/religious minister at a wedding joining two human objects in marriage). In this scenario, one of the user controls would implement a registration method (or use a delegate) which the other would be connected through.
Alternatively, one of the user controls can ask the page (through the FindControl() API) if there are any instances of the other user control and, having obtained a reference to it can then manipulate the other object.
Alternatively, a user control can place a reference to itself in a dictionary (keyed by the HttpContext object for the request) which the other can access. Now if this sounds like it doesn't rely on a middleman you'd be wrong - the dictionary is performing that role - plus it has horrendous implications for performance. I wouldn't touch this one with a barge pole (a long, long stick).
Or you could use a database to share data (as long as both randomly generated user controls can somehow agree what the key is), but that would be bye bye performance, and you'd need to think about how you would clear up the mess.
So that's pretty much your choices.
But if you ever find a way (that doesn't involve magic) please let us all know.

Regards

Dave
0
DMW
2/27/2006 5:41:41 PM

DMW: Thanks for the precise explanation!
Just to clarify, I have an MCAD and MCSD and have been building OOP applications since the early 70s.
I have also been working with ASP.NET since pre-beta (it was known as Active Server Pages+ back then), so, I might know a little of what I am talking about (I hope anyways).
NC...
0
NC01
2/27/2006 5:59:18 PM
Reply: