Access A Web User Control Value In Another Web User Control

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 another Web user Control.

My scenario is I have a drop down list on one of the Web user Control and on button click I am going to show this Drop Down selected value in another Web User control.

Open Visual Studio, File, New, then ASP.NET Empty Web Site



Now I will add 2 Web user Control. So right click on Project’s Solution Explorer, Add, then select Web User Control.





Add another web user control



Now open WebUserControl1.ascx and here's the code snippet:

  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs" Inherits="AccessUserControlToAnother.WebUserControl1" %>  
  2. <asp:DropDownList ID="ddlEmployees" runat="server">  
  3.     <asp:ListItem>Amol Malhotra</asp:ListItem>  
  4.     <asp:ListItem>Shambhu Sharma</asp:ListItem>  
  5.     <asp:ListItem>Hemant Chopra</asp:ListItem>  
  6.     <asp:ListItem>Vishwa M Goswami</asp:ListItem>  
  7.     <asp:ListItem>Mohit Kalra</asp:ListItem>  
  8.     <asp:ListItem>Abhishek Nigam</asp:ListItem>  
  9.     <asp:ListItem>Yogesh Gupta</asp:ListItem>  
  10.     <asp:ListItem>Mayank Dhulekar</asp:ListItem>  
  11.     <asp:ListItem>Saurabh Mehrotra</asp:ListItem>  
  12.     <asp:ListItem>Rakesh Dixit</asp:ListItem>  
  13. </asp:DropDownList>  


Now open WebUserControl1.ascx.cs and write the following code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace AccessUserControlToAnother  
  9. {  
  10.     public partial class WebUserControl1 : System.Web.UI.UserControl  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.   
  15.         }  
  16.   
  17.         public DropDownList ControlA_DDL  
  18.         {  
  19.             get  
  20.             {  
  21.                 return this.ddlEmployees;  
  22.             }  
  23.         }  
  24.   
  25.     }  
  26. }  


Now Open WebUserControl2.ascx and write the following code:
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl2.ascx.cs" Inherits="AccessUserControlToAnother.WebUserControl2" %>  
  2. <table style="background-color: skyblue; width: 100%;">  
  3.     <tr>  
  4.         <td style="height: 10px;"></td>  
  5.     </tr>  
  6.     <tr>  
  7.   
  8.         <td>  
  9.             <asp:Button ID="btnDDLValue" runat="server" OnClick="btnDDLValue_Click" Text="Get DropDown Selected Value" /></td>  
  10.     </tr>  
  11.     <tr>  
  12.         <td style="height: 10px;"></td>  
  13.     </tr>  
  14.     <tr>  
  15.   
  16.         <td>Your Selected Employee:   
  17.             <asp:TextBox ID="txtDDLValue" runat="server"></asp:TextBox></td>  
  18.     </tr>  
  19.   
  20. </table>  


Now open WebUserControl2.ascx.cs and write the following code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. namespace AccessUserControlToAnother  
  9. {  
  10.     public partial class WebUserControl2 : System.Web.UI.UserControl  
  11.     {  
  12.         protected void Page_Load(object sender, EventArgs e)  
  13.         {  
  14.   
  15.         }  
  16.         protected void btnDDLValue_Click(object sender, EventArgs e)  
  17.         {  
  18.             WebUserControl1 ctrlA = (WebUserControl1)Page.FindControl("cA");  
  19.             DropDownList ddl = ctrlA.ControlA_DDL;  
  20.             txtDDLValue.Text = ddl.SelectedValue;  
  21.         }  
  22.     }  
  23. }  
Now run you Application:




Similar Articles