I want to use multiple controls like dropdown list, listbox, radiobutton list in a single ascx page instead of using multiple ascx pages and wanted to access it in an aspx page.
Lets say you have a user control MyControl.ascx with these controls:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyControl.ascx.cs" Inherits="WebApplication1.MyControl" %>
<asp:LinkButton ID="lbtn" runat="server">Link Button</asp:LinkButton>
<asp:RadioButton ID="rbtn" runat="server" Text="Radio Button" />
<asp:DropDownList ID="ddl" runat="server">
<asp:ListItem Text="Dropdown"></asp:ListItem>
</asp:DropDownList>
Now MyControl.ascx.cs page code: Create public properties for each control and set them
public DropDownList dropdown { get; set; }
public RadioButton radioButton { get; set; }
public LinkButton linkbutton { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
SetCtrlProperties();
}
private void SetCtrlProperties(){
dropdown = ddl;
radioButton = rbtn;
linkbutton = lbtn;
}
Now if you are placing this on a page named default.aspx and want to hide all control on a button clicked from .aspx page then .aspx page code:
<body>
<form id="form1" runat="server">
<div>
</div>
<uc1:MyControl ID="MyControl1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Hide" onclick="Button1_Click" />
</form>
</body>
and Default.aspx.cs page code :
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MyControl1.dropdown.Visible = false;
MyControl1.radioButton.Visible = false;
MyControl1.linkbutton.Visible = false;
}