Access User Control From another User Control

 ASP.NET 2.0/VB.NET

I have a number of pages all with two user controls on them.  I need to access controls/properties from one user control from within another.  I've tried the following from one of the user controls:

Dim pg as Page = cType(Me.Page, Page)

pg.FindControl("myControl")  'Returns nothing

pg.form.findControl("myControl")  'Returns nothing

 

I also have public properties declared but I can't see those either (I also tried public shared property).  Can anyone help with the syntax?

 

thanks,

Scott

0
mystical_potato
7/6/2007 4:37:21 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

5 Replies
1090 Views

Similar Articles

[PageSpeed] 50
Get it on Google Play
Get it on Apple App Store

Hi, 

"myControl" - is it ID of your user control on the page?

-yuriy

0
ysw
7/6/2007 4:47:07 PM

Yes.  The pages look like the following:

 ASPX:

<%@ Page Language="VB" MasterPageFile="~/BAndC.master" AutoEventWireup="false" CodeFile="QACoordinator.aspx.vb" Inherits="Apps_QA_OCR_Queues_QACoordinator" title="QA Coordinator Queue" Theme="CVS"%>

<%@ Register Src="~/Apps/QA/OCR/CDWSearch.ascx" TagName="CDWSearch" TagPrefix="uctl" %>

<%@ Register Src="~/Apps/QA/OCR/CDWSearchResults.ascx" TagName="CDWSearchResults" TagPrefix="uctl" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cpLeftNav" Runat="Server">

<uc2:LeftNav ID="LeftNav1" runat="server" />

<uctl:CDWSearch ID="uct1" runat="server" />

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="cpMain" Runat="Server">

<uctl:CDWSearchResults ID="myControl" runat="server" />

</asp:Content>

From uct1:

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click

Dim p As Page = CType(Me.Page, Page)  'Get reference to aspx page

pg.FindControl("myControl")  'Returns nothing

pg.form.findControl("myControl")  'Returns nothing

End Sub
0
mystical_potato
7/6/2007 5:04:10 PM
You could expose a 2 public properties on the page that refer to the 2 controls.
Then use:

Page.Control1 from control2
and
Page.Control2 from control1

HTH,

Jonathan.
MCTS in ASP.NET
0
jonathanparker
7/6/2007 5:25:48 PM

 

Declare circular refences in two control.

//code of Control1

Control2 _C2;

public Control2 C2  { get { return _C2; } set { _C2 = value; } }

// code of Control2 

Control1 _C1;

public Control1 C1  { get { return _C1; } set { _C1 = value; } }

In the page load event of you web page, initialize these reference

webControl1.C2 = webControl2;

webControl2.C1 = webControl1;

 Now you can reference public properties of these control from using these references

 


Gursharn Singh


http://the-guy-wearing-turban.blogspot.com/


[Don't forget to click on Mark As Answer on the post that helped you ]
0
gursharnsingh
7/6/2007 7:03:07 PM

Hi,

Based on my understanding, you could use Me.Parent to get the Page object on which the WebUserControl nested in.

Please try the following code

WebUserControl1

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

WebUserControl2


<%@ Control Language="VB" AutoEventWireup="false" CodeFile="WebUserControl2.ascx.vb" Inherits="WebUserControl2" %>
<%@ Reference VirtualPath="~/WebUserControl.ascx" %>
<asp:Button ID="Button1" runat="server" Text="Button" />

Partial Class WebUserControl2
    Inherits System.Web.UI.UserControl
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
         End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim we As WebUserControl = CType(Me.Parent.FindControl("WebUserControl1"), WebUserControl)

        CType(we.FindControl("TextBox1"), TextBox).Text = "abc"

 

    End Sub
End Class

In the aspx page

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc2" %>

<%@ Register Src="WebUserControl2.ascx" TagName="WebUserControl2" TagPrefix="uc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;<uc1:WebUserControl2 ID="WebUserControl2_2" runat="server" />
        <uc2:WebUserControl ID="WebUserControl1" runat="server" />
   
    </div>
    </form>
</body>
</html>

Hope it helps,

Jessica


Jessica Cao
Sincerely,
Microsoft Online Community Support


“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
0
Jessica
7/10/2007 8:28:36 AM
Reply:

Similar Artilces:

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? 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 Using PropertiesIf the Sample.ascx control has two textboxes and submit button.You can access the valu...

How to access a user control from another user control in a Wizard control
My page has a wizard control and each step is a user control.  I could find the value of a control in step1 user control in the wizard page but not in step2 user control.From wizard.aspx.vb – it worksDim m_title As DropDownList = CType(Me.PersonalBox1.FindControl("ddlTitle1"), DropDownList) Question:How can I find the value of a control(textbox or dropdownlist)in step1 user control from step2 user control? I want to find “PersonalBox1.FindControl("ddlTitle1")” from Step2 AddressBox1. I have tried several ways to access it but no luck. From AddressBox.ascx.vb –...

Unable to access user control's user control's function\property from another user control
Hi, I used to call an user control's user control function as stated below from my user control in ASP.Net 1.1 wucCompany.wucEmployee.GetEmployeeSomething() After migrating to ASP.Net 2.0, I am unable to use any properties/functions(even the public ones) of the user control's User control's from another user control.  The way I have to do is create property\function in wucCompany which calls the wucEmployee's property\function and call the wucDepartment.GetEmployeeSomething().Since I need to do this change in too many places, I can...

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...

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...

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...

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 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 ...

How to access one user control from another user control on same page
hi,I have two usercontrol on same page. 1.ascx and 2.ascx on abc.aspx . Now I need to access dropdown on 1.ascx frm 2.ascx . Plz help me how can i do this?I have tried Page.findcontrol but as a user control inherits usercontrol class and not page class . its not giving reference to proeprty Page.Findcontrol. I can do page.findcontrol on aspx page but I need to do it on ascx only.plz helpregards,max   Hi Max, you have to expose your DropDownList as a public property which lays inside usercontrol 1. Example: 1 class MyUserControl1 : UserControl 2 { 3 4 p...

I have a web user control being used by another web user control, but the one being used will not display the labels in the resx file.
I am using DotNetNuke version 3.13 (might be 3.013) on a Window's XP machine, SP2. I have what I find to be an extremely odd situation. I have a project called TDS.Controls. This project is a web control library. Each control has its own resx file, which is located in the App_LocalResources directory. Each control inherits from Framework.UserControlBase, just like DotNetNuke's User control in DNN\Controls\User.ascx.vb. As a matter of fact, this control is doing the exact same thing I am trying to do. It is a web user control with its own resx file being used by another web user control (See ...

(How) Can i use the form elements of a user control from another user control???
Hi, i have a user control with 4 other user controls. Actually i have 4 panels and each user control is inside a panel. Its like a 4 page form which are all connected to each other. Is it possible to have access to web controls that i have in my user controls from the main user control. In my main user control i have a submit button and a tab list. User can browse through the tabs and each tab is bound to a panel. so clicking on tab1 makes panel1 visible which holds the first user control, second tab makes second panel visible and the rest invisible and the second panel holds the ...

Updating a Label control in one user control from another user control
Hi all, I have a main page in my music store site that has 5 user controls on it with 1 always showing main content and the others being things like a shopping cart, "best sellers", genre list etc (a la amazon). From the main content control I want to be able to set some label controls in my shopping basket control but I just can't seem to work out the syntax for how to do this: Here's the code I'm using - I'm trying to go to the 4th user control on the page (my shopping basket) then find the appropraite label, cast it locally as a Label then update the value: Label _so...

Accessing a user control's div tag from another user control
Hi,I have a form that has two placeholders- a primary and a secondary, for two different usercontrols. At runtime I unload one user control from the primary placeholder and load the second user control onto the secondary placeholder. Before doing the unloading and loading, I need to fill the innerhtml of a div tag in the second user control(the one I am going to load) from the first one(the one I am going to unload). I have been able to get a reference to the second usercontrol but dont know what to do next. Please advise. Your help is appreciated. Suppose you have two user ...

How do you access controls within a web user control from another page?
Hi guys/girls. Say for instance you have a web user control with 2 textboxes in it - txtBoxFirstname and txtBoxSurname. I place this web user control on a page. How do I access those controls from that page? Thanks guys this.Findcontrol("UserControlName"); Hi,You can make two properties on the user control that will expose the Text property of the textboxes and then from the page you'll be able to use them.For example in the user control:public string Text1{   get   {       return Textbox1.Text;   }   s...

Web resources about - Access User Control From another User Control - 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 ...

Out-of-control car hits Ballajura pedestrians and puts four teens in hospital
Four teenagers were injured in Ballajura on Saturday night when an out-of-control car careered into them.

Is Hillary Clinton sorry she reacted to the San Bernardino massacre with a call for more gun control ...
... "Gun safety" has traditionally meant handling your guns safely, but now it's supposed to mean what has more commonly been called "gun control." ...

more pics from Ground Control Touring 15th anniversary: Woods, Kurt Vile, Parquet Courts, Beach Fossils ...
... Parquet Courts / Woods & Kurt Vile / Beach Fossils @ Webster Hall 12/2/2015 We already posted a review and set of pictures from Ground Control ...

Obama renews call for gun control after mass shooting
In wake of San Bernardino attack, president, Republicans float their solutions to combat terror threats

EU could soon have border controls
EU ministers have agreed to press ahead with plans to suspend the Schengen passport-free travel zone, the FT reports.

Liberals Soft On Bomb Control? Are You Sure You Want To Go There, Marco?
... reports: Appearing on Fox & Friends , 2016 GOP presidential contender Marco Rubio hammered at Democrats for proposing additional gun control ...

Open thread for night owls: First U.S. birth control clinic lasted just 10 days before being raided
... Parenthood Only Lasted for 10 Days but Started a Revolution . An excerpt: During her time in Europe, Sanger had seen Norwegian birth control ...

Facebook Gives Users More Control Over Viral Content In News Feed
Facebook is updating its algorithm to give users more control over how high viral content appears in their News Feed.

BUSINESS IS BOOMING Firearm buys likely to spike amid gun control push
BUSINESS IS BOOMING Firearm buys likely to spike amid gun control push

Resources last updated: 12/6/2015 12:50:21 PM