ASP.NET Tutorial/Data Binding/Expressions — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:58, 26 мая 2010

Binding Expression

<%@Page debug="true"%>
<script language="C#" runat="server" >
DateTime theTime;
protected void Page_Load(object o, EventArgs e) {
    theTime = DateTime.Now;
    Page.DataBind();
}
</script>
The time is: <%# theTime %><br/>


Databind the result of a method call

<script runat="server" language="C#">
protected void Page_Load(object o, EventArgs e) {
    if(IsPostBack) {
        Page.DataBind();
    }
}
protected double CalculateFactorial(int input) {
    double result = 1;
    for(;input > 0; input--) {
        result *= input;
    }
    return result;
}
</script>
<form runat="server">
Databind the result of a method call.
<asp:Textbox runat="server" id="theInput" /><br/>
<asp:RequiredFieldValidator 
    runat="server"
    ControlToValidate="theInput"
    ErrorMessage="input is required" />
<asp:RangeValidator
    runat="server"
    ControlToValidate="theInput"
    ErrorMessage="input must be >= 0 and < 20"
    Type="integer"
    MinimumValue="0"
    MaximumValue="20" /><br/>
<asp:Button 
    runat="server" 
    type="submit" 
    Text="Calculate Factorial" /><br/>
The result is: <b><%# CalculateFactorial(Int32.Parse(theInput.Text)).ToString() %></b>
</form>


DataList with data binding expression

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Extracting Rows From Templates</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:NorthWind %>"
            SelectCommand="SELECT TOP 10 * FROM customers">
        </asp:SqlDataSource>
        
        <asp:DataList ID="list" runat="server" DataSourceID="SqlDataSource1" 
            ExtractTemplateRows="true" RepeatColumns="3" BorderStyle="Solid" BorderWidth="1px">
          <alternatingitemstyle backcolor="PaleGoldenrod" />
          <headerstyle backcolor="Brown" forecolor="White" font-size="Larger" 
            font-bold="True" horizontalalign="Center" />
          <HeaderTemplate>
          <asp:table ID="Table1" runat="server">
            <asp:tablerow ID="Tablerow1" runat="server">
              <asp:tablecell ID="Tablecell1" runat="server">Customer</asp:TableCell>
              <asp:tablecell ID="Tablecell2" runat="server">Contact</asp:TableCell>
            </asp:tablerow>
          </asp:table>
          </HeaderTemplate>

          <ItemTemplate>
          <asp:table ID="Table2" runat="server">
            <asp:tablerow ID="Tablerow2" runat="server" font-bold="true">
              <asp:tablecell ID="Tablecell3" runat="server">
                <%# Eval("CustomerID") %>
              </asp:tablecell>
              <asp:tablecell ID="Tablecell4" runat="server">
                <%# Eval("ContactName") %>
              </asp:tablecell>
            </asp:tablerow>
            <asp:tablerow ID="Tablerow3" runat="server">
              <asp:tablecell ID="Tablecell5" runat="server" columnspan="2">
                <%# Eval("Address") + " - " + 
                    Eval("City")   %>
              </asp:tablecell>
            </asp:tablerow>
          </asp:table>
          </ItemTemplate>
        </asp:datalist>    
    </div>
    </form>
</body>
</html>


Put simple data binding expressions

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" 
    Inherits="Default"%>
<%@ Import Namespace="System.Drawing" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Simple Data Binding</title>
</head>
<body>
    <div id="pageContent">
        <form id="form1" runat="server">
            <asp:DropDownList ID="SelColors" runat="server" AutoPostBack="True">
                <asp:ListItem>Orange</asp:ListItem>
                <asp:ListItem>Green</asp:ListItem>
                <asp:ListItem>Cyan</asp:ListItem>
                <asp:ListItem>Red</asp:ListItem>
                <asp:ListItem>Blue</asp:ListItem>
            </asp:DropDownList>
            <asp:Label runat="server" ID="lblColor" EnableTheming="false"
                ForeColor="<%# Color.FromName(SelColors.SelectedValue) %>"
                Text="<%# "<b>You selected: </b>" + SelColors.SelectedValue %>"  /> 
            <asp:Label runat="server" ID="Label4" Text="<%# "on " + DateTime.Now %>" /> 
            <hr />
        </form>
    </div>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class Default : System.Web.UI.Page
{
    void Page_Load(object sender, EventArgs e)
    {
        DataBind();
    }
}