ASP.NET Tutorial/Data Binding/DataBinding Expressions

Материал из .Net Framework эксперт
Версия от 11:59, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

A template can contain other controls

<%@ 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 id="Head1" runat="server">
    <title>Show Links</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater
        id="Repeater1"
        DataSourceId="srcDefaults"
        Runat="server">
        <ItemTemplate>
        <asp:HyperLink
            id="HyperLink1"
            Text="<%# Eval("Title") %>"
            NavigateUrl="<%# Eval("Id", "Details.aspx?id={0}") %>"
            runat="server" />
        <br />
        </ItemTemplate>
    </asp:Repeater>

    <asp:SqlDataSource
        id="srcDefaults"
        ConnectionString="Data Source=.\SQLExpress;
            AttachDbFilename=|DataDirectory|MyDatabase.mdf;
            Integrated Security=True;User Instance=True"
        SelectCommand="SELECT Id, Title FROM Defaults"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Call other methods than the Eval() method in a DataBinding expression

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
    public string FormatTitle(Object title)
    {
        return "<b>" + title.ToString().ToUpper() + "</b>";
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Format Default Titles</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater
        id="Repeater1"
        DataSourceId="srcDefaults"
        Runat="server">
        <ItemTemplate>
        <%# FormatTitle(Eval("Title")) %>
        <hr />
        </ItemTemplate>
    </asp:Repeater>

    <asp:SqlDataSource
        id="srcDefaults"
        ConnectionString="Data Source=.\SQLExpress;
            AttachDbFilename=|DataDirectory|MyDatabase.mdf;
            Integrated Security=True;User Instance=True"
        SelectCommand="SELECT Title FROM Defaults"
        Runat="server" />
    </div>
    </form>
</body>
</html>


Repeated Value Binding

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="RepeatedValueBinding" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="100%">
        <tr>
          <td>
            <select runat="server" ID="Select1" size="3" DataTextField="Key" DataValueField="Value"
              NAME="Select1" />
          </td>
          <td>
            <select runat="server" ID="Select2" DataTextField="Key" DataValueField="Value" NAME="Select2" />
          </td>
          <td>
            <asp:ListBox runat="server" ID="Listbox1" Size="3" DataTextField="Key" DataValueField="Value" />
          </td>
          <td>
            <asp:DropDownList runat="server" ID="DropdownList1" DataTextField="Key" DataValueField="Value" />
          </td>
          <td>
            <asp:RadioButtonList runat="server" ID="OptionList1" DataTextField="Key" DataValueField="Value" />
          </td>
          <td>
            <asp:CheckBoxList runat="server" ID="CheckList1" DataTextField="Key" DataValueField="Value" />
          </td>
        </tr>
      </table>
      <asp:Button runat="server" Text="Get Selection" ID="cmdGetSelection" OnClick="cmdGetSelection_Click" />
      <br>
      <br>
      <asp:Literal runat="server" ID="Result" EnableViewState="False" />
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class RepeatedValueBinding : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
      Hashtable ht = new Hashtable(3);
      ht.Add("value1", "Key1");
      ht.Add("value2", "Key2");
      ht.Add("value3", "Key3");
      Select1.DataSource = ht;
      Select2.DataSource = ht;
      Listbox1.DataSource = ht;
      DropdownList1.DataSource = ht;
      CheckList1.DataSource = ht;
      OptionList1.DataSource = ht;
      Page.DataBind();
    }
    }
  protected void cmdGetSelection_Click(object sender, EventArgs e)
  {
    if (Select1.SelectedIndex != -1)
      Result.Text += "- Item selected in Select1: " + Select1.Items[Select1.SelectedIndex].Text + " - " + Select1.Value + "<br>";
    
    if (Select2.SelectedIndex != -1)
      Result.Text += "- Item selected in Select2: " + Select2.Items[Select2.SelectedIndex].Text + " - " + Select2.Value + "<br>";
    if (Listbox1.SelectedIndex != -1)
      Result.Text += "- Item selected in Listbox1: " + Listbox1.SelectedItem.Text + " - " + Listbox1.SelectedItem.Value + "<br>";
    if (DropdownList1.SelectedIndex != -1)
      Result.Text += "- Item selected in DropdownList1: " + DropdownList1.SelectedItem.Text + " - " + DropdownList1.SelectedItem.Value + "<br>";
    if (OptionList1.SelectedIndex != -1)
      Result.Text += "- Item selected in OptionList1: " + OptionList1.SelectedItem.Text + " - " + OptionList1.SelectedItem.Value + "<br>";
    if (CheckList1.SelectedIndex != -1)
    {
      Result.Text += "- Items selected in CheckList1: ";
      foreach (ListItem li in CheckList1.Items)
      {
        if (li.Selected)
          Result.Text += li.Text + " - " + li.Value + " ";
      }
    }
  }
}


Single value binding

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="SingleValueBinding" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Image runat="server" ImageUrl="<%# FilePath %>" ID="Image1"/><br>
      <asp:Label runat="server" Text="<%# FilePath %>" ID="Label1"/><br>
      <asp:TextBox runat="server" Text="<%# GetFilePath() %>" ID="Textbox1"/><br>
      <asp:HyperLink runat="server" NavigateUrl="<%# LogoPath.Value %>" 
        Font-Bold="True" Text="Show logo" ID="Hyperlink1"/><br>
      <input type="hidden" runat="server" ID="LogoPath" value="apress.gif" NAME="LogoPath">
      <b>
        <%# FilePath %>
      </b>
      <br>
      <img src="<%# GetFilePath() %>">
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class SingleValueBinding : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    this.DataBind();
    }
  protected string GetFilePath()
  {
    return "http://www.nfex.ru/style/logo.png";
  }
  protected string FilePath
  {
    get { return "http://www.nfex.ru/style/logo.png"; }
  }
}


Understanding Templates and DataBinding Expressions

<%@ 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 id="Head1" runat="server">
    <title>Show ItemTemplate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Repeater
        id="Repeater1"
        DataSourceId="srcDefaults"
        Runat="server">
        <ItemTemplate>
        <%#Eval("Title")%>
        <i>directed by</i>
        <%#Eval("Director")%>
        <hr />
        </ItemTemplate>
    </asp:Repeater>

    <asp:SqlDataSource
        id="srcDefaults"
        ConnectionString="Data Source=.\SQLExpress;
            AttachDbFilename=|DataDirectory|MyDatabase.mdf;
            Integrated Security=True;User Instance=True"
        SelectCommand="SELECT Title,Director FROM Defaults"
        Runat="server" />
    </div>
    </form>
</body>
</html>