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. namespace AccessUserControlToAnother
  8. {
  9. public partial class WebUserControl1 : System.Web.UI.UserControl
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. public DropDownList ControlA_DDL
  15. {
  16. get
  17. {
  18. return this.ddlEmployees;
  19. }
  20. }
  21. }
  22. }


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. <td>
  8. <asp:Button ID="btnDDLValue" runat="server" OnClick="btnDDLValue_Click" Text="Get DropDown Selected Value" /></td>
  9. </tr>
  10. <tr>
  11. <td style="height: 10px;"></td>
  12. </tr>
  13. <tr>
  14. <td>Your Selected Employee:
  15. <asp:TextBox ID="txtDDLValue" runat="server"></asp:TextBox></td>
  16. </tr>
  17. </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. namespace AccessUserControlToAnother
  8. {
  9. public partial class WebUserControl2 : System.Web.UI.UserControl
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void btnDDLValue_Click(object sender, EventArgs e)
  15. {
  16. WebUserControl1 ctrlA = (WebUserControl1)Page.FindControl("cA");
  17. DropDownList ddl = ctrlA.ControlA_DDL;
  18. txtDDLValue.Text = ddl.SelectedValue;
  19. }
  20. }
  21. }
Now run you Application: