SIGN UP MEMBER LOGIN:    
ARTICLE

Passing Values between User Controls and ASPX Page

Posted by Santhi Maadhaven Articles | WebForms Controls January 20, 2005
This article is for beginners who are learning. NET. This will be really helpful for them. Beginners will be wondered how to pass values between User Controls and ASPX page .In this article I have listed some of the ways through which you can access and set the User control values in an ASPX page.
Reader Level:

This article is for beginners who are learning. NET.This will be really helpful for them. Beginners will be wondered how to pass values between User Controls and ASPX page. In this article I have listed some of the ways through which you can access and set the User control values in an ASPX page. I started of with some of the basics, advantages and disadvantages of user controls.

User Control:

User Control is the custom, reusable controls. User Controls offers a way to partition and reuse User Interface (UI) functionality across ASP.NET Applications.

Advantages of User Control:

  • Code Reuse
  • Cache the output of the control independently using technique called fragment caching. This will improve performance if used appropriately.
  • Can program against any properties declared in the control, just like ASP.NET web controls.

Disadvantages of User Control:

  • User Controls can be instantiated in the pages that resides in the same web application. If you want to use across applications, you need to place same .ascx control

  • Can't hide code of user controls like server controls by compiling into an assembly.

Adding User Controls to an WebForms Page:

At the top of the .aspx page, add the below line above <Html> tag.

<%@ Register TagPrefix="Test" TagName="TestControl" Src="Test.ascx" %>

This directive registers the control so that it can be recognized when the page is processed. TagPrefix determines the unique namespace of the control, TagName is the name of the user control and Src is the path of the user control.

Declare user controls like

<Test:TestControl id="TestControl" runat="Server"/>

Accessing and Setting User Controls Values in the .aspx Page:

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 test.ascx control has two textboxes and submit button.You can access the values of the textboxes in the control from an .aspx page by declaring public property in the .ascx page.

public string FirstName
{
get{return txtFirstName.Text;}
set{txtFirstName.Text = value;}
}

In .aspx page,you can access FirstName using

TestControl.FirstName

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

TestControl.FirstName = "Suzzanne"

2) Using Javascript

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

document.forms[0]['TestControl:txtFirstName'].value ="Suzzanne";

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

document.forms[0]['TestControl:txtFirstName'].value

3) In ASPX code behind file

TestControl objTestControl = (TestControl)Page.FindControl("TestControl");
TextBox objTextBox = objTestControl.FindControl("txtFirstName");
string strFirstName = objTextBox.Text;

This will find the control named TestControl declared in the aspx page and look for the TextBox named txtFirstName in the user control and instantiate object of it. In this way also you can access the value of the user control.This is not recommended as it consumes more memory.

Login to add your contents and source code to this article
Article Extensions
Contents added by matt cupryk on Jun 07, 2011
Given TwoColumn.master that has a web control

<%@ Master Language="C#" MasterPageFile="~/MasterPages/Root.Master" AutoEventWireup="true"

CodeBehind="TwoColumn.master.cs" Inherits="OmegaLove.Web.MasterPages.TwoColumn" %>

<%@ Register src="../Controls/Search/ctrlSearch.ascx" tagname="ctrlSearch" tagprefix="Test" %>

<asp:Content ID="Content1" ContentPlaceHolderID="cph1" runat="server">

<asp:ContentPlaceHolder ID="cph2" runat="server">

<Test:ctrlSearch ID="ctrlSearch" runat="server" />

</asp:ContentPlaceHolder>

-----------------------------------------------------------------------------------------------------
In my web control ctrlSearch I declared the following in ctrlSearch.ascx.cs

namespace OmegaLove.Web.UI
{

public partial class ctrlSearch : OmegaLoveBasePageUserControl
{
public
string age1 // Dropdownlist is search_age_start
{
   get { return search_age_start.Text; }
  
set { search_age_start.Text = value; }
}
}

In the ctrlSearch UI page ascx I have the following button:

<asp:Button CssClass="button-search" ID="imgSearch" runat="server"
PostBackUrl="~/Secure/SearchResults.aspx" Font-Bold="True" />

when I post to SearchResults.aspx

I cannot retrieve the values:

-------------------------------------------------------------

namespace OmegaLove.Web.UI.Pages

{

public partial class SearchResults : OmegaLoveBasePage

{

public static string ConnnectionString = ConfigurationManager.ConnectionStrings["omegaloveConnectionString"].ToString();

private int counter = 0;

protected void Page_Load(object sender, EventArgs e)

{

ctrlSearch objTestControl = (ctrlSearch)Page.FindControl("ctrlSearch");  --> returns null

DropDownList objDropDownList = (DropDownList)objTestControl.FindControl("search_age_start");

Any help woud be great.

share this article :
post comment
 

Create a form in your code and you can past via get or post and do a request string in your aspx page.

Posted by matt cupryk Jun 07, 2011

I am trying to pass a variable in a web page using the param tag. What changes do I need to make in my c# user control so I can read this value from the same web page.

Posted by Chris Smith Dec 22, 2007
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor