Articles in this section
Category / Section

How to avoid passing extra data added in .NET Web Forms?

2 mins read

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




Note:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied