How to write user control ?
-
Hi, I have tried to construct a user control named EventCategory, that holds 2 dropdownlists to select an event category. Depending on the selection in the first dropdownlist, the second dropdownlist must be repopulated with items corresponding with the first dropdownlist selection. Here is my control : <%@ Control Language="C#" AutoEventWireup="true" CodeFile="EventCategory.ascx.cs" Inherits="EventCategory" %> <table border="0"> <tr> <td style="width: 65px"><asp:Label ID="CategoryLabel" runat="server" Text="Category:" AssociatedControlID="CategoryList" /></td> <td> <asp:DropDownList ID="CategoryList" runat="server" Width="130px" AutoPostBack="True" OnSelectedIndexChanged="CategoryList_SelectedIndexChanged" /> </td> <td style="width: 20px" /> <td style="width: 90px"><asp:Label ID="SubCategoryLabel" runat="server" Text="SubCategory:" AssociatedControlID="SubCategoryList" /></td> <td> <asp:DropDownList ID="SubCategoryList" runat="server" Width="130px" /> </td> </tr> </table> When i enter the control in a web page, I can wite something like this : <!-- The user control gets registered in web.config --> .... <UC:EventCategory ID="EventCategory" runat="server" SelectText="«Select»" Category="Parties" /> Here I want the "Parties" item to be initial selected in the CategoryList control when the page comes up. therefore I wrote the following in code behind : public partial class EventCategory : System.Web.UI.UserControl { #region Private data members private string m_strCategory = string.Empty; private string m_strSubCategory = string.Empty; #endregion #region Public properties [Category("Appearance"), DefaultValue(""), Description("The event category associated with the user control.")] public string Category { get { return ((CategoryList.SelectedItem != null) && (CategoryList.SelectedItem.Value != "0") ? CategoryList.SelectedItem.Text : string.Empty).Trim(); } set { m_strCategory = value.Trim(); } } [Category("Appearance"), DefaultValue(""), Description("The event subcategory associated with the user control.")] public string SubCategory { get { return ((SubCategoryList.SelectedItem != null) && (SubCategoryList.SelectedItem.Val