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?
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
{
}
}
}
}
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;
}
}
mbavila
Member
66 Points
56 Posts
Custom DropDown List Server Control
Mar 14, 2012 09:21 AM|LINK
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?
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 { } } } }avinash_bhud...
Contributor
2881 Points
517 Posts
Re: Custom DropDown List Server Control
Mar 14, 2012 12:29 PM|LINK
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.
mbavila
Member
66 Points
56 Posts
Re: Custom DropDown List Server Control
Mar 15, 2012 01:21 AM|LINK
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?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Custom DropDown List Server Control
Mar 16, 2012 01:48 AM|LINK
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; } }mbavila
Member
66 Points
56 Posts
Re: Custom DropDown List Server Control
Mar 16, 2012 09:33 AM|LINK
yes it works now..thank you very much. i dont know what happened the first time