expose appropiate properties from the UserControl and use code-behind, for exampleMyDropDown.ascx: <%@ Control Language="c#" AutoEventWireup="false" Codebehind="MyDropDown.ascx.cs" Inherits="WebApplication4.MyDropDownList"> <asp:DropDownList id=DropDownList1 runat="server"></asp:DropDownList>MyDropDown.ascx.cs:namespace WebApplication4 { using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; public class MyDropDownList : System.Web.UI.UserControl { protected System.Web.UI.WebControls.DropDownList DropDownList1; public ListItem SelectedItem { get {return DropDownList1.SelectedItem;} } public int SelectedIndex { get {return DropDownList1.SelectedIndex;} set {DropDownList1.SelectedIndex = value;} } ......... } } webform1.aspx: <%@ Register TagPrefix="uc1" TagName="MyDropDown" Src="MyDropDown.ascx" %><form id=Form1 method=post runat="server"> <uc1:MyDropDown id=MyDropDown1 runat="server"> </form>webform1.aspx.cs:protected WebApplication4.MyDropDownList MyDropDown1;//.... Response.Write(MyDropDown1.SelectedItem.Value);
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="MyDropDown.ascx.cs" Inherits="WebApplication4.MyDropDownList">
<asp:DropDownList id=DropDownList1 runat="server"></asp:DropDownList>MyDropDown.ascx.cs:namespace WebApplication4
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls; public class MyDropDownList : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList DropDownList1; public ListItem SelectedItem
{
get {return DropDownList1.SelectedItem;}
} public int SelectedIndex
{
get {return DropDownList1.SelectedIndex;}
set {DropDownList1.SelectedIndex = value;}
}
.........
}
}
webform1.aspx:
<%@ Register TagPrefix="uc1" TagName="MyDropDown" Src="MyDropDown.ascx" %><form id=Form1 method=post runat="server">
<uc1:MyDropDown id=MyDropDown1 runat="server">
</form>webform1.aspx.cs:protected WebApplication4.MyDropDownList MyDropDown1;//....
Response.Write(MyDropDown1.SelectedItem.Value);