Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Access One User Control To Another in ASP.NET

Article:
Viewed:  1116 
Posted On:  24/09/2015 10:33:05 
How we can access one web user control in another web user control in ASP.NET? 

I am writing this article as one of my friend asked me to write on this topic. So I can say it's an On Demand Article.

Here I am going to show how we can access one Web User Control (.ascx) control value in other Web user Control.

My Scenario is  I have a drop down list on One Web user Control and on button click I am going to show this Drop Down selected value in other Web User control.

So Open Visual Studio -> File-> New -> Web Site

1.png

Image 1.

Now I will add 2 Web user Control. So Right Click on Project's Solution Explorer-> Add-> Web User Control.

2.png

Image 2.

3.png

Image 3.

Add another web user control

4.png

Image 4.

Now Open WebUserControl1.ascx and below code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="AccessUserControlToAnother.WebUserControl1" %>
<asp:DropDownList ID="ddlEmployees" runat="server">
    <asp:ListItem>Amol Malhotra</asp:ListItem>
    <asp:ListItem>Shambhu Sharma</asp:ListItem>
    <asp:ListItem>Hemant Chopra</asp:ListItem>
    <asp:ListItem>Vishwa M Goswami</asp:ListItem>
    <asp:ListItem>Mohit Kalra</asp:ListItem>
    <asp:ListItem>Abhishek Nigam</asp:ListItem>
    <asp:ListItem>Yogesh Gupta</asp:ListItem>
    <asp:ListItem>Mayank Dhulekar</asp:ListItem>
    <asp:ListItem>Saurabh Mehrotra</asp:ListItem>
    <asp:ListItem>Rakesh Dixit</asp:ListItem>
</asp:DropDownList>

 

5.png

Image 5.

Now open WebUserControl1.ascx.cs and do below code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AccessUserControlToAnother
{
    public partial class WebUserControl1 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        public DropDownList ControlA_DDL
        {
            get
            {
                return this.ddlEmployees;
            }
        }
 
    }
}

6.png

Image 6.

Now Open WebUserControl2.ascx and do below code:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="AccessUserControlToAnother.WebUserControl2" %>
<table style="background-color: skyblue; width: 100%;">
    <tr>
        <td style="height: 10px;"></td>
    </tr>
    <tr>
 
        <td>
            <asp:Button ID="btnDDLValue" runat="server" OnClick="btnDDLValue_Click" Text="Get DropDown Selected Value" /></td>
    </tr>
    <tr>
        <td style="height: 10px;"></td>
    </tr>
    <tr>
        <td>Your Selected Employee:
            <asp:TextBox ID="txtDDLValue" runat="server"></asp:TextBox></td>
    </tr>
</table> 

7.png

Image 7.

Now open WebUserControl2.ascx.cs and do below code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace AccessUserControlToAnother
{
    public partial class WebUserControl2 : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
        protected void btnDDLValue_Click(object sender, EventArgs e)
        {
            WebUserControl1 ctrlA = (WebUserControl1)Page.FindControl("cA");
            DropDownList ddl = ctrlA.ControlA_DDL;
            txtDDLValue.Text = ddl.SelectedValue;
        }
    }
}

8.png

Image 8.

Now Open Default.aspx and do below code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AccessUserControlToAnother.Default" %>
<%@ Register Src="WebUserControl1.ascx" TagName="ControlA" TagPrefix="ctrlA" %>
<%@ Register Src="WebUserControl2.ascx" TagName="ControlB" TagPrefix="ctrlB" %>
<!DOCTYPE html>
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="background-color: skyblue; width: 100%;">
                <tr>
                    <td style="height: 10px;"></td>
                </tr>
                <tr style="padding: 10px;">
                    <td>Select Employee :
                        <ctrlA:ControlA ID="cA" runat="server" />
                    </td>
                </tr>
                <tr>
                    <td style="height: 10px;"></td>
                </tr>
                <tr>
                    <td>
                        <ctrlB:ControlB ID="cB" runat="server" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

 

Now Run you Application:


9.png

Image 9.

10.png

Image 10.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us