Showing posts with label databound controls. Show all posts
Showing posts with label databound controls. Show all posts

Programmatic DataBinding in Asp.Net


An alternative to declarative databinding, you can programmatically bind any of the List controls to a data source

Example:
Lets create a BulletList which shows the list of Fruits.

<asp:BulletedList
            ID="blFruits"
            runat="server" />

And in Page_Load() event write this code. 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<String> fruits = new List<string>();
            fruits.Add("Apple");
            fruits.Add("Banana");
            fruits.Add("Mango");
            blFruits.DataSource = fruits;
            blFruits.DataBind();
        }
    }

if(!IsPostBack) prevents the re-initialisation of the BulletList items during postbacks.

Read About Declarative DataBinding : Declarative DataBinding in Asp.Net

Read more...

Declarative DataBinding in Asp.Net


You can bind any of the List controls to a data source. The List controls support both
declarative databinding and programmatic databinding

Example:

Suppose you have a movies table in your database. It has two columns named Id and Title.
<asp:DropDownList
            ID="ddlMovies"
            DataSourceID="srcMovies"
            DataTextField="Title"
            DataValueField="Id"
            runat="server" />

        <asp:SqlDataSource
            ID="srcMovies"
            SelectCommand="SELECT Id, Title FROM Movies"
            ConnectionString="yourConnectionString"
            runat="server" />

The DropDownList control’s DataSourceID property points to the ID of the SqlDataSource control. The SqlDataSource control retrieves the records from the Movies database table. The DropDownList control grabs these records from the SqlDataSource control and creates a ListItem control for each data item. 

The DropDownList control has both its DataTextField and DataValueField properties set. When the DropDownList control creates each of its list items, it uses the values of the DataTextField and DataValueField properties to set the Text and Value properties of each list item.

Read About Programmatic DataBinding :  ProgrammaticDataBinding in Asp.Net

Read more...

Eval() and Bind() Two-Way DataBinding Expressions in Asp.Net

The ASP.NET Framework supports both one-way DataBinding expressions and two-way DataBinding expressions.

In a oneway DataBinding expression, you use the DataBinding expression to display the value of a data item. You use the Eval() method to display the value of a one-way DataBinding expression.

In a two-way DataBinding expression, you not only can display the value of a data item,
you also can modify the value of a data item. You use the Bind() method when working with a    two-way DataBinding expression.

Example:

Suppose you have a movies table in your database. It has columns named Id, Title, Director, DateReleased.

<asp:FormView id="FormView1" DataKeyNames="Id" DataSourceId="srcMovies" DefaultMode="Edit" AllowPaging="true" Runat="server">
<EditItemTemplate>

<asp:Label
id="lblTitle"
Text="Title:"
AssociatedControlID="txtTitle"
Runat="server" />

<asp:TextBox
id="txtTitle"
Text='<%#Bind(“Title")%>'
Runat="server" />
<br />

<asp:Label
id="lblDirector"
Text="Director:"
AssociatedControlID="txtDirector"
Runat="server" />

<asp:TextBox
id="txtDirector"
Text='<%#Bind(“Director")%>'
Runat="server" />
<br />

<asp:Button
id="btnUpdate"
Text="Update"
CommandName="Update"
Runat="server" />
</EditItemTemplate>
</asp:FormView>

<asp:SqlDataSource id="srcMovies" ConnectionString="yourConnectionString"
SelectCommand="SELECT Id, Title,Director,DateReleased FROM Movies"
UpdateCommand="UPDATE Movies SET Title=@Title,
Director=@Director WHERE Id=@Id"
Runat="server" />

The FormView contains an EditItemTemplate. The EditItemTemplate contains three TextBox controls. Each TextBox control has a two-way DataBinding expression assigned to its Text property.

The DataBinding expressions associate the TextBox control properties with the properties of the data item being edited. When you click the Update button, any changes you make to the Text properties are updated in the Movies database table.


Read more...