)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to avoid passing extra data added to JSON DataSource from inherited Base class?

Platform: ASP.NET Web Forms |
Control: General |
Published Date: June 1, 2015 |
Last Revised Date: May 8, 2019

Description

When 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:

Extra values

Figure 1: Extra values

Solution

The 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:

Page source

Figure 2: browser page source without extra data

Control is rendered as follows:

DropDownList

Figure 3: Control

2X faster development

The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon