-->

22 December 2019

Asp.Net Two-Way DataBinding Expressions

  Asp.Net CS By Example       22 December 2019


 Two-Way DataBinding Expressions:  

  The ASP.NET Framework actually supports two types of templates and two types of
DataBinding expressions. The ASP.NET Framework supports both one-way DataBinding
expressions and two-way DataBinding expressions.

  we have used one-way DataBinding expressions exclusively.
In a oneway DataBinding expression, we use the DataBinding expression to display the value of a
data item. We use the Eval() method to display the value of a one-way DataBinding expression.

  In a two-way DataBinding expression, we not only can display the value of a data item,
we also can modify the value of a data item. We use the Bind() method when working
with a two-way DataBinding expressionl.
  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 we click the Update button, any changes we make
to the Text properties are updated in the Movies database table.

Code: ShowFormView.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowFormView.aspx.cs" 
Inherits="LearnAsp.Net.ControlDemo.DataBound.ShowFormView" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Show FormView</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FormView ID="FormView1" DataKeyNames="employee_id" DataSourceID="srcEmployees" 
        BackColor="#cbe0c5" BorderColor="Black"
            DefaultMode="Edit" AllowPaging="true" runat="server">
            <EditItemTemplate>
                <asp:Label ID="lblEmployeeName" Text="Employee Name:" AssociatedControlID="txtName"
                    runat="server" />
                <asp:TextBox ID="txtName" Text='<%#Bind("first_name")%>' runat="server" />
                <br />
                <asp:Label ID="lblhiredate" Text="Hire Date:" AssociatedControlID="txthiredate" runat="server" />
                <asp:TextBox ID="txthiredate" Text='<%#Bind("hire_date", "{0:dd-MMM-yyyy}")%>' runat="server" />
                <br />
                <asp:Button ID="btnUpdate" Text="Update" CommandName="Update" runat="server" />
            </EditItemTemplate>
        </asp:FormView>
        <asp:SqlDataSource ID='srcEmployees' ConnectionString="<%$ ConnectionStrings:constrOracle %>"
            ProviderName="System.Data.OracleClient" SelectCommand=" select employee_id,first_name ,hire_date from employees "
            UpdateCommand='UPDATE employees SET first_name=:first_name, hire_date=:hire_date WHERE employee_id=:employee_id'
            runat='server' />
    </div>
    </form>
</body>
</html>

Output:


logoblog

Thanks for reading Asp.Net Two-Way DataBinding Expressions

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.