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. 5 followers. Follow

12 Replies
1551 Views

Similar Articles

[PageSpeed] 21

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:

Similar Artilces:

Web User Control to Web User Control...
Hi, I have 2 User controls on a page and want Control1 to call a method of Control2. Is this Possible? Example: Con1 = Datalist showing products, each with a Quantity Box and a Button. Con2 = Repeater showing Cart Preview (name, Price) in page.aspx. When I click on the button on the DataList in Con1 it adds the item into the Session based Cart. However it does not update the Cart Preview. I need to call BindPreview() in Con2 in order to update the Cart Preview... Does that make sense? argh! any help much appreciated! PeteWeb Design Nottingham, UK Well I can call BindPreview() from m...

web controls and web user control
i have a problem in a web form , I have a webform that contains a web user control which in turn contains a html input form ,the web control events don't fire up ,When I click button ,no effect ,I don't know what is the effect of the html form which is in the web user control on the web controls in the web form ?? can any one help me the web control event should be handle in the code behind page, is the event handler for the button you are clicking registered? You can try double clicking the button, should give you the default btn_click event handler in code behind. Also, make ...

How to access individual controls from a form containing the web user control containing those controls
 Below is a block of code in my .aspx form named default.aspx. In this form I load a web user control. I then add it to a place holder control.(Lines 3 and 4 below) So how do I access the individual controls in my web user control? I want to set the properties on those controls, but I am baffled as to how to access the control. For example I have a text label control named "label3". How do I access this control. Because for example, under certain situations I want to hide that control. ( Label3.Visible = false;)  1    if(!Page.IsPostBack) 2&nb...

Persisting data in a web user control, which contains a list of other web user controls.
 Hi All, I have a control I've created, which can have a number of child controls - also user controls - stored as an ArrayList, which can be added wither a design time or programmatically.My problem is when I add to the list programmatically, it doesn't persist them. So if I add 3 child controls in step 1 of a wizard, with the aim that those three will be visible inside the parent in step 3, what happens is that the parent has no child controls and the item count is 0.Can anyone help? Thanks in advance.Code:Parent Control Markup: <%@ Control Language="C#" Auto...

User Control Within UserControl
If I have a user control A and then inside it a user control B embedded/referenced within it.  User Control A has a panel defined (cpeNewUser) that I want to manipulate from the code behind of User Control B based on some event that happens.So putting this in my code behind of User Control B won't work as it has no idea of cpeNewUser:cpeNewUser.Collapsed = true;So is it because only at runtime they are married up and because of this you cannot access each other's objects at compile time?  How would I get this to work becuase I'm using all user controls inside a AJAX Too...

Problem in accessing web form data in user controls when create user control dynamically
HI. I have a problem using web user control. I designed a user control named NewsForm. This user control contains some web form elements like <asp:textbox>. When I add it in an .ASPX page, all things may be true. I can know which user filles in textbox (for example and other controls) <%@ Register TagPrefix="uc1" TagName="NewsForm" Src="NewsForm.ascx" %> and <UC1:NewsForm id="newsForm" runat="server"></UC1:NewsForm> But when i create it in code behined dynamically, however user control has web form controls but their value are blank for example //aspx co...

Web user control inside update panel replace with another web user control
 Hi there. I have a page with a menu. an and then the content. My thing is. That i have the update panel, and inside that i have a regual panel in which i add the Web User Controls i need. Which all works fine. My problem is. That i i have a submit funcion on almoast all the Web User controls. What i would like is that i inside that update panel, can dispose the original Web user Control and then add a response Web User Control. In normal language. Is there a way that i can load one web user control into antoher and then disposose the original? Thanks in advance hello. well, what ...

change layout of web controls in web user control
Hi I have create a user control with 3 asp:textbox in it. I would like to reuse the same 3 textboxes in 2 seperate web pages. In one case I want them to appear in  a row (as search parameters) and in the other I want them to appear in a column(to add a new object)..  Can I do change layouts using CSS or something? Hi,    Here is a solution to your question for your reference. Create a web user control as the following <%@ Control Language="C#" AutoEventWireup="true" CodeFile="DynamicalUserControl.ascx.cs" Inherits="DynamicalUserControl" %><asp:Panel ID="...

Server Web User Control or Web Custom Control
Ok, I made a DataGrid I like, stuck it in a Web Control Lib, and then to my toolbar. Now I want to take my nice little control and add it to a greater control, maybe place some buttons around it etc. I could always inherit from it in a new custom control and dynamically add the buttons, but I thought about using a Web User Control. I thought I could easily create a Web User Control, drop on my grid and the buttons on a form, and expose some properties and events, but once I got into it, I got a feeling that this isn't what a Web User Control is for. When designing a control li...

What is the difference between a Web User Control and a Web Custom Control
Hi ,please could you answer for the following question with example. i know wht they are. but i want to know hw they are existing in web pages with simple example. What is the difference between a Web User Control and a Web Custom Control?  Thanks in AdvanceDilip    Check this out .... hope these might help you out... http://support.microsoft.com/kb/893667http://msdn2.microsoft.com/en-us/library/aa651710(VS.71).aspx itsUrDAY  Hi, See the following URLs: https://msdn2.microsoft.com/en-us/library/fb3w5b53(VS.71).aspx(Introduction to Web User Contr...

Use Web Custom control or Web User control?
Friends,  I have a generic aspx page and its functions which is used in many .NET web application. The generic page contains a HTML Div control, server drop down list, server buttons, server textbox and it have different server and client functions to do its tasks for calling aspx pages in web applications. My problem is I do not know whether I should make that generic page as a web user control (but this is tedious because all other application folders must store frontpage aspx and code behind files of the generic page) or as a web custom control (This seems to be good beca...

Single page with multiple user web controls VS Multiple page with specific user web controls
I have a question regarding single page with multiple user web controls vs multiple page with specific user web controls   Option 1: I am of the opinion to have multiple pages with specific user web controls and use an object to store all the information as the user navigates through the application. And finally at the end, read this object and save all the information to the backend database.   Option 2: My respected team member suggests that we have only one page, with all the specific user web controls and not use an object, instead show/hide the user web controls based ...

Access SqlDataSource on Web Form from a Web User Control...
Hello, I want to access a SqlDataSource on a Web Form fromout. a Web User Control. To do this, I have in my code behind of the Web User Control the folowing code: DropDownList1.DataSource = (this.Page.FindControl("SqlDataSource3") as SqlDataSource);DropDownList1.DataTextField = "Name;DropDownList1.DataValueField = "NameIDropDownList1.DataBind(); But, the control (ic. SqlDataSource3) is not found. Even other controls like labels and buttons are not found with the FindControl. What am I doing wrong? Thanks Andromedaris     This is not much of a surprise,...

Access a Web User Control Method from a Web Form
I'm trying to pass the Selected Index value from a web user control in a web form to the user controls selectedIndexchanged Sub.  I can't seem to figure out how to pass the information that I need to the user control and have it execute the sub routine. In the SelectedInedxChanged Sub I have a switch Statement and I want to get the index value from the web form where the user control is located and compare it to each case and then to have it execute the code for the case that it matches. What do I need to do? Hi, I am not sure if I understood your question correctly. I su...

Web resources about - access controls on web user control x from web user control Y - asp.net.web-forms

Control - Wikipedia, the free encyclopedia
Text is available under the Creative Commons Attribution-ShareAlike License ;additional terms may apply. By using this site, you agree to the ...

WA bushfires: Firefighters work through the night to control several blazes
Firefighters in Western Australia are working through the night to control and contain several blazes.

MAZEL TOV, HILLARY: Clinton Wins Gun Control Award From Major Anti-Bill of Rights Group
... restrictions on the freedoms protected by the Constitution is not a sign of character, but a clear indication of blind adherence to a gun control ...

Brussels Pushes Gun Control In Wake of Paris Attacks
Europeans double down on gun control after terrorists murder defenseless civilians

The Legend of Zelda: Twilight Princess Wii U will likely support Wiimote and Nunchuk controls
Well this is interesting. Fans have been speculating as to the control schemes that will be supported in The Legend of Zelda: Twilight Princess ...

Control Your HTPC With Scavenged IR Parts
If you’ve built yourself a home theater PC, one of your highest priorities is probably coming up with a convenient control solution. The easiest ...

ICYMI: Even the AP couldn’t validate any of Hillary’s gun control nonsense
... – not 200 as she claimed . Asked to explain the discrepancy, Clinton’s campaign pointed to 2013 statistics from the Centers for Disease Control ...

5 Questions: Punjab’s rightful control over Chandigarh, says Prem Singh Chandumajra
Shiromani Akali Dal (Badal) MP Prem Singh Chandumajra on why he raised the issue of Punjab’s claim over Chandigarh in LS.

Over-The-Counter Birth Control Empowers Women And Pharmacists In Oregon And California
Over-the-counter birth control is coming to Oregon and California. There’s an enactment of new laws on the horizon that will allow pharmacists ...

In this Dark Fairy Tale, a Sheltered Young Girl Seizes Control of Her Future
“In a family headed by a tyrannical father, the younger daughter will regain her freedom on her birthday.” It’s a simple synopsis, but Blood ...

Resources last updated: 11/26/2015 11:25:13 AM