在網站或應用程序中,要顯示數據信息,可用到ASP.NET提供的數據源控件和能夠顯示數據的控件。
一、數據源控件
數據源控件用於連接數據源、從數據源中讀取數據以及把數據寫入數據源。
1.數據源控件特點
使用數據源控件可以不用編寫任何代碼就可以實現頁面的數據綁定。
2.數據源控件的種類
.NET框架提供了如下幾個數據源控件:
- SqlDataSource,它用來訪問存儲在關系數據中的數據源,它與SQL Server一起使用時支持高級緩存功能。當數據作為DataSet對象返回時,此控件還支持排序、篩選和分頁。
- AccessDataSource,它主要用來訪問Microsoft Access數據庫。當數據作為DataSet對象返回時,支持排序、篩選和分頁。
- ObjectDataSource,它是表示具有數據檢索和更新功能的中間層對象,允許使用業務對象或其他類,並可創建依賴中間層對象管理數據的Web應用程序。
- XmlDataSource,它主要用來訪問XML文件,特別適用於分層的ASP.NET服務器控件,如TreeView或Menu控件。
- SiteMapDataSource,結合 ASP.NET 站點導航使用。
- LinqDataSource,使用 LinqDataSource 控件,可以在 ASP.NET 網頁中使用 LINQ,從數據表或內存數據集合中檢索數據。
- EntityDataSource,EntityDataSource 控件支持基於實體數據模型(EDM) 的數據綁定方案。此數據規范將數據表示為實體和關系集。它支持自動生成更新、插入、刪除和選擇命令以及排序、篩選和分頁。
二、數據綁定控件
數據綁定控件可以綁定到數據源控件,利用數據源控件提供的數據實現包括排序、分頁、更新、刪除和插入等對數據的操作功能,也可以通過編寫代碼實現。
1.數據綁定控件特點
數據綁定控件通過DataSourceID屬性連接到數據源控件。
2.數據源控件的種類
常用的數據綁定控件有:
- GridView控件,以表格的形式顯示數據,它是所有數據綁定控件中封裝功能最多,最完善的一個控件,在不編寫任務代碼的情況下可以實現對數據進行技術檢索、更新、刪除、排序和分頁等功能。也能運行代碼綁定。
- DataList控件,使用不同的布局來顯示數據記錄,如將數據記錄排成列或行。該控件提供了實現基本數據操作功能的常用命令,同時也提供了豐富的模板供用戶使用。
- DetailsView控件,以表格形式顯示數據,只是一次只能呈現一條記錄,並提供翻閱多條記錄以及插入、更新和刪除記錄功能。該控件通常和其他控件配合使用,如用GridView控件顯示基本信息,用DetailsView控件顯示相關的詳細信息。
- FormView控件,與DetailsView控件類似,它一次呈現數據源的一條記錄,並提供翻閱多條記錄以及插入、更新和刪除記錄功能。FormView控件使用自定義布局,在布局時會更靈活些 。
- Repeater控件,以只讀的方式顯示多條記錄,Repeater控件使用自定義布局。
- ListView控件,類似Repeater控件,它本身不提供分頁功能,借助DataPage控件實現分頁。
3.GridView控件
GridView控件的使用方式:
(1)通過任務編輯器操作GridView控件
(2)通過代碼操作GridView控件
【例】通過代碼操作GridView控件,實現數據的顯示、刪除、更新和分頁功能。
-----【顯示功能】
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; //引入命名空間 using System.Data.SqlClient; //引入命名空間 using System.Configuration; //引入命名空間 public partial class _2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack){ GetData(); } } protected void GetData() { SqlConnection con = new SqlConnection(); //定義數據庫連接對象 con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //定義數據庫連接字符串 SqlCommand com = new SqlCommand(); //定義數據庫操作命令對象 com.Connection = con; //連接數據庫 com.CommandText = "select CustomerID,CompanyName,ContactName,ContactTitle,Address from Customers"; //定義執行查詢操作的sql語句 SqlDataAdapter da = new SqlDataAdapter(); //創建數據適配器對象 da.SelectCommand = com; //執行數據庫操作命令 DataSet ds = new DataSet(); //創建數據集對象 da.Fill(ds,"customers"); //填充數據集 GridView1.DataSource = ds.Tables["Customers"].DefaultView;//設置gridview控件的數據源為創建的數據集ds GridView1.DataBind(); //綁定數據庫表中數據 } }
運行結果:
-----【分頁功能】
設置相關屬性。在源視窗中手動添加 AllowPaging="True" PageSize="8"
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" PageSize="8" AutoGenerateColumns="False" onpageindexchanging="GridView1_PageIndexChanging" > <Columns> <asp:BoundField DataField="CustomerID" HeaderText="編號" /> <asp:BoundField DataField="CompanyName" HeaderText="公司名稱" /> <asp:BoundField DataField="ContactName" HeaderText="聯系人" /> <asp:BoundField DataField="ContactTitle" HeaderText="聯系人頭銜 " /> <asp:BoundField DataField="Address" HeaderText="地址" /> </Columns> </asp:GridView>
后台代碼如下
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; GetData(); }
運行結果:
-----【選擇、編輯、更新功能】
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { GridView1.SelectedIndex = e.NewSelectedIndex; //實現選擇功能 } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; //實現編輯功能 GetData(); } protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1;//實現取消編輯功能 GetData(); } protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string customerId=GridView1.DataKeys[e.RowIndex][0].ToString();//取出修改行的主鍵值 //取出修改后各字段的值 string companyName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).ToString(); string contactName = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).ToString(); string address = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).ToString(); string phone = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).ToString(); //將用戶更新的數據修改數據庫 SqlConnection con = new SqlConnection(); //定義數據庫連接對象 con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //定義數據庫連接字符串 SqlCommand com = new SqlCommand(); //定義數據庫操作命令對象 com.Connection = con; //連接數據庫 string sql = "update Customers set CompanyName=@companyName,ContactName=@contactName,Address =@address, Phone=@phone where CustomerId =@customerId "; con.Open(); SqlCommand cmd = new SqlCommand(sql, con); cmd.Parameters.AddWithValue("@companyName", companyName); cmd.Parameters.AddWithValue("@contactName", contactName); cmd.Parameters.AddWithValue("@address", address); cmd.Parameters.AddWithValue("@phone", phone); cmd.Parameters.AddWithValue("@customerId", customerId); com.CommandText = sql.ToString(); com.ExecuteNonQuery(); con.Close(); GridView1.EditIndex = -1; GetData(); }
-----【刪除功能】
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string customerId = GridView1.DataKeys[e.RowIndex][0].ToString();//取出修改行的主鍵值 SqlConnection con = new SqlConnection(); //定義數據庫連接對象 con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; //定義數據庫連接字符串 SqlCommand com = new SqlCommand(); //定義數據庫操作命令對象 com.Connection = con; //連接數據庫 string sql = "delete from Customers where CustomerId=@customerId"; con.Open(); SqlCommand cmd = new SqlCommand(sql, con); com.Parameters.AddWithValue("@customerId", customerId); com.ExecuteNonQuery(); con.Close(); GridView1.EditIndex = -1; GetData(); }