Custom DropDown List Server Controlhttp://forums.asp.net/t/1780171.aspx/1?Custom+DropDown+List+Server+ControlFri, 16 Mar 2012 09:33:20 -040017801714879335http://forums.asp.net/p/1780171/4879335.aspx/1?Custom+DropDown+List+Server+ControlCustom DropDown List Server Control <p>Hi posted a problem about dynamic tables but realize i was tackling a giant without learning how to punch first. I just realized my other more simpler controls are not able to save view state also. What i want is simple. I just want to have a custom dropdown list that inherits everything the dropdown list have. No customization yet whatsover. Tried to use the code but its not loading the items to the dropdownlist...please help</p> <p>heres the code for both server control and webform. I checked the datatable it has items in it so its not a problem with the data source. Anyone knows whats wrong with it? <img title="Cry" border="0" alt="Cry" src="http://forums.asp.net/scripts/tiny_mce/plugins/emotions/img/smiley-cry.gif"></p> <p>&nbsp;</p> <pre class="prettyprint">using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; namespace SFAHPOI { [DefaultProperty(&quot;Text&quot;)] [ToolboxData(&quot;&lt;{0}:NpaxDropDown runat=server&gt;&lt;/{0}:NpaxDropDown&gt;&quot;)] public class NpaxDropDown : DropDownList, IPostBackDataHandler { [Bindable(true)] [Category(&quot;Appearance&quot;)] [DefaultValue(&quot;&quot;)] [Localizable(true)] public string Text { get { String s = this.Text; return ((s == null) ? String.Empty : s); } set { this.Text = value; EnsureChildControls(); } } [Bindable(true)] [Category(&quot;DataBind&quot;)] [DefaultValue(&quot;&quot;)] [Localizable(true)] public DataTable DataSource { get { DataTable s = (DataTable)ViewState[&quot;DataSource&quot;]; return ((s == null) ? new DataTable(): s); } set { ViewState[&quot;DataSource&quot;] = value; EnsureChildControls(); } } [Bindable(true)] [Category(&quot;DataBind&quot;)] [DefaultValue(&quot;&quot;)] [Localizable(true)] public string DataValueField { get { String s = (string)ViewState[&quot;DataValueField&quot;]; if (s == null) { return String.Empty; } else { return s; } } set { ViewState[&quot;DataValueField&quot;]=value; EnsureChildControls(); } } [Bindable(true)] [Category(&quot;DataBind&quot;)] [DefaultValue(&quot;&quot;)] [Localizable(true)] public string DataTextField { get { String s = (string)ViewState[&quot;DataTextField&quot;]; return ((s == null) ? String.Empty : s); } set { ViewState[&quot;DataValueField&quot;] = value; EnsureChildControls(); } } protected override void CreateChildControls() { this.DataTextField = DataTextField; this.DataValueField = DataValueField; this.DataSource = DataSource; base.CreateChildControls(); } } }</pre> <pre class="prettyprint">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using SFAHPOI; using System.Data; using HpoiBL; using DevExpress.XtraCharts.Web; namespace HPOI_SFA { public partial class WebForm4 : System.Web.UI.Page { string[] usernames= new string[5]; int s; ScreenMasterBL SM; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { NpaxDropDown1.DataSource = SM.GetScreens(); NpaxDropDown1.DataValueField = "ScreenID"; NpaxDropDown1.DataTextField = "ScreenName"; NpaxDropDown1.DataBind(); } else { } } } }</pre> <p>&nbsp;</p> 2012-03-14T09:21:23-04:004879743http://forums.asp.net/p/1780171/4879743.aspx/1?Re+Custom+DropDown+List+Server+ControlRe: Custom DropDown List Server Control <p>You need to inherit custom control class by &quot;DataBoundControl&quot; which is a data-bound control base class.</p> <p>refer the links below.</p> <p><a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms366539.aspx">http://msdn.microsoft.com/en-us/library/ms366539.aspx</a></p> <p>Simple DataBoundControl implementation</p> <p><a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms366540.aspx">http://msdn.microsoft.com/en-us/library/ms366540.aspx</a></p> <p>Hope this helps.</p> 2012-03-14T12:29:27-04:004880783http://forums.asp.net/p/1780171/4880783.aspx/1?Re+Custom+DropDown+List+Server+ControlRe: Custom DropDown List Server Control <p></p> <blockquote><span class="icon-blockquote"></span> <h4>avinash_bhudke</h4> <p></p> <p>You need to inherit custom control class by &quot;DataBoundControl&quot; which is a data-bound control base class.</p> <p>refer the links below.</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms366539.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms366539.aspx</a></p> <p>Simple DataBoundControl implementation</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms366540.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms366540.aspx</a></p> <p>Hope this helps.</p> <p></p> </blockquote> <p></p> <p>&nbsp;</p> <p>Isnt the DropDownList already a data bound control? whats the point of inheriting the object if u cant use the properties that it already has?</p> 2012-03-15T01:21:17-04:004882731http://forums.asp.net/p/1780171/4882731.aspx/1?Re+Custom+DropDown+List+Server+ControlRe: Custom DropDown List Server Control <p>Hello mba</p> <p>YesDropdownlist itself has the property of DataSourceDataTextField as well as DataValue FieldSo no need for you to create these propertiesothersisesyou will hide them</p> <p>So try this</p> <pre class="prettyprint">[DefaultProperty(&quot;Text&quot;)] [ToolboxData(&quot;&lt;{0}:NpaxDropDown runat=server&gt;&lt;/{0}:NpaxDropDown&gt;&quot;)] public class NpaxDropDown : DropDownList, IPostBackDataHandler { [Bindable(true)] [Category(&quot;Appearance&quot;)] [DefaultValue(&quot;&quot;)] [Localizable(true)] public override string Text { get { String s = this.Text; return ((s == null) ? String.Empty : s); } set { this.Text = value; EnsureChildControls(); } } protected override void CreateChildControls() { this.DataTextField = DataTextField; this.DataValueField = DataValueField; this.DataSource = DataSource; base.CreateChildControls(); ChildControlsCreated = true; } }</pre> 2012-03-16T01:48:35-04:004883571http://forums.asp.net/p/1780171/4883571.aspx/1?Re+Custom+DropDown+List+Server+ControlRe: Custom DropDown List Server Control <p>yes it works now..thank you very much. i dont know what happened the first time</p> 2012-03-16T09:33:20-04:00