..


Sponsored Links

Data binding

In ASP.NET, a number of controls has the ability to understand the form and content of a collection and to generate the proper tags to represent the same. Among them we can cite, for example, listbox and dropdownlist.

One of the most common problems in developing any software application or website is to represent collections / sets of items in the appropriate user interfaces (UI, User Interface). We think one of the many commercial sites on the web. When you access one of them if we buy something we usually fill out a card, in which the various data usually is their nation. This field is usually represented by a dropdownlist, a drop down menu that allows you to view the list and select one of the nations.

The fundamental question is how this control is populated? Controls like this (but also listbox for example) displaying the Items collection, through which you can add items to list. Just use the method Items.Add






 protected void PopolaDropDownList (IList list)







 {

 

  



 for (int i = 0; i <lista.Count; i + +)

 

  



 {

 

    



 this.MiaDropDownList.Items.Add (list [i]);

 

  



 }







 }



However, as one can understand the addition of elements to a manual control is not a recommended way in certain contexts (consider, for example, sets of items to display that may change over time) and for this reason, ASP.NET includes a number data link control (date bound) capable of making collections of items and automatically generate tags for display in our place (the so-called data binding).

Each of these controls include appropriate properties to connect to a data source (data source). For simple data binding using the DataSource property, which you can connect to any collection / collection that implements IEnumerable, ICollection or IListSource. After the property is valued as possible, you could invoke the DataBind method on the page (or control) to instruct the control to iterate the collection is connected.

Controls that support this feature are: ListControl, CheckBoxList, RadioButtonList, dropdownlist, listbox, treeview, menu, gridview, datagrid, repeater, FormView, DetailsView. Clearly, for the purposes of this guide we will not investigate the characteristics of each of them and do what I ask you to consult the official Microsoft.

Let's see an example of using some of these controls. In Visual Studio create a new website and add to it a class car has two properties that make and model, as you might guess, will serve as a collection of brands and models of cars.

Here is its implementation






 public class Car







 {



  



 public string Brand {get; set;}



  



 public string Model {get; set;}





  



 public Car (string strMarca,



  



 strModello string)



  



 {



      



 this.Marca = strMarca;



      



 this.Modello = strModello;



  



 }



    

  



 public static List <Auto> CreaListaAuto ()



  



 {



    



 List = new List <Auto> lavish <Auto> ();



        

    



 Car saute;



        

    



 sauté = new Car ("Lance", "Delta");



    



 lAuto.Add (saute);



        

    



 sauté = new car ("Fiat", "Point");



    



 lAuto.Add (saute);



        

    



 sauté = new Car ("Audi", "A4");



    



 lAuto.Add (saute);



        

    



 sauté = new Car ("Mercedes", "SLK");



    



 lAuto.Add (saute);



        

    



 sauté = new Car ("Ferrari", "F399");



    



 lAuto.Add (saute);



        

    



 sauté = new Car ("Ford", "Kuga");



    



 lAuto.Add (saute);



        

    



 return hearty;



  



 }







 }



Add a new web form called Default.aspx in our project and insert it into four of the controls we've seen before, and that is a listbox, a dropdownlist, a RadioButtonList and CheckBoxList

We set the AutoPostBack property to true for all controls so that selecting an item to generate a postback event during which the element can be questioned. It also inserts into any position on the page with a label that will be useful later. Now we have to change the link to the page class that returns the collection of cars to various controls. For each control by setting the DataTextField property to the model field, so that the car models appear in various controls. And set the DataValueField property on Brand area. Finally in the Load event of the page insert the following code to create the collection of cars and for its association with various controls






 protected void Page_Load (object sender, EventArgs e)







 {



  



 if (! this.IsPostBack)



  



 {



    



 List <Auto> autoList Auto.CreaListaAuto = ();



    



 this.ListBox1.DataSource = autoList;



    



 this.DropDownList1.DataSource = autoList;



    



 this.RadioButtonList1.DataSource = autoList;



    



 this.CheckBoxList1.DataSource = autoList;



    



 this.DataBind ();



  



 }







 }



Launching the application as it will appear here each control

At this point we can connect to the SelectedIndexChanged event of various controls to highlight how to access information about the specific make of car. Since the procedure is similar only to make an example of the listbox. We generate so its SelectedIndexChanged event and insert the following code inside






 ListBox1_SelectedIndexChanged protected void (object sender, EventArgs e)







 {



  



 this.Label1.Text = this.ListBox1.SelectedValue;







 }



In this way we will see that by starting the application by selecting the different car models the label will be enhanced with the corresponding brand

Help with Visual Studio ASP.Net
E-Learning
ASP (Advanced) ASP (Advanced)
Full course for creating dynamic Web sites. From 39 €.
ASP.NET (Course) ASP.NET (Course)
Full course for building Web applications from 49 €.
SQL and Database (Course) SQL and Database (Course)
Create and manage relational databases. From 39 €.
Sponsored Links