麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

ASP.NET數據控件

2019-11-14 15:46:40
字體:
來源:轉載
供稿:網友

  數據服務器控件就是能夠顯示數據的控件,與那些簡單格式的列表控件不同,這些控件不但提供顯示數據的豐富界面(可以顯示多行多列數據并根據用戶定義來顯示),還提供了修改、刪除和插入數據的接口。

  asp.net 4.0提供的主要數據服務器控件:

    1.GridView:是一個全方位的網格控件,能夠顯示一整張表的數據,它是ASP.NET中最為重要的數據控件。

    2.DetailsView :是用來一次顯示一條記錄。

    3.FormView :也是用來一次顯示一條記錄,與DetailsView不同的是,FormView是基于模板的,可以使布局具有靈活性。

    4.DataList :可用來自定義顯示各行數據庫信息,顯示的9格式在創建的模板中定義。

    5.Repeater :生成一系列單個項,可以使用模板定義頁面上單個項的布局,在頁面運行時,該控件為數據源中的每個項重復相應的布局。

    6.ListView :可以綁定從數據源返回的數據并顯示它們,它會按照使用模板和樣式定義的格式顯示數據。

    7.Chart :新增的圖表控件,能實現柱狀圖、餅圖等多種圖表數據。

  GridView控件

    GridView 控件,可以顯示、編輯和刪除來自不同數據源的數據

    GridView控件的主要功能:

      1.綁定和顯示數據。

      2.對綁定其中的數據進行選擇、排序、分頁、編輯和刪除。

      3.自定義列和樣式。

      4.自定義用戶界面元素。

      5.在事件處理程序中加入代碼來完成與GridView控件的交互。

    GridView控件的常用屬性

      1.AllowPaging:獲取或設置指示是否啟用分頁的值。

      2.AllowSorting:獲取或設置指示是否啟用排序的值。

      3.AlternatingRoeStyle:獲取對TableItemStyle對象的引用,使用該對象可以設置GridView控件中的交替數據行的外觀。

      4.AutoGenerateColumns:獲取或設置一個值,該值指示是否為數據源中的每一字段自動創建BoundColumn對象并在GridView控件中顯示這些對象。

      5.AutoGenerateDeleteButton:獲取或設置一個值,該值指示每一個數據行都帶有刪除按鈕的CommandField字段列是否自動添加到GridView控件。

      6.AutoGenerateSelectButton:獲取或設置一個值,該值指示每一個數據行都帶有選擇按鈕的CommandField字段列是否自動添加到GridView控件。

      7.BottomPagerRow:獲取一個GridViewRow對象,該對象表示GridView控件中的底部頁導航行。

      8.Columns:獲取表示GridView控件各列的對象集合。

      9.DataMember:當數據源包含多個不同的數據項列表時,獲取或設置數據綁定控件綁定到的數據列表的名稱。

      10.PageIndex:獲取或設置當前顯示頁的索引。

      11.DataSource:獲取或設置源,該源包含用于填充控件中的項的值列表。

      12.PageCount:獲取顯示GridView控件中各項所需的總頁數。

      13.PageSize:獲取或設置要在GridView控件的單頁上顯示的項數。

      14.SelectedIndex:獲取或設置GridView控件中選定項的索引。

    GridView 控件的常用方法

      1.DataBind:將數據源綁定到GridView控件。

      2.DeleteRow:從數據源中刪除位于指定索引位置的記錄。

      3.IsBindableType:確定指定的數據類型是否能綁定到GridView控件中的列。

      4.Sort:根據指定的排序表達式和方向對GridView控件進行排序。

      5.UpdateRow:使用行的字段值更新位于指定行索引位置的記錄。 

    GridView 控件的事件

      1.PageIndexChanged :在單擊某一頁導航按鈕時,但在GridView控件處理分頁操作之后發生。

      2.PageIndexChanging:在單擊某一頁導航按鈕時,但在GridView控件處理分頁操作之前發生。

      3.RowCancelingEdit:單擊編輯模式中某一行的取消按鈕之后,在該行退出編輯模式之前發生。

      4.RowCommand:當單擊GridView控件中的按鈕時發生。 

      5.RowCreate:在GridView控件中創建行時發生。

      6.RowDataBound:在GridView控件中將數據行綁定到數據時發生。

      7.RowDeleted :在單擊某一行的刪除按鈕時,但在GridView控件刪除該行之后發生。

      8.RowDeleting:在單擊某一行的刪除按鈕時,但在GridView控件刪除該行之前發生。

      9.RowEditing:發生在單擊某一行的“編輯”按鈕以后,GridView控件進入編輯模式之前。

      10.RowUpdated:發生在單擊某一行的“更新”按鈕,并且GridView控件對該行進行更新之后。 

      11.SelectedIndexChanged :發生在單擊某一行的選擇按鈕后,GridView控件對相應的選擇操作進行處理之后。 

      12.SelectedIndexChanging:發生在單擊某一行的選擇按鈕后,GridView控件對相應的選擇操作進行處理之前。

      13.Sorted:在單擊用于列排序的超鏈接時,但在 GridView控件對相應的排序操作進行處理之后發生。

      14.Sorting:在單擊用于列排序的超鏈接時,但在GridView控件對相應的排序操作進行處理之前發生。

    GridView控件中的綁定數據。

      在GridView控件中綁定數據有兩種方式:一是使用多值綁定,二是使用數據源控件。

        1.使用多值綁定。使用多值綁定方法綁定GridView控件。

          在Web.config文件中添加如下代碼:

1   <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

          

          在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" > 3             <AlternatingRowStyle BackColor="White" /> 4             <EditRowStyle BackColor="#2461BF" /> 5             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 6             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 7             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 8             <RowStyle BackColor="#EFF3FB" /> 9             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />10             <SortedAscendingCellStyle BackColor="#F5F7FB" />11             <SortedAscendingHeaderStyle BackColor="#6D95E1" />12             <SortedDescendingCellStyle BackColor="#E9EBEF" />13             <SortedDescendingHeaderStyle BackColor="#4870BE" />14         </asp:GridView>15     </div>
View Code

          Default.aspx.cs文件中的代碼如下:

 1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Configuration; 8 using System.Web.UI; 9 using System.Web.UI.WebControls;10 11 namespace Webapplication112 {13     public partial class Default : System.Web.UI.Page14     {15         PRivate string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;16         protected void Page_Load(object sender, EventArgs e)17         {18             if (!this.IsPostBack)19             {20                 Binding();21             }22         }23         private void Binding()24         {25             string selectSql = "select * from Student";//獲取學生信息26             SqlConnection connection = new SqlConnection(connectionString);27             SqlCommand command = new SqlCommand(selectSql, connection);28             SqlDataAdapter dataAdapter = new SqlDataAdapter(command);29             DataSet dataSet = new DataSet();30             //打開數據庫并讀取信息31             try32             {33                 connection.Open();34                 dataAdapter.Fill(dataSet, "Student");35                 //把存儲在DataSet中的數據綁定到GridView控件中36                 this.GridView1.DataSource = dataSet.Tables["Student"].DefaultView;37                 this.GridView1.DataBind();38             }39             catch (Exception ex)40             {41                 Response.Write("讀取過程發生錯誤:" + ex.Message.ToString());42             }43             finally44             {45                 connection.Close();46             }47         }48     }49 }
View Code

        2.使用數據源控件。使用數據源控件綁定GridView控件。

          在Web.config文件中添加如下代碼:

1   <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

          在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" 3              ProviderName="System.Data.SqlClient" DataSourceMode="DataReader" SelectCommand="select * from [Student]"> 4         </asp:SqlDataSource> 5         <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" DataSourceID="SqlDataSource1" > 6             <AlternatingRowStyle BackColor="White" /> 7             <EditRowStyle BackColor="#2461BF" /> 8             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 9             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />10             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />11             <RowStyle BackColor="#EFF3FB" />12             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />13             <SortedAscendingCellStyle BackColor="#F5F7FB" />14             <SortedAscendingHeaderStyle BackColor="#6D95E1" />15             <SortedDescendingCellStyle BackColor="#E9EBEF" />16             <SortedDescendingHeaderStyle BackColor="#4870BE" />17         </asp:GridView>18     </div>
View Code

 

  GridView控件的列

    把GridView控件的屬性AutoGenerateColumns設置為true,在控件中顯示的列都是自動生成的。但在很多種情況下,GridView控件的每一列的顯示都需要實現定義。

    GridView控件提供了幾種類型的列以方便操作:

      1.BoundField:顯示數據源中某個字段的值,它是GridView控件的默認列類型。

      2.ButtonField:為GridView控件中的每個項顯示一個命令按鈕。

      3.CheckBoxField:為GridView控件中的每一項顯示一個復選框。

      4.CommandField:顯示用來執行選擇、編輯和刪除操作的預定義命令按鈕。

      5.HyperLinkField:將數據源中某個字段的值顯示為超鏈接。

      6.ImagField:為GridView控件中的每一項顯示一個圖象。

      7.TemplateField:根據指定的模版為GridView控件中每一項顯示用戶定義的內容。 

    所有的列的編輯都可以通過字段對話框來進行,進入字段對話框的方式有兩種:

      1.選中要編輯的GridView控件,單擊右上角的小按鈕,在彈出的快捷菜單中選擇編輯列,命令即可彈出字段對話框。

      2.選中要編輯的GridView控件,在屬性窗口中找到Columns屬性,選中該屬性,單擊在該屬性最右邊出現的按鈕即可彈出字段對話框。

    實現定義GridView控件的各個列,需要把AutoGenerateColumns設置為false。避免在GridView控件中出現不必要的列。

  GridView控件的排序。

    GridView控件支持對顯示在其中的數據進行排序,只需要把屬性AllowSorting設置為true即可。

    實現排序功能:

      在Web.config文件中添加如下代碼:

1   <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

      在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" 3              ProviderName="System.Data.SqlClient" DataSourceMode="DataSet" SelectCommand="select * from [Student]"> 4         </asp:SqlDataSource> 5         <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" DataSourceID="SqlDataSource1"  6             AutoGenerateColumns="False" AllowSorting="true"> 7             <AlternatingRowStyle BackColor="White" /> 8             <Columns> 9                 <asp:ButtonField DataTextField="stuNo" Text="按鈕" HeaderText="學號" SortExpression="stuNo" />10                 <asp:ButtonField DataTextField="Class" Text="按鈕" HeaderText="班級" SortExpression="Class" />11                 <asp:ButtonField DataTextField="stuName" Text="按鈕" HeaderText="姓名" SortExpression="stuName" />12                 <asp:ButtonField DataTextField="Age" Text="按鈕" HeaderText="年齡" SortExpression="Age" />13             </Columns>14             <EditRowStyle BackColor="#2461BF" />15             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />16             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />17             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />18             <RowStyle BackColor="#EFF3FB" />19             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />20             <SortedAscendingCellStyle BackColor="#F5F7FB" />21             <SortedAscendingHeaderStyle BackColor="#6D95E1" />22             <SortedDescendingCellStyle BackColor="#E9EBEF" />23             <SortedDescendingHeaderStyle BackColor="#4870BE" />24         </asp:GridView>25     </div>
View Code

    其實GridView控件自己并不執行排序,它是通過數據源控件實現排序的。GridView控件只不過提供一個顯示和操作的數據界面而已,而真實的數據排序是它所綁定到的數據源控件。如果所綁定的數據源控件可以排列數據,則選擇數據后,GridView控件可以通過列的屬性SortExpression傳遞給數據源與該數據源控件進行交互并請求排序后的數據。

    并不是所有的數據源控件都支持排序的,支持排序的數據源控件所需要的配置如下:

      1.SqlDataSource控件的屬性DataSourceMode設置為DataSet,或屬性SortParameterName設置為DataSetDataReader

      2.accessDataSource控件的屬性DataSourceMode設置為DataSet,或屬性SortParameterName設置為DataSetDataReader

      3.ObjectDataSource控件的屬性SortParameterName設置為基礎對象所支持的屬性值。

    當所綁定的數據源控件可以排序數據時,只要將GridView控件的AllowSorting屬性設置為true,即可啟動該控件中的默認排序行為。將此屬性設置為true會使GridView控件將LinkButton控件呈現在列標題中。此外,該控件還將每一列的SortExpression屬性隱式設置為它所綁定到的數據字段的名稱。

    默認情況下,當把AllowSorting屬性設置為true時,GridView控件將支持所有列可排序,但可以通過設置列的屬性SortExpression為空字符串禁用對這個列的排序操作。

    還可以對GridView控件進行自定義排序行為,可以在Sorting事件中編寫自定義的排序代碼。在Sorting事件處理程序中可以進行如下的操作:

      1.自定義傳遞給數據源控件的排序表達式。默認情況下,排序表達式是單個列的名稱,可以在處理程序中修改排序表達式,然后將修改過的排序表達式傳遞給數據源控件。

      2.創建自己的排序邏輯。如果使用的數據源不支持排序,則可以用自己的代碼執行排序,然后將網格綁定到排序后的數據。

  GridView控件的分頁

    GridView控件支持對所綁定的數據源中的項進行分頁。

    屬性AllowPaging決定是否使用分頁顯示。AllowingPaging屬性設置為true時,PagerSettings屬性允許自定義GridView控件的分頁界面。

    PagerSettings屬性對應PagerSettings類,它提供一些屬性,這些屬性支持自定義GridView控件的分頁界面,PagerSettings類的屬性如下:

      1.FirstPageImageUrl:獲取或設置為第一頁按鈕顯示的圖像URL

      2.FirstPageText:獲取或設置為第一頁按鈕顯示的文字。

      3.LastPageImageUrl:獲取或設置為最后一頁按鈕顯示的圖像URL

      4.LastPageText:獲取或設置為最后一頁按鈕顯示的文字。

      5.Mode:獲取或設置支持分頁的控件中的頁導航控件的顯示模式。

      6.NextPageImageUrl:獲取或設置為下一頁按鈕顯示的圖像的URL

      7.NextPageText:獲取或設置為下一頁按鈕顯示的文字。

      8.PageButtonCount:獲取或設置在Mode屬性設置為NumericNumericFirstLast值時頁面導航中顯示的頁按鈕的數量。

      9.Position:獲取或設置一個值,該值指示頁導航的顯示位置。

      10.PreviousPageImageUrl:獲取或設置為上一頁按鈕顯示的圖像的URL

      11.PreviousPageText:獲取或設置為上一頁按鈕顯示的文字。

      12.Visible:獲取或設置一個值,該值指示是否在支持分頁的控件中顯示分頁控件。

    通過設置PagerSettings類的Mode屬性可以指定GridView控件的分頁模式Mode屬性的值包括:

      1.NextPrevious:上一頁按鈕和下一頁按鈕模式。

      2.NextPreviousFirstLast:上一頁按鈕、下一頁按鈕、第一頁按鈕和最后一頁按鈕模式。

      3.Numeric:可以直接訪問頁面的帶編號的鏈接按鈕模式。

      4.NumericFirstLast:帶編號的鏈接按鈕、第一頁鏈接按鈕和最后一頁鏈接按鈕模式。

    GridView控件的PageSettings屬性的設置可以在GridView控件的屬性窗口中進行。

    在設置GridView控件支持分頁功能時,還需要設置屬性PageSize的值,來指示在每一頁中最多顯示的數據條。

    實現分頁功能:

      在Web.config文件中添加如下代碼:

1   <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

      在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" 3              ProviderName="System.Data.SqlClient" DataSourceMode="DataSet" SelectCommand="select * from [Student]"> 4         </asp:SqlDataSource> 5         <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" DataSourceID="SqlDataSource1"  6             AutoGenerateColumns="False" AllowSorting="true" AllowPaging="True" PageSize="5"> 7             <AlternatingRowStyle BackColor="White" /> 8             <PagerSettings Mode="Numeric" /> 9             <Columns>10                 <asp:ButtonField DataTextField="stuNo" Text="按鈕" HeaderText="學號" SortExpression="stuNo" />11                 <asp:ButtonField DataTextField="Class" Text="按鈕" HeaderText="班級" SortExpression="Class" />12                 <asp:ButtonField DataTextField="stuName" Text="按鈕" HeaderText="姓名" SortExpression="stuName" />13                 <asp:ButtonField DataTextField="Age" Text="按鈕" HeaderText="年齡" SortExpression="Age" />14             </Columns>15             <EditRowStyle BackColor="#2461BF" />16             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />17             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />18             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />19             <RowStyle BackColor="#EFF3FB" />20             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />21             <SortedAscendingCellStyle BackColor="#F5F7FB" />22             <SortedAscendingHeaderStyle BackColor="#6D95E1" />23             <SortedDescendingCellStyle BackColor="#E9EBEF" />24             <SortedDescendingHeaderStyle BackColor="#4870BE" />25         </asp:GridView>26     </div>
View Code

  GridView控件的模板列

    GridView控件的列的類型中有一個名為TemplateField的列,該列為模板列,可以利用它根據指定的模版為GridView控件中每一項顯示用戶定義的內容。

    可以使用TemplateField對象制定包含標記和控件的模版,TemplateField對相對應TemplateField類,該類提供了不同部分定義的自定義模版,使用不同的自定義模版可以定義不同的部分,如下:

      1.AlternatingItemTemplate:為TemplateField對象中的交替項指定要顯示的內容。

      2.EditItemTemplate:為TemplateField對象中處于編輯模式中的項指定要顯示的內容。

      3.FooterTemplate:為TemplateField對象的腳注部分指定要顯示的內容。

      4.HeaderTemplate:為TemplateField對象的標頭部分指定要顯示的內容。

      5.InsertItemTemplate:為TemplateField對象中處于插入模式中的項指定要顯示的內容。只有DetailsView控件支持該模版。

      6.ItemTemplate:為Template對象中的項指定要顯示的內容。

    創建模版

      在GridView控件的定義標記中使用標記<asp:TemplateField>即可定義模版。

      模版其實就是一個容器,在里面可以放置各種控件、標記以及文字等以自定義列的顯示樣式和布局。

      模板列的創建也可以通過VS提供的可視化界面進行操作,在.aspx頁面的設計視圖中選中要編輯模板列的GridView控件,單擊右上角的小按鈕,在彈出的菜單中選擇編輯列命令,在彈出的字段對話框中選中TemplateField,單擊添加按鈕即可完成一個模板列的創建,然后可以利用該模板列進行具體格式的定義。

  GridView控件行的選取

    GridView控件允許用戶在表格中選取一行,可能在GridView控件中選取一行并不執行任何功能,但通過添加選定內容功能,可以向網格添加一些功能,在用戶選取某行時進行一些操作。由于在行被選取的過程中和被選取后分別將引發事件SelectedIndexChangingSelectedIndexChanged,因此可以在這兩個事件的處理程序中加入一些自定義代碼以實現自定義的功能。

    GridView控件的行的選取功能是通過添加選擇列來作為選取行的觸發器,選擇列的生成有兩種方式:

      1.設置GridView控件的屬性AutoGenerateSelectButtontrue,在網格中會自動生成一個選擇列。

      2.在字段對話框中找到CommandField,選中選擇類型,單擊添加按鈕,會在網格中添加一個選擇列。

    實現行的選取功能:

      在Web.config文件中添加如下代碼:

1  <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

      在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" 3              ProviderName="System.Data.SqlClient" DataSourceMode="DataSet" SelectCommand="select * from [Student]"> 4         </asp:SqlDataSource> 5         <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" DataSourceID="SqlDataSource1"  6             AutoGenerateColumns="False" AutoGenerateSelectButton="true" AllowSorting="true" AllowPaging="True" PageSize="5" 7              OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> 8             <Columns> 9                 <asp:BoundField DataField="stuNo" HeaderText="學號" />10                 <asp:BoundField DataField="Class" HeaderText="班級" />11                 <asp:BoundField DataField="stuName" HeaderText="姓名" />12                 <asp:BoundField DataField="Age" HeaderText="年齡" />13             </Columns>14             <EditRowStyle BackColor="#2461BF" />15             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />16             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />17             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />18             <RowStyle BackColor="#EFF3FB" />19             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />20             <SortedAscendingCellStyle BackColor="#F5F7FB" />21             <SortedAscendingHeaderStyle BackColor="#6D95E1" />22             <SortedDescendingCellStyle BackColor="#E9EBEF" />23             <SortedDescendingHeaderStyle BackColor="#4870BE" />24         </asp:GridView>25     </div>
View Code

      Default.aspx.cs中的代碼如下:

 1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Configuration; 8 using System.Web.UI; 9 using System.Web.UI.WebControls;10 11 namespace WebApplication112 {13     public partial class Default : System.Web.UI.Page14     {15         protected void Page_Load(object sender, EventArgs e)16         {17         }18 19         protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)20         {21             string ID = this.GridView1.SelectedRow.Cells[1].Text.ToString();//獲取選取行的索引22             Response.Redirect("Form1.aspx?stuNo=" + ID);23         }24     }25 }
View Code

      

      在From1.aspx中添加如下代碼:

 1     <div> 2         <asp:Label ID="Label1" runat="server" Text="選取的學生信息:" /> 3         <br /> 4         <asp:Label ID="Label2" runat="server" /> 5         <br /> 6         <asp:Label ID="Label3" runat="server" /> 7         <br /> 8         <asp:Label ID="Label4" runat="server" /> 9         <br />10         <asp:Label ID="Label5" runat="server" />11     </div>
View Code

      Form1.aspx.cs中的代碼如下:

 1 using System; 2 using System.Collections.Generic; 3 using System.Data; 4 using System.Data.SqlClient; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Configuration; 8 using System.Web.UI; 9 using System.Web.UI.WebControls;10 11 namespace WebApplication112 {13     public partial class Form1 : System.Web.UI.Page14     {15         string connectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;16         protected void Page_Load(object sender, EventArgs e)17         {18             SqlConnection connection = new SqlConnection(connectionString);19             try20             {21                 string stuNo = Request.QueryString["stuNo"].ToString();22                 string selectSql = "select * from Student where stuNo=" + stuNo;23                 SqlCommand command = new SqlCommand(selectSql, connection);24                 SqlDataAdapter dataAdapter = new SqlDataAdapter(command);25                 DataSet dataSet = new DataSet();26                 connection.Open();27                 dataAdapter.Fill(dataSet, "Student");28                 this.Label2.Text = "學號:" + dataSet.Tables["Student"].Rows[0]["stuNo"].ToString();29                 this.Label3.Text = "班級:" + dataSet.Tables["Student"].Rows[0]["Class"].ToString();30                 this.Label4.Text = "姓名:" + dataSet.Tables["Student"].Rows[0]["stuName"].ToString();31                 this.Label5.Text = "年齡:" + dataSet.Tables["Student"].Rows[0]["Age"].ToString();32             }33             catch (Exception ex)34             {35                 Response.Write("讀取錯誤:" + ex.Message.ToString());36             }37             finally38             {39                 connection.Close();40             }41         }42     }43 }
View Code

  GridView控件的數據操作

    當GridView控件把數據顯示到頁面時,可能需要對數據進行修改、刪除的操作。GridView控件通過內置的屬性來提供這些操作界面,而實際的數據操作則通過數據源控件或ADO.NET來實現。啟動GridView控件的刪除修改功能的方式如下:

      1.將AutoGenerateEditButton屬性設置為true,可以啟動修改功能;將AutoGenerateDeleteButton屬性設置為true,可以啟動刪除功能。

      2.添加一個CommandField列,將其ShowEditButton屬性設置為true來啟動修改功能,將其屬性ShowDeleteButton屬性設置為true來啟動刪除功能。

      3.創建一個TemplateField,其中ItemTemplate包含多個命令按鈕,要進行更新時將CommandName設置為Edit,要進行刪除時將CommandName設置為Delete

    當啟用GridView控件的刪除或修改功能時,GridView控件會顯示一個能夠讓用戶編輯或刪除各行的用戶界面:一般情況下,會在一列或多列中顯示按鈕或鏈接,              用戶通過單擊按鈕或鏈接把所在的行置于可編輯的模式下或直接把行刪除。

    在處理更改和刪除GridView控件數據的實際操作時,有如下兩種選擇:

      1. 使用數據源控件。用戶保存更改時,GridView控件將更改和主鍵信息傳遞到由DataSourceID屬性標識的數據源控件,從而調用適當的更新操作。

      2. 在事件處理程序中使用ADO.NET方法編寫自動的更新或刪除代碼。用戶保存更改時,將觸發事件RowUpdated,在該事件處理程序中獲得更改后數據,然后使用ADO.NET方法調用 Update語句把數據更新。用戶刪除行時,將觸發事件RowDeleted,在事件處理程序中獲得要刪除行的數據的主鍵,然后使用ADO.NET方法調用Delete語句把數據更新。  

    在事件處理程序中是根據三個字典集來獲得GridView控件的傳遞數據的,三個字典集合分別是:KeysNewValuesOldValuesKeys字典包含字段的名稱和值,通過它們唯一的標識將要更新或刪除的記錄,并始終包含鍵字段的原始值。NewValues字典包含正在編輯的行中的輸入控件的當前值。OldValues字典包含除鍵字段以外的任何字段的原始值,鍵字段放在Keys字典中。

    使用自動生成的刪除列和更新列:

      在Web.config文件中添加如下代碼:

1   <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

      在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" 3              ProviderName="System.Data.SqlClient" DataSourceMode="DataReader" SelectCommand="select * from Student" 4              UpdateCommand="update Student set stuNo=@stuNo,Class=@Class,stuName=@stuName,Age=@Age where stuNo=@stuNo" 5              DeleteCommand="delete from Student Where stuNo=@stuNo"> 6             <UpdateParameters> 7                 <asp:Parameter Name="Class" Type="String" /> 8                 <asp:Parameter Name="stuName" Type="String" /> 9                 <asp:Parameter Name="Age" Type="String" />10                 <asp:Parameter Name="stuNo" Type="String" DefaultValue="007" />11             </UpdateParameters>12             <DeleteParameters>13                 <asp:Parameter Name="stuNo" Type="String" DefaultValue="007" />14             </DeleteParameters>15         </asp:SqlDataSource>16         <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="stuNo"17              OnRowDeleted="GridView1_RowDeleted" OnRowUpdated="GridView1_RowUpdated"18              AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" ForeColor="#333333" GridLines="None">19             <AlternatingRowStyle BackColor="White" />20             <Columns>21                 <asp:BoundField DataField="stuNo" HeaderText="學號" ReadOnly="True" />22                 <asp:BoundField DataField="Class" HeaderText="班級" />23                 <asp:BoundField DataField="stuName" HeaderText="姓名" />24                 <asp:BoundField DataField="Age" HeaderText="年齡" />25             </Columns>26             <EditRowStyle BackColor="#7C6F57" />27             <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />28             <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />29             <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />30             <RowStyle BackColor="#E3EAEB" />31             <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />32             <SortedAscendingCellStyle BackColor="#F8FAFA" />33             <SortedAscendingHeaderStyle BackColor="#246B61" />34             <SortedDescendingCellStyle BackColor="#D4DFE1" />35             <SortedDescendingHeaderStyle BackColor="#15524A" />36         </asp:GridView>37     </div>
View Code

      Default.aspx.cs中的代碼如下:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 namespace WebApplication2 9 {10     public partial class Default : System.Web.UI.Page11     {12         protected void Page_Load(object sender, EventArgs e)13         {14 15         }16 17         protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)18         {19             this.GridView1.DataBind();20         }21 22         protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)23         {24             this.GridView1.DataBind();25         }26     }27 }
View Code

    使用CommandField刪除列和更新列:

      在Web.config文件中添加如下代碼:

1   <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

      在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" 3              ProviderName="System.Data.SqlClient" DataSourceMode="DataReader" SelectCommand="select * from Student" 4              UpdateCommand="update Student set stuNo=@stuNo,Class=@Class,stuName=@stuName,Age=@Age where stuNo=@stuNo" 5              DeleteCommand="delete from Student Where stuNo=@stuNo"> 6             <UpdateParameters> 7                 <asp:Parameter Name="Class" Type="String" /> 8                 <asp:Parameter Name="stuName" Type="String" /> 9                 <asp:Parameter Name="Age" Type="String" />10                 <asp:Parameter Name="stuNo" Type="String" DefaultValue="007" />11             </UpdateParameters>12             <DeleteParameters>13                 <asp:Parameter Name="stuNo" Type="String" DefaultValue="007" />14             </DeleteParameters>15         </asp:SqlDataSource>16         <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="stuNo"17              OnRowDeleted="GridView1_RowDeleted" OnRowUpdated="GridView1_RowUpdated"18               CellPadding="4" ForeColor="#333333" GridLines="None">19             <AlternatingRowStyle BackColor="White" />20             <Columns>21                 <asp:BoundField DataField="stuNo" HeaderText="學號" ReadOnly="True" />22                 <asp:BoundField DataField="Class" HeaderText="班級" />23                 <asp:BoundField DataField="stuName" HeaderText="姓名" />24                 <asp:BoundField DataField="Age" HeaderText="年齡" />25                 <asp:CommandField ShowDeleteButton="true" ShowEditButton="true" />26             </Columns>27             <EditRowStyle BackColor="#7C6F57" />28             <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />29             <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />30             <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />31             <RowStyle BackColor="#E3EAEB" />32             <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />33             <SortedAscendingCellStyle BackColor="#F8FAFA" />34             <SortedAscendingHeaderStyle BackColor="#246B61" />35             <SortedDescendingCellStyle BackColor="#D4DFE1" />36             <SortedDescendingHeaderStyle BackColor="#15524A" />37         </asp:GridView>38     </div>
View Code

      Default.aspx.cs中的代碼如下:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 namespace WebApplication2 9 {10     public partial class Default : System.Web.UI.Page11     {12         protected void Page_Load(object sender, EventArgs e)13         {14 15         }16 17         protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)18         {19             this.GridView1.DataBind();20         }21 22         protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)23         {24             this.GridView1.DataBind();25         }26     }27 }
View Code

DetailsView控件

  DetailsView控件主要用來從與它聯系的數據源中一次顯示、編輯、插入或刪除一條記錄。通常,它將與GridView控件一起使用在主/詳細方案中,GridView控件用來顯示主要的數據目錄,而DetailsView控件顯示每條數據的詳細信息。

  DetailsView控件本身自帶了編輯數據的功能,只要把屬性AutoGenerateDeleteButton、 AutoGenerateInsertButtonAutoGenerateEditButton設置為“true”就可以啟用DetailsView控件的編輯數據的功能,當然實際的數據操作過程還是在數據源控件中進行。

  與GridView控件相比,DetailsView控件中支持進行數據的插入操作。

  使用DetailsView控件顯示數據:

    在Web.config文件中添加如下代碼:

1   <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

    在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" 3              ProviderName="System.Data.SqlClient" DataSourceMode="DataSet" SelectCommand="select * from Student"> 4         </asp:SqlDataSource> 5         <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#DEDFDE" 6              BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" AllowPaging="true"> 7             <AlternatingRowStyle BackColor="White" /> 8             <EditRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" /> 9             <FooterStyle BackColor="#CCCC99" />10             <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />11             <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />12             <RowStyle BackColor="#F7F7DE" />13         </asp:DetailsView>14     </div>
View Code

  

  在DetailsView控件中操作數據:

    在Web.config文件中添加如下代碼:

1   <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

    在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" 3              ProviderName="System.Data.SqlClient" DataSourceMode="DataSet" SelectCommand="select * from Student" 4              InsertCommand="insert into Student(stuNo,Class,stuName,Age)  5             values(@stuNo,@Class,@stuName,@Age);select @stuNo=SCOPE_IDENTITY()" 6              UpdateCommand="update Student set stuNo=@stuNo,Class=@Class,stuName=@stuName,Age=@Age" 7              DeleteCommand="delete Student where stuNo=@stuNo"> 8             <InsertParameters> 9                 <asp:Parameter Name="Class" Type="String" />10                 <asp:Parameter Name="stuName" Type="String" />11                 <asp:Parameter Name="Age" Type="Int32" />12                 <asp:Parameter Name="stuNo" Type="String" DefaultValue="009" />13             </InsertParameters>14             <UpdateParameters>15                 <asp:Parameter Name="Class" Type="String" />16                 <asp:Parameter Name="stuName" Type="String" />17                 <asp:Parameter Name="Age" Type="Int32" />18                 <asp:Parameter Name="stuNo" Type="String" DefaultValue="009" />19             </UpdateParameters>20             <DeleteParameters>21                 <asp:Parameter Name="stuNo" Type="String" DefaultValue="009" />22             </DeleteParameters>23         </asp:SqlDataSource>24         <asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#DEDFDE"25              BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" AllowPaging="True"26              AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" AutoGenerateInsertButton="True" DataKeyNames="stuNo">27             <AlternatingRowStyle BackColor="White" />28             <EditRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />29             <FooterStyle BackColor="#CCCC99" />30             <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />31             <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />32             <RowStyle BackColor="#F7F7DE" />33         </asp:DetailsView>34     </div>
View Code

ListView控件 

  ListView控件可以顯示使用數據源控件或ADO.NET獲得的數據 。

  使用模板和樣式定義的格式顯示數據。利用ListView控件,可以逐項顯示數據,也可以按組顯示數據。

  ListView 控件的特點

    1.支持綁定到數據源控件,例如SqlDataSourceObjectDataSource

    2.可通過用戶定義的模板和樣式自定義外觀。

    3.內置排序和選擇功能。

    4.內置更新、插入和刪除功能。

    5.支持通過使用DataPager控件進行分頁的功能。

    6.支持以編程方式訪問ListView對象模型,從而可以動態設置屬性、處理事件。

    7.支持多個鍵字段。 

  支持的模板

    為了使ListView控件顯示內容,必須為控件的不同部分創建模板。用戶需要根據不同的需求創建相應的模板。

      1.通過創建LayoutTemplate模板,可以定義ListView控件的主要(根)布局。該模板必須包含一個充當數據占位符的控件。這些控件將包含ItemTemplate模板  所定義的每個項的輸出。

      2.ItemTemplate模板中,需要定義各個項的內容。此模板包含的控件通常已綁定到數據列或其他單個數據元素。

      3.使用GroupTemplate模板,可以選擇對ListView控件中的項進行分組。對項分組通常是為了創建平鋪的表布局。

      4.使用EditItemTemplate模板,可以提供已綁定數據的用戶界面,從而使用戶可以修改現有的數據項。

      5.使用InsertItemTemplate模板還可以定義已綁定數據的用戶界面,以使用戶能夠添加新的數據項。

    通常需要向模板中添加一些按鈕,以允許用戶指定要執行的操作。通過設置按鈕的 CommandName 屬性,可以定義按鈕將執行的操作。

      1. Select:顯示所選項的SelectedItemTemplate模板的內容。

      2. Insert:在InsertItemTemplate模板中,將數據綁定控件的內容   保存在數據源中。

      3. Edit:把ListView控件切換到編輯模式,并使用EditItemTemplate模板顯示項。

      4. Update:在EditItemTemplate 模板中,指定應將數據綁定控件的內容保存在數據源中。

      5. Delete:從數據源中刪除項。

      6. Cancel:取消當前操作。 

  ListView控件模版的應用:  

    在Web.config文件中添加如下代碼:  

1   <connectionStrings>2     <add name="ConnectionString" connectionString="Data Source=追風的蝸牛;Initial Catalog=Student;Integrated Security=True"/>3   </connectionStrings>
View Code

    在Default.aspx中添加如下代碼:

 1     <div> 2         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" 3              ProviderName="System.Data.SqlClient" DataSourceMode="DataReader" SelectCommand="select * from Student"> 4         </asp:SqlDataSource> 5         <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"> 6             <LayoutTemplate> 7                 <table runat="server" id="Table1"> 8                     <tr runat="server" id="itemPlaceHolder" /> 9                 </table>10             </LayoutTemplate>11             <ItemTemplate>12                 <tr runat="server">13                     <td id="td1" runat="server">14                         <asp:Label ID="Label1" runat="server" Text='<%#Eval("stuName") %>' />15                     </td>16                 </tr>17             </ItemTemplate>18         </asp:ListView>19     </div>
View Code

Chart控件 

  Chart控件是Visual Studio 2010中新增的一個圖表型控件。 

  Chart控件功能非常強大,可實現柱狀直方圖、曲線走勢圖、餅狀比例圖等,甚至可以是混合圖表,可以是二維或三維圖表,可以帶或不帶坐標系,可以自由配置各條目的顏色、字體等等。 

  Chart控件的主要組成部分:

    1.Annotation:圖形注解集合,它是對圖形的以下注解對象的集合,注解對象類似于對某個點的詳細或批注的說明。

    2.CharAreas:圖表區域集合,圖表的繪制區。Chart控件不限制CharAreas繪圖區域的添加個數,對于每一個CharAreas繪圖區域可以設置各自的屬性。繪圖區域只是一個可以繪圖的區域范圍,本身并不包括各種屬性數據。

    3.Legends:圖例集合,標注圖形中各個線條或顏色的含義,同樣,一個圖片也可以包含多個圖例說明,分別說明各個繪圖區域的信息。

    4.Series:圖標序列集合,圖標序列就是實際的繪圖繪圖數據區域,實際呈現的圖形形狀就是由此集合中的每個圖表來構成的,可以往集合里添加多個圖表,每個圖表都可以有自己的繪制形狀、樣式、獨立的數據等。每個圖表可以指定它的繪圖區域,讓圖標呈現在某個繪圖區域,也可以讓幾個圖表在同一個繪圖區域疊加。

    5.Titles:圖表標題集合,用于圖表標題的設置,可以添加多個標題,設置標題的屬性。

  對于簡單的圖表,用默認的樣式就可以了,不用對ChartArea進行太多的修改,只要在<asp:Series>中添加數據點。數據點被包含在<Points></Points >標簽中,使用<asp:DataPoint/>來定義。數據點有以下幾個重要的屬性:

    1. AxisLabel:獲取或設置為數據列或空點的X軸標簽文本。此屬性僅在自定義標簽尚未就有關Axis對象指定時使用。

    2. XValue:設置或獲取一個圖表上數據點的X坐標值。

    3. YValues:設置或獲取一個圖表中數據點的Y坐標值。 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: caoporn国产一区二区 | 中日韩乱码一二新区 | 龙的两根好大拔不出去h | 久久99国产精品久久99果冻传媒 | 国产艳妇av视国产精选av一区 | 水多视频在线观看 | 精品一区二区三区中文字幕老牛 | 主播粉嫩国产在线精品 | 中文字幕免费在线看 | 欧美日韩一区,二区,三区,久久精品 | 中文区永久区 | 久久国产精品一区 | 91九色蝌蚪国产 | 最新av在线免费观看 | 杏美月av| 中文在线免费观看 | 亚洲国产视频网 | 色综合欧美 | 色综合激情 | 久久伊人国产精品 | 爱爱视频天天干 | 一级毛片看 | 久久久久久久黄色片 | 91久久国产综合精品女同国语 | 成人午夜在线观看视频 | av免费在线免费观看 | 免费毛片在线 | 午夜精品久久久久久中宇 | 视频一区二区三区在线播放 | 深夜福利视频免费观看 | 9999视频| 毛片视频网站在线观看 | 在线看免电影网站 | 91精品国产日韩91久久久久久360 | 国产一国产精品一级毛片 | 天天操天天骑 | 色中色在线播放 | 国产寡妇xxxxxxxx性开放 | 一区二区免费 | 国产三级精品最新在线 | 国产乱色精品成人免费视频 |