Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

I decided to write this example because I always encounter this type of questions at the asp.net WebParts and Personalization forums.

Many of us have used UserControls as WebParts so that we can incorporate different server controls in a single WebPart element and there are times we need to retrieve or pass a value to a Control that‘s inside a UserControl.

So in this example, I will show different ways on  how to get or pass values to a control that is residing within a UserControl.

Suppose that we have this UserControl declaration below:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl1.ascx.cs" Inherits="WebUserControl1" %>

<asp:TextBox ID="TextBox1" runat="server"/>

<br /><br />

<asp:DropDownList ID="DropDownList1" runat="server">

</asp:DropDownList>

 

From above, you can see a TextBox and a DropDownList control in the UserControl.

Now, lets try to add that UserControl in the WebPartZone by dropping it to the WebPartZone. See the ASPX markup below:

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

 

<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" 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>WebParts and UserControls Demo</title>

</head>

<body>

    <form id="form1" runat="server">

   

    <asp:WebPartManager ID="WebPartManager1" runat="server">

    </asp:WebPartManager>

    <asp:WebPartZone ID="WebPartZone1" runat="server">

        <ZoneTemplate>

            <uc1:WebUserControl1 ID="WebUserControl1" runat="server" />

        </ZoneTemplate>

    </asp:WebPartZone>

    <asp:WebPartZone ID="WebPartZone2" runat="server">

 

    </asp:WebPartZone>

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Pass Data" />

    <asp:Button ID="Button2" runat="server" Text="Get Data"

        onclick="Button2_Click" />

    </form>

</body>

</html>

 

Now let’s pass a value to a TextBox and DropDownList controls that is residing from a UserControl when the Button is clicked.

Option 1: Using the FindControl method

The first option is using a FindControl method. As you may know, FindControl method is the general way for accessing Controls in the page.

Here’s the code block below:

protected void Button1_Click(object sender, EventArgs e)

{

        if (WebPartManager1.WebParts.Count > 0)

        {

            //Extract the WebPart element (UserControl)

            WebPart wp = (WebPart)WebPartManager1.WebParts["WebUserControl1"];

            //Extract the TextBox Control that is residing from a WebPart

            TextBox tb = (TextBox)wp.Controls[0].FindControl("TextBox1");

            if (tb != null)

            {

                tb.Text = "Hello ASPNET!";

            }

 

            //Extract the TextBox Control that is residing from a WebPart

            DropDownList ddl = (DropDownList)wp.Controls[0].FindControl("DropDownList1");

            if (ddl != null)

            {

                ddl.Items.Add("Item 1");

                ddl.Items.Add("Item 2");

                ddl.Items.Add("Item 3");

            }

        }

}

 

As you can see, the code above was pretty straight forward.  You can use the above approach if you wan’t to manipulate the controls that you are accessing, like for instance in the above example we are accessing the DropDownList control and then add some items to it.

Running the code above will show something like this below:


Now here’s the output below after pressing the button “Pass Data”


Here’s the code block below for getting the data from the UserControl:

protected void Button2_Click(object sender, EventArgs e)

{

        if (WebPartManager1.WebParts.Count > 0)

        {

            //Extract the WebPart element (UserControl)

            WebPart wp = (WebPart)WebPartManager1.WebParts["WebUserControl1"];

            //Extract the TextBox Control that is residing from a WebPart

            TextBox tb = (TextBox)wp.Controls[0].FindControl("TextBox1");

            if (tb != null)

            {

                Response.Write(tb.Text + "<BR/>");

            }

 

            //Extract the TextBox Control that is residing from a WebPart

            DropDownList ddl = (DropDownList)wp.Controls[0].FindControl("DropDownList1");

            if (ddl != null)

            {

                Response.Write(ddl.SelectedItem.Text);

            }

        }

}

 

The output would look something like this after pressing the “Get Data” button.

 

Option 2:  Using Properties

This option is the easiest and efficient way to pass and retrieve values from a Control that is residing from a UserControl. All you need here is to expose a public property to a UserControl .ASCX page.

Here’s an example:

public partial class WebUserControl1 : System.Web.UI.UserControl

{

 

    public string TextValue

    {

        get { return TextBox1.Text; }

        set { TextBox1.Text = value; }

    }

 

 

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

 

}

 

Now at Button Click event in the ASPX page, you can pass a value to the TextBox control this way:

protected void Button1_Click(object sender, EventArgs e)

{

        WebUserControl1.TextValue = "Hello There";

}

 

Here’s the page output below after pressing the “Pass Data” button.

 

Here’s the code for getting the value from the TextBox:

protected void Button2_Click(object sender, EventArgs e)

{

        string str = WebUserControl1.TextValue;

        Response.Write(str);

}

 

As you can see, the code above was pretty simple!

Conclusion:

Use Option 1, if you are going to manipulate the control’s property that you are accessing. If you are just passing and retrieving controls value from the UserControl then use Option 2 instead.

That’s it! I hope you will find this example useful!

 

Technorati Tags: ,,
Posted on Thursday, June 25, 2009 1:17 AM ASP.NET , WebParts , C# | Back to top


Comments on this post: ASPNET WebPart FAQ: Accessing Controls from UserControls in ASPX page

# re: ASPNET WebPart FAQ: Accessing Controls from UserControls in ASPX page
Requesting Gravatar...
How about if the buttons are inside the usercontrol? How can I pass a value from inside the usercontrol (like a row command argument), by clicking the row of a gridview for example) to a textbox that resides on the aspx page?
Left by Jay G on Jan 26, 2010 6:23 AM

# re: ASPNET WebPart FAQ: Accessing Controls from UserControls in ASPX page
Requesting Gravatar...
This is exactly the answer I was looking for. Thanks.
Left by Mike Hanson on Feb 06, 2010 2:48 AM

# re: ASPNET WebPart FAQ: Accessing Controls from UserControls in ASPX page
Requesting Gravatar...
Excellent! very easy to understand and useful information.
Left by Shital on Dec 06, 2011 4:55 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net