日本国产亚洲-日本国产一区-日本国产一区二区三区-日本韩国欧美一区-日本韩国欧美在线-日本韩国欧美在线观看

當(dāng)前位置:雨林木風(fēng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

運(yùn)用 ASP+ 下文綁定控件(中)

運(yùn)用 ASP+ 下文綁定控件(中)

更新時(shí)間:2022-05-09 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

Repeater1Page 類覆蓋了 Page 類的 OnLoad 方法。此表示在對(duì)該頁
的首次請(qǐng)求中調(diào)用 DataBind。這將導(dǎo)致對(duì)這些頁上的數(shù)據(jù)綁定表達(dá)式求
值并使 repeater 控件列舉數(shù)據(jù)源以及創(chuàng)建其項(xiàng)目。僅在首次請(qǐng)求時(shí)調(diào)用
DataBind 方法。這之所以能正常工作是因?yàn)?repeater能夠在從前一次保
存狀態(tài)的回傳過程中重新創(chuàng)建其項(xiàng)目,而無需數(shù)據(jù)源實(shí)例。

  此頁將類型ICollection 的公用屬性顯露出來。這將在設(shè)置repeater
的 DataSource 屬性值的數(shù)據(jù)綁定表達(dá)式中使用。屬性的獲取實(shí)現(xiàn)使用包
含一組SiteInfo對(duì)象序列的ArrayList。此屬性是公用的,因?yàn)橹挥许擃?
的公用和保護(hù)成員可在數(shù)據(jù)綁定表達(dá)式中使用。

  每個(gè)SiteInfo 對(duì)象有兩個(gè)屬性:SiteName 和 SiteURL。當(dāng)對(duì)模板中
的HyperLink 控件進(jìn)行數(shù)據(jù)綁定時(shí)將訪問這些屬性。在此控件的綁定表達(dá)
式中,Container.DataItem 表示要將特定項(xiàng)綁定到其上的單個(gè) SiteInfo
對(duì)象。DataBinder.Eval(Container.DataItem, "SiteName") 訪問當(dāng)前
SiteInfo 對(duì)象的 SiteName 屬性。

  Repeater1 示例向您介紹了幾個(gè)基本概念:

●定義模板
●模板中的數(shù)據(jù)綁定語法和數(shù)據(jù)綁定表達(dá)式
●將 ArrayList 的 ICollection 表示用作數(shù)據(jù)源
●在最初處理頁的過程中調(diào)用 DataBind 方法

DataList 控件

  DataList控件是一個(gè)模板化控件,它提供使用樣式屬性可視化地格式
化其表示的能力。它也可以產(chǎn)生多列布局。

摘自 DataList1.aspx:

〈%@ Page language="C#" src="DataList1.cs" inherits="Samples.
DataList1Page"%〉
...

〈asp:DataList runat=server id="peopleDataList"
 RepeatColumns="2" RepeatDirection="Vertical" RepeatMode="Table"
 Width="100%"〉

 〈property name="AlternatingItemStyle"〉
  〈asp:TableItemStyle BackColor="#EEEEEE"/〉
 〈/property〉
 〈template name="ItemTemplate"〉
  〈asp:Panel runat=server font-size="12pt" font-bold="true"〉
   〈%# ((Person)Container.DataItem).Name %〉
  〈/asp:Panel〉
  〈asp:Label runat=server Width="20px"
   BorderStyle="Solid" BorderWidth="1px" BorderColor="Black"
   BackColor='〈%# ((Person)Container.DataItem).FavoriteColor
   %〉'〉  
  〈/asp:Label〉
    
  〈asp:Label runat=server Font-Size="10pt"
   Text='〈%# GetColorName(((Person)Container.DataItem).
   FavoriteColor) %〉'〉
  〈/asp:Label〉
 〈/template〉
〈/asp:DataList〉

此 .aspx 文件顯示了用來生成此示例的 DataList 的聲明。

  在此示例中,DataList 的多列布局是通過將 RepeatColumns 屬性設(shè)
置為“2”來實(shí)現(xiàn)的。將RepeatDirection設(shè)置為“Vertical”會(huì)使項(xiàng)目從
上到下、然后從左到右排列。相反,值設(shè)置為“Horizontal”會(huì)導(dǎo)致項(xiàng)目
從左到右、然后從上到下排列。

  aspx語法包含對(duì)少數(shù)幾種DataList的樣式屬性的設(shè)置。在此示例中,
DataList的Width被設(shè)置為其父級(jí)的100%。設(shè)置具灰色背景的Alternating
ItemStyle是為了獲得帶有條紋的外觀。此示例還說明模板可以包含任意
復(fù)雜的控件定義,以滿足在每個(gè)項(xiàng)目?jī)?nèi)獲得理想布局的需要。

  最后此模板中的數(shù)據(jù)綁定表達(dá)式通過將Container.DataItem轉(zhuǎn)換為其
類型來使用前期綁定。這不會(huì)招致與使用DataBinder.Eval(如 Repeater1
中所示)相關(guān)聯(lián)的后期綁定的代價(jià)。但是,這種方法可能會(huì)產(chǎn)生可讀性較
差的表達(dá)式。以下示例還給出了一個(gè)調(diào)用GetColorName方法(該方法是在
本頁有代碼支持的文件中實(shí)現(xiàn)的)的表達(dá)式示例。

DataList1.cs:

namespace Samples {
  ...

  public class DataList1Page : Page {
    protected DataList peopleDataList;

    protected string GetColorName(Color c) {
      return
       TypeDescriptor.GetConverter(typeof(Color)).Convert
       ToString(c);
    }

    private void LoadPeopleList() {
      // 創(chuàng)建數(shù)據(jù)源
      Person[] people = new Person[] {
        new Person("Nikhil Kothari", Color.Green),
        new Person("Steve Millet", Color.Purple),
        new Person("Chris Anderson", Color.Blue),
        new Person("Mike Pope", Color.Orange),
        new Person("Anthony Moore", Color.Yellow),
        new Person("Jon Jung", Color.MediumAquamarine),
        new Person("Susan Warren", Color.SlateBlue),
        new Person("Izzy Gryko", Color.Red)
      };

      // 設(shè)置控件的數(shù)據(jù)源
      peopleDataList.DataSource = people;

      // 并使該控件用此數(shù)據(jù)源構(gòu)建其項(xiàng)目
      peopleDataList.DataBind();
    }

    protected override void OnLoad(EventArgs e) {
      base.OnLoad(e);

      if (!IsPostBack) {
        // 首次請(qǐng)求此頁
        LoadPeopleList();
      }
    }
  }

  public sealed class Person {
    private string name;
    private Color favoriteColor;

    public Person(string name, Color favoriteColor) {
       this.name = name;
       this.favoriteColor = favoriteColor;
    }

    public Color FavoriteColor {
      get { return favoriteColor; }
    }
    public string Name {
      get { return name; }
    }
  }
}

  在此頁中,控件的 DataSource 屬性是通過程序設(shè)置的,與在aspx文
件中聲明性地設(shè)置相對(duì)。兩種方法的結(jié)果相同。無法選擇哪種方法,都必
須調(diào)用 DataBind 方法,以便控件可以列舉其數(shù)據(jù)源并創(chuàng)建它要表示的項(xiàng)
目。

  此示例中所用的數(shù)據(jù)源是 Person 對(duì)象的一個(gè)簡(jiǎn)單數(shù)組。由于每個(gè)數(shù)
組都實(shí)現(xiàn)ICollection方法,所以數(shù)組適合用作數(shù)據(jù)源。這顯示了將數(shù)據(jù)
結(jié)構(gòu)和類型用作數(shù)據(jù)源時(shí)可獲得的靈活程度。

DataList1 示例介紹了下列概念:

●在模板中定義豐富的 HTML UI
●使用簡(jiǎn)單數(shù)組作為數(shù)據(jù)源
●通過程序設(shè)置數(shù)據(jù)源
●數(shù)據(jù)綁定語法中所允許的各種表達(dá)式

DataGrid 控件

  DataGrid 控件使您可以生成數(shù)據(jù)源格式豐富的列表表示。此外,它
還支持隨其它操作選擇項(xiàng)目。

  本節(jié)的四個(gè)示例使用包含有關(guān)書名信息(標(biāo)題、標(biāo)題ID、作者、價(jià)格
和出版日期)的表。全部數(shù)據(jù)都用TitlesDB.xml中的XML予以維持。在建
立頁面來表示此表的內(nèi)容并選擇書籍時(shí),這些示例遵循增量方法。代碼列
表包含黑體文本,以表明一個(gè)示例構(gòu)建于以前示例時(shí)所作的更改。

截自 TitlesDB.xml:

〈root〉
〈schema id="DocumentElement" targetNamespace=""
    xmlns=http://www.w3.org/1999/XMLSchema
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"〉
  〈element name="Title"〉
    〈complexType content="elementOnly"〉
      〈element name="title_id" type="string"〉〈/element〉
      〈element name="title" type="string"〉〈/element〉
      〈element name="au_name" type="string"〉〈/element〉
      〈element name="price" msdata:DataType="System.
      Currency"
           minOccurs="0"
           type="string"〉〈/element〉
      〈element name="pubdate" type="timeInstant"〉
      〈/element〉
    〈/complexType〉
    〈unique name="TitleConstraint" msdata:PrimaryKey="True"〉
      〈selector〉.〈/selector〉
      〈field〉title_id〈/field〉
    〈/unique〉
  〈/element〉
〈/schema〉
〈DocumentElement〉
  〈Title〉
    〈title_id〉BU1032〈/title_id〉
    〈title〉The Busy Executive's Database Guide〈/title〉
    〈au_name〉Marjorie Green〈/au_name〉
    〈price〉19.99〈/price〉
    〈pubdate〉1991-06-12T07:00:00〈/pubdate〉
  〈/Title〉
  ...
〈/DocumentElement〉
〈/root〉

  在典型的Web應(yīng)用程序中,為了獲得最大的可伸縮性和性能上的好處,
很可能會(huì)使用 Web服務(wù)或商業(yè)對(duì)象來存取數(shù)據(jù)。為了簡(jiǎn)化這些示例并將注
意力集中在使用 DataGrid 而不是數(shù)據(jù)存取上,我們選擇在應(yīng)用程序啟動(dòng)
時(shí)一次性加載數(shù)據(jù),并在 Global.asax 中的 ASP 應(yīng)用程序狀態(tài)中高速緩
存所得的DataSet,如下所示。

截自 Global.asax:

public void Application_OnStart() {
  FileStream fs = null;
  DataSet ds = null;

  try {
    fs = new FileStream(Server.MapPath("TitlesDB.xml"),
    FileMode.Open,
              FileAccess.Read);
    ds = new DataSet();

    // 將 xml 文件中的數(shù)據(jù)加載到 DataSet 中
    ds.ReadXml(fs);
  } finally {
    if (fs != null) {
      fs.Close();
      fs = null;
    }
  }

  // 將數(shù)據(jù)集高速緩存到應(yīng)用程序狀態(tài)中,以便在單個(gè)頁面中使用
  Application["TitlesDataSet"] = ds;
}

DataGrid1

  DataGrid1說明DataGrid的基本用法,說明控件如何用最少的用戶代
碼生成表示來提供豐富的功能。

截自 DataGrid1.aspx:

〈%@ Page language="C#" src="DataGrid.cs" inherits="Samples.Data
GridPage"%〉
...

〈asp:DataGrid runat=server id="titlesGrid"〉
〈/asp:DataGrid〉

  上面的.aspx 文件顯示在不設(shè)置 DataGrid 控件任何屬性的情況下對(duì)
其進(jìn)行聲明。

DataGrid.cs:

namespace Samples {
  ...

  public class DataGridPage : Page {
    protected DataGrid titlesGrid;

    public ICollection GetTitlesList() {
      // 從在應(yīng)用程序狀態(tài)中高速緩存的 DataSet 中檢索標(biāo)題列
      表。
      DataSet titlesDataSet = (DataSet)Application["Titles
      DataSet"];

      if (titlesDataSet != null) {
        return titlesDataSet.Tables["Title"].DefaultView;
      }
      else {
        return null;
      }
    }

    private void LoadTitlesGrid() {
      // 從數(shù)據(jù)庫中檢索數(shù)據(jù)
      ICollection titlesList = GetTitlesList();

      // 設(shè)置控件的數(shù)據(jù)源
      titlesGrid.DataSource = titlesList;

      // 并使它用此數(shù)據(jù)源構(gòu)建其項(xiàng)目
      titlesGrid.DataBind();
    }

    protected override void OnLoad(EventArgs e) {
      base.OnLoad(e);

      if (!IsPostBack) {
        // 首次請(qǐng)求此頁
        LoadTitlesGrid();
      }
    }
  }
}

  .cs文件包含用于此頁的代碼。此代碼與DataList1示例中使用的代碼
功能相同。在對(duì)此頁的首次請(qǐng)求中,它覆蓋 OnLoad 方法以檢索數(shù)據(jù)并在
調(diào)用DataBind之前設(shè)置控件的DataSource屬性。這將使DataGrid創(chuàng)建其項(xiàng)
目,這些項(xiàng)目是表中必要的行。在回傳處理的過程中,DataGrid從狀態(tài)
(該狀態(tài)包括在上一次請(qǐng)求中所保存的單元格內(nèi)容)重新創(chuàng)建項(xiàng)目。

  此示例說明了 DataGrid 控件的 AutoGenerateColumns 屬性的功能。
此屬性的默認(rèn)值為 true。當(dāng)設(shè)置為 true時(shí),DataGrid將使用reflection
檢查其數(shù)據(jù)源和對(duì)象,并為每個(gè)公用屬性或字段創(chuàng)建一個(gè)列。在此示例中,
控件表示“標(biāo)題”表中當(dāng)前的所有字段。這一功能允許用最少的用戶代碼
快速而容易地生成任何數(shù)據(jù)源的列表表示。

  每個(gè)自動(dòng)生成列的類型都是BoundColumn。這種列類型將與其關(guān)聯(lián)的
屬性值轉(zhuǎn)換為要用作表單元格文本的字符串。

DataGrid2

  DataGrid2說明具有在.aspx文件中定義的Columns集合的DataGrid。

摘自 DataGrid2.aspx:

〈%@ Page language="C#" src="DataGrid.cs" inherits="Samples.Data
GridPage"%〉
...

〈asp:DataGrid runat=server id="titlesGrid"
   AutoGenerateColumns="false"〉
 〈property name="Columns"〉
  〈asp:BoundColumn headerText="Title" DataField="title"/〉
  〈asp:BoundColumn headerText="Author" DataField="au_name"/〉
  〈asp:BoundColumn headerText="Date Published" DataField="
  pubdate"/〉
  〈asp:BoundColumn headerText="Price" DataField="price"/〉
 〈/property〉
〈/asp:DataGrid〉

  此.aspx文件顯示了一個(gè)具有用戶指定的列集合的 DataGrid 控件。
此示例使用與 DataGrid1 相同的有代碼支持的文件,因?yàn)椴恍枰娜?
何代碼。

  DataGrid的AutoGenerateColumns屬性被設(shè)置為假,從而阻止控件自
動(dòng)生成列,而讓用戶負(fù)責(zé)定義將要在表中表示的列。

有許多好處:
●您可控制列的順序。以聲明的順序表示列。另一方面,自動(dòng)生成的列是
按用映像檢索到的順序表示的,此順序不必與代碼中的列順序或數(shù)據(jù)庫表
本身的列順序相匹配。
●可以用列的headerText屬性來指定每列的標(biāo)頭。在前一個(gè)示例中,列標(biāo)
頭指明了字段名,這可能并不合適。當(dāng)在此模式下使用控件時(shí),Columns
還提供其它可設(shè)置的屬性。
●自動(dòng)生成的列的類型始終是 BoundColumn。指定列集合使用戶可以控制
每列的類型。


溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!

本類教程下載

系統(tǒng)下載排行

主站蜘蛛池模板: youwu在线影院 | 添人人躁日日躁夜夜躁夜夜揉 | 成人午夜免费视频免费看 | 人体自拍亚洲综合图区 | 国产清纯91天堂在线观看 | 国产一区风间由美在线观看 | 国产成人精品在视频 | 日韩毛片免费看 | 91天堂国产在线 在线播放 | 日韩高清在线日韩大片观看网址 | 午夜国产精品色福利视频 | 国产精品亚洲综合色区韩国 | 日韩精品久久久久影院 | 免费三片在线观看网站 | 99久久这里只精品麻豆 | 18女人毛片水真多免费 | 国产成人综合一区人人 | 久久久夜色精品国产噜噜 | 欧洲自拍偷拍 | 天天曰天天操 | 欧美另类孕交videos | 99热久这里都是精品小草 | 国产成人亚洲综合网站不卡 | 欧美日韩亚洲二区在线 | 色福利在线 | 亚洲国产成人久久综合一区 | 视频一区 国产 | 99爱在线精品视频免费观看9 | 国产精品国产精品国产专区不卡 | 男人和女人做免费做爽爽视频 | 四虎精品免费国产成人 | 怡红院在线播放 | 动漫精品一区二区三区视频 | 99热国产在线观看 | 国产精品私人玩物在线观看 | 韩国午夜理伦三级2020宅男 | 亚洲另类欧美日韩 | 欧美视频久久久 | 欧美色综合天天综合高清网 | 久久久久久久久久久9精品视频 | 日本xxww |