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:
- <%@ 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>
Now open WebUserControl1.ascx.cs and write the following 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;
- }
- }
-
- }
- }
Now Open WebUserControl2.ascx and write the following 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>
Now open WebUserControl2.ascx.cs and write the following 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;
- }
- }
- }
Now run you Application: