locked
Custom DropDown List Server Control RRS feed

  • Question

  • User-1908143565 posted

    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

    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? Cry

     

    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("Text")]
        [ToolboxData("<{0}:NpaxDropDown runat=server></{0}:NpaxDropDown>")]
        public class NpaxDropDown : DropDownList, IPostBackDataHandler
        {
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [Localizable(true)]
            public string Text
            {
                get
                {
                    String s = this.Text;
                    return ((s == null) ? String.Empty : s);
                }
    
                set
                {
                    this.Text = value;
                    EnsureChildControls();
                }
            }
            [Bindable(true)]
            [Category("DataBind")]
            [DefaultValue("")]
            [Localizable(true)]
            public DataTable DataSource
            {
                get
                {
                    DataTable s = (DataTable)ViewState["DataSource"];
    
                    return ((s == null) ? new DataTable(): s);
                }
    
                set
                {
                    ViewState["DataSource"] = value;
                    EnsureChildControls();
                }
            }
            [Bindable(true)]
            [Category("DataBind")]
            [DefaultValue("")]
            [Localizable(true)]
            public string DataValueField
            {
                get
                {
                    
                    String s = (string)ViewState["DataValueField"];
                    if (s == null)
                    {
                        return String.Empty;
                    }
                    else
                    {
                        return s;
                    }
                }
    
                set
                {
                    ViewState["DataValueField"]=value;
                    EnsureChildControls();
                }
            }
            [Bindable(true)]
            [Category("DataBind")]
            [DefaultValue("")]
            [Localizable(true)]
            public string DataTextField
            {
                
                get
                {
                    String s = (string)ViewState["DataTextField"];
                    return ((s == null) ? String.Empty : s);
                }
    
                set
                {
                    
                    ViewState["DataValueField"] = value;
                    EnsureChildControls();
                }
            }
            
     
           protected override void  CreateChildControls()
            {
                this.DataTextField = DataTextField;
                this.DataValueField = DataValueField;
                this.DataSource = DataSource;
               
     	    base.CreateChildControls();
            }
                   
         
        }
    }
    
    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
                {
                    
                }
                
            }
    
           
          
    
    
          }
    }

     

    Wednesday, March 14, 2012 5:21 AM

Answers

  • User3866881 posted

    Hello mba:)

    Yes,Dropdownlist itself has the property of DataSource,DataTextField as well as DataValue Field,So no need for you to create these properties——othersises,you will hide them。

    So try this:

    [DefaultProperty("Text")]
        [ToolboxData("<{0}:NpaxDropDown runat=server></{0}:NpaxDropDown>")]
        public class NpaxDropDown : DropDownList, IPostBackDataHandler
        {
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [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;
            }
    
    
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 15, 2012 9:48 PM

All replies

  • User1849990362 posted

    You need to inherit custom control class by "DataBoundControl" which is a data-bound control base class.

    refer the links below.

    http://msdn.microsoft.com/en-us/library/ms366539.aspx

    Simple DataBoundControl implementation

    http://msdn.microsoft.com/en-us/library/ms366540.aspx

    Hope this helps.

    Wednesday, March 14, 2012 8:29 AM
  • User-1908143565 posted

    You need to inherit custom control class by "DataBoundControl" which is a data-bound control base class.

    refer the links below.

    http://msdn.microsoft.com/en-us/library/ms366539.aspx

    Simple DataBoundControl implementation

    http://msdn.microsoft.com/en-us/library/ms366540.aspx

    Hope this helps.

     

    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?

    Wednesday, March 14, 2012 9:21 PM
  • User3866881 posted

    Hello mba:)

    Yes,Dropdownlist itself has the property of DataSource,DataTextField as well as DataValue Field,So no need for you to create these properties——othersises,you will hide them。

    So try this:

    [DefaultProperty("Text")]
        [ToolboxData("<{0}:NpaxDropDown runat=server></{0}:NpaxDropDown>")]
        public class NpaxDropDown : DropDownList, IPostBackDataHandler
        {
            [Bindable(true)]
            [Category("Appearance")]
            [DefaultValue("")]
            [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;
            }
    
    
        }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 15, 2012 9:48 PM
  • User-1908143565 posted

    yes it works now..thank you very much. i dont know what happened the first time

    Friday, March 16, 2012 5:33 AM