How do I control one control from another control?

I have a page page1.aspx which is within a master page.

On page I have a table with two cells. Within each cell I have placed two controls (ascx) that I have developed.

Now I'd like to be able to get these two controls to interact. One needs to update the other. One has some search criteria that the other one needs to update its gridview. One needs to make the other visible/not visible at certain times. Out of the box neither control can see each other at all.

Is there a good article or someplace I can find some information on how to do this?

 


AllAboutFocus.com.
Information Technology Solutions
Helping You Focus. Helping You Succeed.
0
patrick24601
3/22/2007 11:15:47 PM
asp.net.navigation-controls 13714 articles. 0 followers. Follow

6 Replies
1681 Views

Similar Articles

[PageSpeed] 3

Here's a hint for you... ascx user controls are self-contained objects that hide their contents from other applications.  When you build them, you will need to place in properties and methods are are accessable from an outside.

Here is a down-and-dirty example

Create a usercontrol. name it HelloOutsideWorld.ascx with the following contents:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="HelloOutsideWorld.ascx.cs" Inherits="HelloOutsideWorld" %>
<asp:Label ID="lblMessage" runat="server" Text="Awaiting Message From The Outside..."></asp:Label>

 Here is the codebehind for HelloOutsideWorld.ascx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class HelloOutsideWorld : System.Web.UI.UserControl
{
    public string OutsideMessage
    {
        get { return this.lblMessage.Text; }
        set { this.lblMessage.Text = value; }
    }


    
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

 

Now create a Default.aspx file and put this in its source:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<%@ Register Src="HelloOutsideWorld.ascx" TagName="HelloOutsideWorld" 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>
        <uc1:HelloOutsideWorld ID="HelloOutsideWorld1" runat="server" />
        <br />
        <br />
        <asp:TextBox ID="txtLeaveAMessage" runat="server"></asp:TextBox>
        <asp:Button ID="btnSend" runat="server" Text="Send a message" /></div>
    </form>
</body>
</html>

 And in the codebehind named Default.aspx.cs place this content:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            if (txtLeaveAMessage.Text != string.Empty)
                HelloOutsideWorld1.OutsideMessage = txtLeaveAMessage.Text;
        }
    }
}

 

Basically, the form on the Default page passes on the message to the usercontrol's publicly accessable property called OutsideMessage, and the user control passes it onto lblMessage.Text for you.

So as you can see, the controls within the usercontrol are only changeable by the usercontrol itself.  Its like the old joke: How many Object Oriented Programmers does it take to change a lightbulb? 

Answer: None... the lightbulb changes itself

Alternate answer: None, Microsoft changed the standard to Dark.


"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

http://pixelsyndicate.com/ps/
0
pixelsyndicate
3/23/2007 2:11:55 AM
This is very cool - marked as answer. Thanks.

AllAboutFocus.com.
Information Technology Solutions
Helping You Focus. Helping You Succeed.
0
patrick24601
3/23/2007 3:23:46 AM
Follow up - so if I want two controls on the page to talk to each other I just have to expose public properties for them and then use the parent page (default.aspx) to pass information back and forth? 

AllAboutFocus.com.
Information Technology Solutions
Helping You Focus. Helping You Succeed.
0
patrick24601
3/23/2007 4:14:47 AM

patrick24601:
Follow up - so if I want two controls on the page to talk to each other I just have to expose public properties for them and then use the parent page (default.aspx) to pass information back and forth?

If you wanted to do it the right way, you would have events, delegates and have your main page subscribe to the events that bubble up from the usercontrols...

here is a link to Event Bubbling (asp.net 1.1)
http://www.odetocode.com/Article94.aspx


"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

http://pixelsyndicate.com/ps/
0
pixelsyndicate
3/23/2007 1:14:42 PM
Thanks. Do you have a good working link ? :-)

AllAboutFocus.com.
Information Technology Solutions
Helping You Focus. Helping You Succeed.
0
patrick24601
3/23/2007 1:43:47 PM

patrick24601:
Thanks. Do you have a good working link ? :-)

Geeze. sorry, i dont know what happened to the s/ in it!

http://www.odetocode.com/Articles/94.aspx

 


"A common mistake that people make when trying to design something completely foolproof was to underestimate the ingenuity of complete fools." ~ Douglas Adams

http://pixelsyndicate.com/ps/
0
pixelsyndicate
3/23/2007 2:10:04 PM
Reply:

Similar Artilces:

Trying to load a control into a control from another control
Exactly as the subject states, I'm trying to load a control into another control from a control loaded into a page, I keep getting this error from the code I have below. I've also tried the findcontrol but it keeps coming up null. (67): 'System.Web.UI.UserControl' denotes a 'class' where a 'variable' was expected UserControl ut = ((webcontrols.BrowseItems)UserControl).LoadControl("BrowseItems"); ((webcontrols.BrowseItems)UserControl).pnl_browse_main.Controls.Add(ut); This is how we Load User controls into a content place holder. try { UserCont...

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

Custom Control within a Control Control
I am developing a custom controls that will render headers for different web sites. Within these controls, I want to place another custom control that will change the language. I created the language control and had it implement IPostBackDataHandler. This control works fine if I drag it on a new web form, but if I tell a header custom control to render it problems occur. The Language Selector custom control does not have a Page member anymore and the event handlers do not work. Does anyone have any experience with doing something similiar? Anyone have any suggestions? Thanks, ...

Controls.Add in Composite Control that inherits another control
Hi! I created a composite control that inherits from the WebControls.Button. I added the following in the Load event:         Dim mHidden As New WebControls.HiddenField        Me.Controls.Add(mHidden) but the control is not created. Why?Dimitris PapadimitriouSoftware Development Professional Hi,Child controls on Button do not make much sense (how would you expect them to be rendered, doesn't button allow only text into value attribute?). Button allows child controls to be added, but they won't be rendered b...

want to call one c#.net function in one user control from another user control
hi i have two user controls info.ascx and personal.ascx i have a function like in info.ascx i have a function like showdetails() {   } now i want to access this function from personal.ascx can i do this ? thanks in advance. shwetaThanksshwetaIn a day when don't come across any problem -you can be sure that you are traveling in wrong path  you should better create a class where you will define the "functions" that you need in both files. declare "static" the function and call it like nameofclass.nameoffunction In a web project you can...

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

How do you control the placement of controls in a user define control
I have a user define control.  I put the control on the (C# Visual Studio 2008) page where I want them. When I run the program, the controls are all over the page.  How do I place them where they do not move form where I put them? You'll need to study the HTML output and see what it's producing, then start to look at how to do an HTML layout. hi, there are absolute and Relative position in html. I guess you must use relative position. For example, at design-time, you place a TextBox below an Image, at run-time, no matter what size is the image, the TextBox is alway bel...

Control as Property to another Control
How do I add a property to a server control that has the type of another server control? I have tool controls, ControlA and ControlB. ControlA has a property called ControlBProperty. However, the property does not appear in the HTML and when I build, the ControlBProperty returns to blank (doesn't persist). Note: the ControlB has a TypeConverter called ControlBConverter, so it is able to convert between the ControlB type and a string. The string return in the TypeConverter is the ID of the ControlB....

Access control from another control
Tried to adjust the sample here for my own uses... http://forums.asp.net/779045/ShowPost.aspx But, doesn't want to cooperate. I have a dropdown list on a user control (header.ascx), autopostback = true public string sStyleSheet; private void Page_Load(object sender, System.EventArgs e) { sStyleSheet = "includes/Styles.css"; //Allows the user to change style sheets for the site } public event EventHandler isClicked; private void ddlStyles_SelectedIndexChanged(object sender, System.EventArgs e) { if(ddlStyles.SelectedIndex == 0) sStyleSheet = "Styles.css"; else if(ddlStyles.Sele...

Controller inside another controller...
Sorry, I dont know if this make any sense...My doubt is, suppose  that I have a CourseController, and there is two types of courses.. public and private...If I wanna have a CourseController, with action List, to list both types of courses.. And, route /course/public/{index,save,edit,delete} to public courses, and route /course/private/{index,save,edit,delete}...Is this possible to do? Or I have to create CoursePublicController and CoursePrivateController?How do I model this?I tried to look at ASP.NET MVC Controller Tutorials.. but its all basics...  Thanks.. It could be don...

Error using the PopupControlExtender control when referencing controls that are contained within another control...
I'm running into an issue when using the PopupControlExtender control. It appears the if the TargetControlID or control you are targeting is inside another ASP.NET control you can not access the control b/c the ID of the control when the page renders is differnt. It appears that you also can not use the <%# control.UniqueID %> method b/c the PopupControlExtender does not support DataBinding. An example of my problem...I have a wizard control. Inside that wizard control is a textbox called TextBox1. I want to add a popup calendar to that textbox by using the PopupControlExtender contro...

How to visible an control below the control
hi friends i Need all your help In a form i placed an button. On the click event of button i returned an function to show an calendar And i placed dropdown control below the button. When i click the button the was calendar shown on that time dropdown was invisible What i want is to show the calendar as well as the dropdown control in the form.  waiting for you reply thanks santhosh Inside the click event: dropdownlist1.visible="true"David WierMCP/ASPInsiderASPNet101.com - where to look first!Please Vote for ASPNet101 - 'Best Community Resource'!Contro...

More Birth Control Controls
they just can't stop http://www.aclu.org/blog/reproductive-freedom/use-birth-control-youre-fired <quote> First, a bill that gives immunity to doctors who lie to couples about the results of their prenatal tests in order to prevent them from getting an abortion. Now, a bill that would give your boss the green light to fire you for using birth control. You think I am kidding? I wish. For a decade now, Arizona insurance companies have been required to provide coverage for contraception just like other prescriptions. But, because they saw an opening to score some polit...

Visibility control of Panel control and its affect on data controls
Thought I would toss this out here this morning and perhaps someone else had run across the same issue and had found a good way to accomplish what I want. First let me state what I'm attempting to do and the problem that I've run into.  GridView1 has a collection of rows in a SQL table that I want to present to the user. I've provided them the options of sorting and selecting only from the GridView control.  Orgionally, I had created a detail view that was being presented in Edit mode and then customized the layout of the edit item template to suit my needs.&nbs...

Web resources about - How do I control one control from another control? - asp.net.navigation-controls

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

China lays out its vision of the internet: more control, more censorship
... censorship of the internet, in a high-profile speech underscoring China's increasingly emphatic attempts to justify its strict online control. ...

Stringify your life by taking control and automating tasks
... Adobe Post or If you can think it, you can create it in Scribblenauts Unlimited . Stringify IF by IFTTT Stringify your life by taking control ...

How to engage Tesla's Launch Control mode
Filed under: Green , Videos , Tesla , Electric Short video shows Model S keeping it straight on a rain-slicked road.

Nuance’s voice control going where Siri, Google Now and Cortana fall short
... for Samsung's S Voice and Apple’s Siri, is ready to go where no other digital assistant has gone before. The company wants to bring voice control ...

EU Acts on Refugees, Terrorism With Tougher Border Controls
Beset by the refugee crisis and fears of terrorism, the European Union floated plans to stiffen its external border controls for Europeans and ...

Apple is about to take even more control
... be about to get tough for some Apple partners as the company invests in top secret display technology development labs and increases its control ...

Google Updates BigQuery With New Cost Controls
Enterprises using BigQuery to run analytics against large data sets can now set a daily query quota on a project-wide or an individual basis. ...

Cancer May Be More Within Our Control Than We Thought
Some cancers may be due to 'bad luck.' But even more of the risk may come from the choices we make every day.

After losing control of Parliament, Venezuelan socialists create a new one
After losing control of Parliament, Venezuelan socialists create a new one

Resources last updated: 12/17/2015 7:21:18 PM