2X faster development
The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
DescriptionWhen the DataSource for a DropDownList control is bound from a Derived class, then the generated JSON DataSource has some extra values as shown in the following screenshot: Figure 1: Extra values SolutionThe reason there are extra values in the JSON DataSource of the DropDownList control is because the DataSource is defined from a Derived class. In general, when an object is created for a Derived class, it includes the public properties, methods, and variables from its Base class. In order to overcome this issue, define the DataSource for the DropDownList control only with values that are to be bound with the DropDownList fields. That is, when you bind the DataSource from a Derived class having multiple data objects from the Base class, you have to extract the specifically required data to be mapped in the control fields. Generate a filtered data list from the Derived class to bind the DropDownList DataSource by using the Select() method.
ASPX <ej:DropDownList runat="server" ID="DropDown1" DataTextField="Text" DataValueField="Value"></ej:DropDownList>
To bind the DataTextField and DataValueField, generate a data list source from the corresponding class as given in following code.
CodeBehind - C# protected void Page_Load(object sender, EventArgs e) { DropDown1.DataSource = new DerivedClass().GetDDList().Select(data => new { data.Text, data.Value }).ToList(); } public class DerivedClass : BaseClass { public int Value { get; set; } public string Text { get; set; } public DerivedClass() { } public DerivedClass(int _val, string _text) { Value = _val; Text = _text; } public List<DerivedClass> GetDDList() { List<DerivedClass> data = new List<DerivedClass>(); data.Add(new DerivedClass(1, "Accordion")); data.Add(new DerivedClass(2, "AutoComplete")); data.Add(new DerivedClass(3, "Barcode")); data.Add(new DerivedClass(4, "Button")); data.Add(new DerivedClass(5, "Chart")); data.Add(new DerivedClass(6, "Checkbox")); data.Add(new DerivedClass(7, "Diagram")); data.Add(new DerivedClass(8, "DropDownList")); data.Add(new DerivedClass(9, "TreeView")); data.Add(new DerivedClass(10, "Schedule")); data.Add(new DerivedClass(11, "ListBox")); data.Add(new DerivedClass(12, "ListView")); data.Add(new DerivedClass(13, "Gauge")); data.Add(new DerivedClass(14, "Map")); data.Add(new DerivedClass(15, "MaskEdit")); data.Add(new DerivedClass(16, "Numeric TextBox")); data.Add(new DerivedClass(17, "Pivot Grid")); data.Add(new DerivedClass(18, "Grid")); data.Add(new DerivedClass(19, "Tree Map")); data.Add(new DerivedClass(20, "Tree grid")); return data; } } public class BaseClass { public string Control { get; set; } public string Product { get; set; } public BaseClass() { this.Control = "DropDown"; this.Product = "ASP.NET"; } }
Now, the browser page source has no extra data as shown in the following screenshot: Figure 2: browser page source without extra data Control is rendered as follows: Figure 3: Control |
2X faster development
The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
This page will automatically be redirected to the sign-in page in 10 seconds.