国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > ASP > 正文

使用 ASP+ 列表綁定控件(下)

2019-11-18 21:13:41
字體:
來源:轉載
供稿:網友
DataGrid3

  DataGrid3通過添加可視格式化和內容格式化構建于DataGrid2之上。

摘自 DataGrid3.aspx:

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

〈asp:DataGrid runat=server id="titlesGrid"
   AutoGenerateColumns="false"
   Width="80%"
   BackColor="White"
   BorderWidth="1px" BorderStyle="Solid" CellPadding="2" Cell
   Spacing="0"
   BorderColor="Tan"
   Font-Name="宋體" Font-Size="8pt"〉
 〈PRoperty name="Columns"〉
  〈asp:BoundColumn headerText="Title" DataField="title"/〉
  〈asp:BoundColumn headerText="Author" DataField="au_name"/〉
  〈asp:BoundColumn headerText="Date Published" DataField=
  "pubdate"
     DataFormatString="{0:MMM yyyy}"/〉
  〈asp:BoundColumn headerText="Price" DataField="price"
   DataFormatString="{0:c}"〉
   〈property name="ItemStyle"〉
    〈asp:TableItemStyle HorizontalAlign="Right"/〉
   〈/property〉
  〈/asp:BoundColumn〉
 〈/property〉

 〈property name="headerStyle"〉
  〈asp:TableItemStyle BackColor="DarkRed" ForeColor="White"
   Font-Bold="true"/〉
 〈/property〉
 〈property name="ItemStyle"〉
  〈asp:TableItemStyle ForeColor="DarkSlateBlue"/〉
 〈/property〉
 〈property name="AlternatingItemStyle"〉
  〈asp:TableItemStyle BackColor="Beige"/〉
 〈/property〉
〈/asp:DataGrid〉

  此.aspx文件顯示了與前面相同的DataGrid控件聲明,并設置了各種
樣式屬性。這將導致視覺上更具吸引力的表示。仍就不需要對代碼進行任
何更改,使用與以前示例相同的有代碼支持的文件。

  因為它是從 WebControl 得到的,所以 DataGrid 控件繼承了諸如
Width、BackColor、BorderStyle 和 Font.Name 之類的樣式屬性。此外,
DataGrid提供諸如CellPadding這樣的屬性,這些屬性是特定于表的。這
些屬性允許從總體上定制控件。

  聲明還顯示了設置的若干項目樣式,如headerStyle和Alternating
ItemStyle。這些樣式控制著它們相應項目的外觀。請注意此示例中出現
的樣式合并。備選項目與一般項目的前景色相同,因為它們的樣式是
AlternatingItemStyle和ItemStyle的組合。最后,此示例還通過右對齊
價格列中的文本說明了為特定列設置樣式。

  DataGrid還允許您格式化其單元格中的文本內容。這是通過設置Bound
Column的DataFormatString屬性值完成的。該列使用其格式說明格式化使
用 String.Format的單元格內容。此屬性可隨格式化類型(如日期或貨幣)
一起預置或附加任意內容。此外,由于格式化考慮了當前頁的CultureInfo
和請求,所以它也支持全局化。如果未指定格式,則使用該值的ToString
方法。

DataGrid4

  DataGrid4 說明如何通過處理 SelectedIndexChanged 事件來利用
DataGrid 中的選擇。

截自 DataGrid4.aspx:

〈%@ Page language="C#" src="DataGrid4.cs" inherits="Samples.
DataGrid4Page"%〉
...

〈asp:DataGrid runat=server id="titlesGrid"
   AutoGenerateColumns="false"
   Width="80%"
   BackColor="White"
   BorderWidth="1px" BorderStyle="Solid" CellPadding="2"
   CellSpacing="0"
   BorderColor="Tan"
   Font-Name="宋體" Font-Size="8pt"
   DataKeyField="title_id"
   OnSelectedIndexChanged="OnSelectedIndexChangedTitlesGrid"〉
 〈property name="Columns"〉
  〈asp:ButtonColumn Text="Select" Command="Select"/〉
  〈asp:BoundColumn headerText="Title" DataField="title"/〉
  〈asp:BoundColumn headerText="Author" DataField="au_name"/〉
  〈asp:BoundColumn headerText="Date Published" DataField=
  "pubdate"
     DataFormatString="{0:MMM yyyy}"/〉
  〈asp:BoundColumn headerText="Price" DataField="price"
   DataFormatString="{0:c}"〉
   〈property name="ItemStyle"〉
    〈asp:TableItemStyle HorizontalAlign="Right"/〉
   〈/property〉
  〈/asp:BoundColumn〉
 〈/property〉

 〈property name="headerStyle"〉
  〈asp:TableItemStyle BackColor="DarkRed" ForeColor="White"
   Font-Bold="true"/〉
 〈/property〉
 〈property name="ItemStyle"〉
  〈asp:TableItemStyle ForeColor="DarkSlateBlue"/〉
 〈/property〉
 〈property name="AlternatingItemStyle"〉
  〈asp:TableItemStyle BackColor="Beige"/〉
 〈/property〉
 〈property name="SelectedItemStyle"〉
  〈asp:TableItemStyle BackColor="PaleGoldenRod" Font-Bold=
  "true"/〉
 〈/property〉
〈/asp:DataGrid〉
...
〈asp:Label runat=server id="selectionInfoLabel" Font-Name="宋體"
Font-Size="8pt"/〉

  在此.aspx文件中,為DataGrid的SelectedIndexChanged事件注冊了
一個事件處理程序。此事件處理程序是在有代碼支持的文件中實現的。已
在列集合中添加了一個命令為“Select”的 ButtonColumn,使得DataGrid
為每個項目表示一個包含Select按鈕的附加列。同時指定了SelectedItem
Style。此樣式用于從視覺上區(qū)分選定的項目。最后還指定了 DataGrid的
DataKeyField屬性。此字段將置入DataGrid的DataKeys集合,該集合將在
有代碼支持的文件中用到。

DataGrid4.cs:

namespace Samples {
  ...

  public class DataGrid4Page : Page {
    protected DataGrid titlesGrid;
    protected Label selectionInfoLabel;

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

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

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

      // 設置控件的數據源并重新設置其選擇,
      titlesGrid.DataSource = titlesList;
      titlesGrid.SelectedIndex = -1;

      // 并使該控件使用此數據源構建其項目
      titlesGrid.DataBind();

      // 更新選定的標題信息
      UpdateSelectedTitleInfo();
    }

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

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

    // 處理 DataGrid 的 OnSelectedIndexChanged 事件
    protected void OnSelectedIndexChangedTitlesGrid(object
    sender,
                            EventArgs
                            e) {
      UpdateSelectedTitleInfo();
    }

    private void UpdateSelectedTitleInfo() {
      // 獲取選定的索引
      int selIndex = titlesGrid.SelectedIndex;
      string selTitleID = null;
      string selectionInfo;

      if (selIndex != -1) {
        // 顯示選定標題的關鍵字段
        selTitleID = (string)titlesGrid.DataKeys[selIndex];
        selectionInfo = "ID of selected title: " +
        selTitleID;
      }
      else {
        selectionInfo = "No title is currently selected.";
      }

      selectionInfoLabel.Text = selectionInfo;
    }
  }
}

  此 .cs 文件包含處理 SelectedIndexChanged 事件以及在 DataGrid
下顯示選定標題的ID的邏輯。DataGrid處理命令事件,該事件是通過包含
在其項目中的按鈕觸發(fā)的。它識別標準命令“Select”,該命令使其更改
它的SelectedIndex屬性,并通過觸發(fā)此事件來將此更改通知用戶的代碼。

  在實現事件處理程序的過程中,示例代碼調用 UpdateSelectedTitle
Info 方法。該方法負責顯示有關選定書名的信息,本例中為標題的 ID。
在更現實的方案中,此 ID 可用來鏈接某個頁面,以顯示有關選定標題的
更多詳細信息。

  ID 是通過訪問 DataKeys 集合進行檢索的。該集合是因為設置了
DataKeyField屬性而置入的。通常,將它設置為主關鍵字或使用戶可以唯
一標識項目的某些其它字段,并將此信息用作后續(xù)的數據庫查詢或過濾數
據中的準則。

  此示例說明除了僅僅表示數據源中的對象之外,如何進一步支持諸如
選擇數據源中對象之類的操作。DataGrid 包含對若干其它特性(如排序、
分頁、現場編輯和TemplateColumns)的支持。但是,這些特定特性超出
了本文的討論范圍,將在以后的文章中加以探討。

Repeater、DataList 或 DataGrid?

  Repeater、DataList和DataGrid控件共享公用編程模型。同時,每個
控件都被設計為側重某個特定方案,為正確的方案選擇正確的列表綁定控
件是一個重要的決策。本節(jié)說明控件層次結構和每種控件的功能,以及每
種控件可能用于的典型方案的示例。

  正如在下面的類層次結構中看到的那樣,Repeater是一種小巧輕便的
控件。它只繼承了基本Control類的功能,如 ID 屬性和子控件集合。另
一方面,DataList 控件和 DataGrid 控件都繼承了 WebControl 功能,
如樣式和外觀屬性。

  在對象模型方面,repeater控件是最簡單的控件。它同時也是最小的
數據綁定控件并且基本上是不同的,即它不會強制使用任何特殊的UI布局。
最后的表示遵循生成文本的方法,其方式是通過重復為此控件指定的模板
內容。此控件對樣式和外觀屬性或行為不提供任何內建的支持。對于需要
完全控制表示的方案而言,它是一個極好的選擇。

  DataList 控件是強制使用分列布局或流布局的 repeater。它繼承了
WebControl 中實現的外觀屬性,并增加了適用于它所創(chuàng)建的項目的其它
樣式屬性。DataList控件還包括對其項目標準操作(如選擇、編輯和刪除)
的支持。它很適用于生成分布于一列或多列的水平或垂直的項目序列流。

  DataGrid控件強制使用列或行的列表布局。與DataList類似,此控件
提供樣式和外觀屬性。除選擇和編輯之外,DataGrid還支持對整個項目集
合的高級操作,如分頁和排序。DataGrid 和 DataList 的一個主要區(qū)別
是 DataGrid 不包含任何模板屬性,即 DataGrid 控件的項目或行是非模
板化的。但是,將 TemplateColumn 添加到 DataGrid 中就可以在特定列
中使用模板。

下表是列表綁定控件所提供的功能的摘要。

功能                   Repeater    DataList    DataGrid
模板                   是(必需)  是(必需)  列內(可選)
列表布局               否          否          是
流布局                 是          是          否
分列/報紙欄目樣式布局  否          是          否
樣式和外觀屬性         否          是          是
選擇                   否          是          是
編輯                   否          是          是
刪除                   否          是          是
分頁                   否          否          是
排序                   否          否          是


相關資源

  隨Microsoft .NET Framework SDK 發(fā)布的QuickStart示例包含這些
控件的若干示例,以及說明使用 xml 和 Web 服務存取數據的示例。SDK
附帶的文檔包括相關主題的概念性資料,如ASP+頁面框架和服務器控件,
以及說明作為此框架一部分的控件的對象模型的參考書目。  


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 丁青县| 九龙城区| 嵊州市| 富民县| 梨树县| 瑞昌市| 河间市| 鹤山市| 石台县| 南开区| 和顺县| 泰州市| 鹰潭市| 元氏县| 奇台县| 山丹县| 收藏| 北海市| 镇平县| 元朗区| 高密市| 丹东市| 喀什市| 汉寿县| 隆德县| 邵武市| 台山市| 清新县| 华坪县| 韶山市| 平塘县| 崇仁县| 德令哈市| 北安市| 长寿区| 桑植县| 黔西县| 辽中县| 延川县| 广平县| 临江市|