Pass data from Web User Control to Web Form

I have a Web Form that contains a Web User Control.  The WUC displays dynamic content.  I want to change the TITLE property of the Web Form, based on the contents of the WUC.  For example, if the WUC displays information about Horses, I want to change the page title to "HORSES" by passing the string to the parent Web Form.

I have tried playing with parameters and such, but I can't remember how I did this in the past. PLEASE HELP!
~o0 Spin-Monkey Oo~
0
spinmonkey
5/21/2003 4:06:01 PM
asp.net.web-forms 93655 articles. 5 followers. Follow

13 Replies
1346 Views

Similar Articles

[PageSpeed] 26

The best way to do this is by creating a public property on your web user control and accessing that from your web form.  Check this out
--
Ambrose Little
Infragistics, Inc.
0
Ambrose
5/21/2003 4:08:52 PM
Yes, that is nice and all, but HOW DO I ACCESS the methods/parameters of the Web User control from the Web Form (or access the Web Form stuff from the Web User Control)?  You can not instantiate a Web User Control in the Web Form, BUT you can instantiate the Web Form from the Web User Control.

I conducted a simple test: In Visual Studio, I open the Web Form and drag the WUC into the design grid. The Web Form contains the property "TestMe" which contains a default value of "Original String". I instantiate the Web Form in the WUC and set the property value:
this.WebForm.TestMe = "Hello, World!"

Now, in the Web Form's HTML code, I type:
<% Response.Write(this.TESTME); %>
When I run the Web Form, the resulting string is "Original String".
~o0 Spin-Monkey Oo~
0
spinmonkey
5/21/2003 4:54:50 PM
I think it can be done easily using session variables 

u can use session variables in the web user control and change the session variables accordingly
and on the webform's On-prerender event grab the session value and display it as the title.
badri.
0
badinar
5/21/2003 4:57:27 PM
On the contrary, you can instantiate a web user control on a web form.  YOu can do this in two ways.  First, if you declare it in the ASPX, it will be parsed and instantiated by ASP.NET.  If you do it this way, you will need to add a member variable on the code-behind class of the user control's type, e.g., protected MyUserControlType myUserControl;.  ASP.NET will then set that identifier equal to the control it parses from the page.  The other way to instantiate a user control in a web form is by using the System.Web.UI.Page.LoadControl method.  In that case you would need to declare a variable of your user control's type and then set it equal to Page.LoadControl("virtualPathToYourUserControl"), e.g., MyUserControlType myUserControl = (MyUserControlType)Page.LoadControl("/controls/MyUserControl.ascx");.

You can then access the properties of the user control using the variable you've created in code, e.g., myUserControl.MyPublicProperty = "blah";. The properties you create can be used both to pass data into and to retrieve data from user controls.
I'd be fascinated to see how you create a web form inside of a user control. I don't think it's possible.

--
Ambrose Little
Infragistics, Inc.
0
Ambrose
5/21/2003 5:10:07 PM
Could you post an example similar to the simple example mentioned above.

I have seen many complicated examples and incomplete references to what could be done.
I would love to see a concise simple example.
Thanks
0
pspeth
10/23/2003 7:53:35 PM
Could you clarify what you're looking for?  Are you asking me for an example or one of the others?
--
Ambrose Little
Infragistics, Inc.
0
Ambrose
10/24/2003 2:34:53 PM
I am asking you.

I would like to see just a simple example, using Code Behind.
Thanks
0
pspeth
10/24/2003 4:46:48 PM
I assume you mean you want to see accessing a property on a user control from a web form?

Here it is in brief:
ASCX (WebUserControl1.ascx):

<%@ Control Language="c#" AutoEventWireup="false" Codebehind="WebUserControl1.ascx.cs" Inherits="Tester.WebUserControl1" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<asp:Label Runat="server" ID="Test" />

User Control Code Behind:

namespace Tester
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for WebUserControl1.
/// </summary>
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Label Test;
/// <summary>
/// Example String Property
/// </summary>
public string MyProperty
{
get
{
object o = ViewState["MyProperty"];
return (o == null) ? "Not Set": o.ToString();
}
set
{
ViewState["MyProperty"] = value;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
this.Test.Text = this.MyProperty;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

ASPX (WebForm2.aspx):

<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="Tester.WebForm2" %>
<%@ Register TagPrefix="uc1" TagName="WebUserControl1" Src="WebUserControl1.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<uc1:WebUserControl1 id="MyUC" runat="server"></uc1:WebUserControl1>
</form>
</body>
</HTML>

Web Form Code Behind:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Tester
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected WebUserControl1 MyUC;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
this.MyUC.MyProperty = "This is a test; this is only a test.";
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

Put these in the same web app dir and you should be good to go.

--
Ambrose Little
Infragistics, Inc.
0
Ambrose
10/24/2003 6:46:55 PM
I really hate to ask, but could you post the VB equivalent??

Thanks
0
pspeth
10/24/2003 7:54:00 PM
0
Ambrose
10/24/2003 8:26:04 PM
I have a text box on the user control and I need to pass the value from it to the container page. I believe a raise event is needed, but is there a simpler way?
0
troy23
10/24/2003 8:34:44 PM
Your example seems to be feeding a value to the user control property, but what about the user control sending a value to the container instead?
0
troy23
10/24/2003 8:36:28 PM
In that case, the get accessor for your property would return the value of the Text property of your TextBox.  The property simply provides the access (get/set) to any of your user control's data.

You could use an event as well, but generally those are used when the observer (in your case, the Page) needs to be notified when something happens on the subject (in your case the UC). That is, use events when you need to notify an external object that something has happened.
In any case, someone might suggest just using the Page property of the UC to access the page from the UC, but that is bad design because that creates an unknowable interface that consuming pages must adhere to.
--
Ambrose Little
Infragistics, Inc.
0
Ambrose
10/24/2003 10:08:35 PM
Reply:

Similar Artilces:

Web Form and Web User Control
Hello, I have a Web User Control (ascx) that includes a button. This button has an onclick event method. This Web User control is included into a Web Form. (thru LoadControl). However, when I click on the button, the event method is not executed. What should I include in order it works fine? Thanks in advance. GAA. Using Loadcontrol is using dynamic controls and when using dynamic controls you must recreate the same dynamic controls ono postback to econstruct the page control tree. It is best if you do this during the Page PreInit event. How complex are your controls ...

Passing Values from a Web Form to Another Web Form
Hello All! I am trying to pass values from one web form to another web form. I know that there are 3 ways of doing this: queryString, Server.Transfer and Session. I am using the Server.Transfer method. But I am not able to create, in my 2nd web form, an instance of my 1st web form. My 1st Web form is named CallTrack.aspx and my 2nd is named Export.aspx. What I mean is something like this: Public value as CallTrack. I can't create it in my 2nd web form because when the intellisense is displayed, there's no CallTrack in the options. Thanks!   How about using cross-...

How to access a control on a web form from anoter web form
hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it possible to access lbl1 from form2, change its text permenantly? thanks.   refer: http://dotnetslackers.com/Community/blogs/haissam/archive/2007/11/26/ways-to-pass-data-between-webforms.aspxSSNPlease remember to click "Mark as Answer" on the post that helps you. MinaSamy: hi all I have this scenarios i have two web forms, one called form1 and the other called form2 form1 has a label with id "lbl1" is it p...

Web User Control Not rendering in Web Form
I have created a Web User control that is made up of a Multiview with 2 views inside the Multiview. I have buttons on both views and a gridview on 1 view. My question is when I drop this Web User Control on a Web Form all I see is [MultiView"MultiView1"] and when I view the from in the browser I dont see the Web User Control. I was wondering if someone could please tell me why the Web User Control is not displaying or rendering.     Thanks In Advance Hi, Please check your aspx file for correct reference, like following example: TestControl.ascx:<%@ Control ...

custom web user control and web form
Guys,          I am trying to develop a context menu control for all of my pages. what i am trying to do is that i want the control to pick a datasource from the markup and then check whether its an xml file or a datatable. I created a class called datasource.   /**************************DataSource Class****************************/  public class DataSource { /// <summary> /// This is to identify unique elements from the markup /// </summary>protected string _ID; /// <summary> /// This will be the Datasource ID e...

Submitting data from a know web form to unknown web form
hi i m making a software in which i will submitt some data from a web page that will go to a web form of which contrlos are unknown to me only i know the name and how many controls in that form it means i can see the form only. how can i do this please help me soon with code plz its very argent.i m doin this wit the help of foxtrot.xray.WebRobot but its giving me error that this dll file is already exists in The file ('C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\_foxtrot.xray.WebRobot.dll' already exists).i don know what will do so please help me soon please thanks praveen We ca...

web web web
Name: bahadir Email: sensiz_olmuyor_t1_at_hotmail.com Product: Firefox 2 Beta 2 Summary: web web web Comments: web sayfası yapmak Browser Details: Mozilla/5.0 (Windows; U; Windows NT 5.1; tr; rv:1.8.1b2) Gecko/20060821 Firefox/2.0b2 ...

passing data from user control to web form
I have a user control, with some text boxes. in my webform, I am dynamically loading the control - Dim uc As Control = Page.LoadControl("moreemployers.ascx") ' move control to placeholder. place1.Controls.Add(uc) I am using the following to define the text fields in the user controls. Public Property employername() Set(ByVal Value) txtename.Text = Value End Set Get Return txtename.Text End Get End Property how can I access these variables in the web form? bevbev if y...

Web forms and mobile web forms
Hey everyone, I am looking to develope a web site that depending on the device connecting to it, it will display a certain page. In this case im looking to create a web and wap version of the same project. I have the Web forms version finished using ASP.NET and C# and I am wondering how to create the mobile web form and incorporate it in the project. In the project folder, can I just add a mobile web??? But what about displaying device specific pages?? Cheers -Dudach Yes, you can add mobile web forms and controls to your Web Application project. No problem. For device/br...

How to Make Web Forms from other Web Forms
In putting together an ASP.NET application for a biz application, we have come to the point where there are three different types of basic forms which have, for the most part, been perfected. Now its is a matter of cloning those forms for various uses and modifying or adding code to suit the particular need. Copy and paste works OK, but there is a lot of changing of certain words, etc. Is there a way in ASP.NET to take these basic forms, turn them into classes or templates, create forms from them, then add or overwrite code? ? (Anybody who is a Vis Fox Pro programmer would know imme...

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

Interaction between Web User Control & Web form
Ok, I want to create my first Web User Control which is used for file uploading. It's very simple, consist only of the <asp:FileUpload> and <asp:Button> control. The button has one server side event which is resizing the uploaded image to two different images (stored as byte[] in the cs file). So, my question is how I can send these two arrays of type byte to the Web form where the web user control is inside. I thought it should be possible to write something like this in the Web form:<uc1:FileUploader ID="uploader" runat="server" OnUploaded="recieveImages" />So that means ...

validate Textbox in Web User Control from web form?
ASP.NET 2.0 I have a web user control that contains a textbox.  I need to add some validation (like requiredfieldvalidator, comparevalidator, etc). I would normally add the validator to my web user control by placing the validator next to my textbox.  However, I need the validation message somewhere else on my web form, not near the user control. So how would I associate a validation control on my web form with the text box inside my web user control?  Can this be done; any alternatives out there? Thanks!   The error message should occur wherever you place the vali...

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

Web resources about - Pass data from Web User Control to Web Form - 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 ...

TERROR IN JAKARTA: 'Situation under control' after explosions, gunfire erupt in Indonesian capital
At least six people are dead, including three police officers, after several explosions at the Sarinah shopping mall in an apparent multi-site ...

DFES downgrades bushfire in Dunsborough as it is contained, but not under control
A BUSHFIRE south of Dunsborough has been contained but is not yet under control.

WA fires: threatening Jindalee blaze brought under control
A fire that was at one stage a threat to lives on homes in Jindalee on Thursday has been brought under control.

Queensland police shut down out-of-control party in Berserker, Rockhampton
Police shut down a party of 150 young people who had gathered at a house in Berserker, Rockhampton on Friday night.

BMW recalls i8 for stability control problem
... , BMW , Safety , Coupe , Hybrid , Performance BMW will recall a small number of 2015 i8s because of a problem with the dynamic stability control ...

ESI cost control and networks
There is an interesting discussion thread on Twitter about how the way Medicare for All saves money is by significantly reducing provider, device, ...

While No One Was Watching, Obama Took Control of the Courts
... and President Obama’s legacy.” It didn’t favor conservatives but there was a dramatic shift to the far left. The appellate is under the control ...

Apple has landed a very rare sponsorship deal to ensure it has control of the hottest ticket in San Francisco ...
Apple has signed up for the first time to become a sponsor of the Super Bowl Host Committee, Daniel Roberts at Yahoo Finance reported. It's ...

Leonardo DiCaprio: The Oscar stuff is ‘completely beyond my control at this point’
... last night’s London premiere of The Revenant . Leo is looking pretty good these days. He’s de-bloated, his facial hair is very much under control, ...

Resources last updated: 1/17/2016 12:21:29 AM