数据服务器控件就是能够显示数据的控件,与那些简单格式的列表控件不同,这些控件不但提供显示数据的丰富界面(可以显示多行多列数据并根据用户定义来显示),还提供了修改、删除和插入数据的接口。
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<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:GridViewID="GridView1"runat="server"CellPadding="4"ForeColor="#333333"GridLines="None">3<AlternatingRowStyleBackColor="White"/>4<EditRowStyleBackColor="#2461BF"/>5<FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>6<HeaderStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>7<PagerStyleBackColor="#2461BF"ForeColor="White"HorizontalAlign="Center"/>8<RowStyleBackColor="#EFF3FB"/>9<SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/>10<SortedAscendingCellStyleBackColor="#F5F7FB"/>11<SortedAscendingHeaderStyleBackColor="#6D95E1"/>12<SortedDescendingCellStyleBackColor="#E9EBEF"/>13<SortedDescendingHeaderStyleBackColor="#4870BE"/>14</asp:GridView>15</div>
View Code
Default.aspx.cs文件中的代码如下:
1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Data;4usingSystem.Data.SqlClient;5usingSystem.Linq;6usingSystem.Web;7usingSystem.Web.Configuration;8usingSystem.Web.UI;9usingSystem.Web.UI.WebControls;1011namespaceWebapplication112{13publicpartialclassDefault : System.Web.UI.Page14{15PRivatestringconnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;16protectedvoidPage_Load(objectsender, EventArgs e)17{18if(!this.IsPostBack)19{20Binding();21}22}23privatevoidBinding()24{25stringselectSql ="select * from Student";//获取学生信息26SqlConnection connection =newSqlConnection(connectionString);27SqlCommand command =newSqlCommand(selectSql, connection);28SqlDataAdapter dataAdapter =newSqlDataAdapter(command);29DataSet dataSet =newDataSet();30//打开数据库并读取信息31try32{33connection.Open();34dataAdapter.Fill(dataSet,"Student");35//把存储在DataSet中的数据绑定到GridView控件中36this.GridView1.DataSource = dataSet.Tables["Student"].DefaultView;37this.GridView1.DataBind();38}39catch(Exception ex)40{41Response.Write("读取过程发生错误:"+ex.Message.ToString());42}43finally44{45connection.Close();46}47}48}49}
View Code
2.使用数据源控件。使用数据源控件绑定GridView控件。
在Web.config文件中添加如下代码:
1<connectionStrings>2<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:ConnectionString %>"3ProviderName="System.Data.SqlClient"DataSourceMode="DataReader"SelectCommand="select * from [Student]">4</asp:SqlDataSource>5<asp:GridViewID="GridView1"runat="server"CellPadding="4"ForeColor="#333333"GridLines="None"DataSourceID="SqlDataSource1">6<AlternatingRowStyleBackColor="White"/>7<EditRowStyleBackColor="#2461BF"/>8<FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>9<HeaderStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>10<PagerStyleBackColor="#2461BF"ForeColor="White"HorizontalAlign="Center"/>11<RowStyleBackColor="#EFF3FB"/>12<SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/>13<SortedAscendingCellStyleBackColor="#F5F7FB"/>14<SortedAscendingHeaderStyleBackColor="#6D95E1"/>15<SortedDescendingCellStyleBackColor="#E9EBEF"/>16<SortedDescendingHeaderStyleBackColor="#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<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:ConnectionString %>"3ProviderName="System.Data.SqlClient"DataSourceMode="DataSet"SelectCommand="select * from [Student]">4</asp:SqlDataSource>5<asp:GridViewID="GridView1"runat="server"CellPadding="4"ForeColor="#333333"GridLines="None"DataSourceID="SqlDataSource1"6AutoGenerateColumns="False"AllowSorting="true">7<AlternatingRowStyleBackColor="White"/>8<Columns>9<asp:ButtonFieldDataTextField="stuNo"Text="按钮"HeaderText="学号"SortExpression="stuNo"/>10<asp:ButtonFieldDataTextField="Class"Text="按钮"HeaderText="班级"SortExpression="Class"/>11<asp:ButtonFieldDataTextField="stuName"Text="按钮"HeaderText="姓名"SortExpression="stuName"/>12<asp:ButtonFieldDataTextField="Age"Text="按钮"HeaderText="年龄"SortExpression="Age"/>13</Columns>14<EditRowStyleBackColor="#2461BF"/>15<FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>16<HeaderStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>17<PagerStyleBackColor="#2461BF"ForeColor="White"HorizontalAlign="Center"/>18<RowStyleBackColor="#EFF3FB"/>19<SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/>20<SortedAscendingCellStyleBackColor="#F5F7FB"/>21<SortedAscendingHeaderStyleBackColor="#6D95E1"/>22<SortedDescendingCellStyleBackColor="#E9EBEF"/>23<SortedDescendingHeaderStyleBackColor="#4870BE"/>24</asp:GridView>25</div>
View Code
其实GridView控件自己并不执行排序,它是通过数据源控件实现排序的。GridView控件只不过提供一个显示和操作的数据界面而已,而真实的数据排序是它所绑定到的数据源控件。如果所绑定的数据源控件可以排列数据,则选择数据后,GridView控件可以通过列的属性SortExpression传递给数据源与该数据源控件进行交互并请求排序后的数据。
并不是所有的数据源控件都支持排序的,支持排序的数据源控件所需要的配置如下:
1.SqlDataSource控件的属性DataSourceMode设置为DataSet,或属性SortParameterName设置为DataSet或DataReader。
2.accessDataSource控件的属性DataSourceMode设置为DataSet,或属性SortParameterName设置为DataSet或DataReader。
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属性设置为Numeric或NumericFirstLast值时页面导航中显示的页按钮的数量。
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<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:ConnectionString %>"3ProviderName="System.Data.SqlClient"DataSourceMode="DataSet"SelectCommand="select * from [Student]">4</asp:SqlDataSource>5<asp:GridViewID="GridView1"runat="server"CellPadding="4"ForeColor="#333333"GridLines="None"DataSourceID="SqlDataSource1"6AutoGenerateColumns="False"AllowSorting="true"AllowPaging="True"PageSize="5">7<AlternatingRowStyleBackColor="White"/>8<PagerSettingsMode="Numeric"/>9<Columns>10<asp:ButtonFieldDataTextField="stuNo"Text="按钮"HeaderText="学号"SortExpression="stuNo"/>11<asp:ButtonFieldDataTextField="Class"Text="按钮"HeaderText="班级"SortExpression="Class"/>12<asp:ButtonFieldDataTextField="stuName"Text="按钮"HeaderText="姓名"SortExpression="stuName"/>13<asp:ButtonFieldDataTextField="Age"Text="按钮"HeaderText="年龄"SortExpression="Age"/>14</Columns>15<EditRowStyleBackColor="#2461BF"/>16<FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>17<HeaderStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>18<PagerStyleBackColor="#2461BF"ForeColor="White"HorizontalAlign="Center"/>19<RowStyleBackColor="#EFF3FB"/>20<SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/>21<SortedAscendingCellStyleBackColor="#F5F7FB"/>22<SortedAscendingHeaderStyleBackColor="#6D95E1"/>23<SortedDescendingCellStyleBackColor="#E9EBEF"/>24<SortedDescendingHeaderStyleBackColor="#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控件中选取一行并不执行任何功能,但通过添加选定内容功能,可以向网格添加一些功能,在用户选取某行时进行一些操作。由于在行被选取的过程中和被选取后分别将引发事件SelectedIndexChanging和SelectedIndexChanged,因此可以在这两个事件的处理程序中加入一些自定义代码以实现自定义的功能。
GridView控件的行的选取功能是通过添加选择列来作为选取行的触发器,选择列的生成有两种方式:
1.设置GridView控件的属性AutoGenerateSelectButton为true,在网格中会自动生成一个选择列。
2.在字段对话框中找到CommandField,选中选择类型,单击添加按钮,会在网格中添加一个选择列。
实现行的选取功能:
在Web.config文件中添加如下代码:
1<connectionStrings>2<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:ConnectionString %>"3ProviderName="System.Data.SqlClient"DataSourceMode="DataSet"SelectCommand="select * from [Student]">4</asp:SqlDataSource>5<asp:GridViewID="GridView1"runat="server"CellPadding="4"ForeColor="#333333"GridLines="None"DataSourceID="SqlDataSource1"6AutoGenerateColumns="False"AutoGenerateSelectButton="true"AllowSorting="true"AllowPaging="True"PageSize="5"7OnSelectedIndexChanged="GridView1_SelectedIndexChanged">8<Columns>9<asp:BoundFieldDataField="stuNo"HeaderText="学号"/>10<asp:BoundFieldDataField="Class"HeaderText="班级"/>11<asp:BoundFieldDataField="stuName"HeaderText="姓名"/>12<asp:BoundFieldDataField="Age"HeaderText="年龄"/>13</Columns>14<EditRowStyleBackColor="#2461BF"/>15<FooterStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>16<HeaderStyleBackColor="#507CD1"Font-Bold="True"ForeColor="White"/>17<PagerStyleBackColor="#2461BF"ForeColor="White"HorizontalAlign="Center"/>18<RowStyleBackColor="#EFF3FB"/>19<SelectedRowStyleBackColor="#D1DDF1"Font-Bold="True"ForeColor="#333333"/>20<SortedAscendingCellStyleBackColor="#F5F7FB"/>21<SortedAscendingHeaderStyleBackColor="#6D95E1"/>22<SortedDescendingCellStyleBackColor="#E9EBEF"/>23<SortedDescendingHeaderStyleBackColor="#4870BE"/>24</asp:GridView>25</div>
View Code
Default.aspx.cs中的代码如下:
1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Data;4usingSystem.Data.SqlClient;5usingSystem.Linq;6usingSystem.Web;7usingSystem.Web.Configuration;8usingSystem.Web.UI;9usingSystem.Web.UI.WebControls;1011namespaceWebApplication112{13publicpartialclassDefault : System.Web.UI.Page14{15protectedvoidPage_Load(objectsender, EventArgs e)16{17}1819protectedvoidGridView1_SelectedIndexChanged(objectsender, EventArgs e)20{21stringID =this.GridView1.SelectedRow.Cells[1].Text.ToString();//获取选取行的索引22Response.Redirect("Form1.aspx?stuNo="+ID);23}24}25}
View Code
在From1.aspx中添加如下代码:
1<div>2<asp:LabelID="Label1"runat="server"Text="选取的学生信息:"/>3<
br/>4<asp:LabelID="Label2"runat="server"/>5<
br/>6<asp:LabelID="Label3"runat="server"/>7<
br/>8<asp:LabelID="Label4"runat="server"/>9<
br/>10<asp:LabelID="Label5"runat="server"/>11</div>
View Code
Form1.aspx.cs中的代码如下:
1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Data;4usingSystem.Data.SqlClient;5usingSystem.Linq;6usingSystem.Web;7usingSystem.Web.Configuration;8usingSystem.Web.UI;9usingSystem.Web.UI.WebControls;1011namespaceWebApplication112{13publicpartialclassForm1 : System.Web.UI.Page14{15stringconnectionString = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;16protectedvoidPage_Load(objectsender, EventArgs e)17{18SqlConnection connection =newSqlConnection(connectionString);19try20{21stringstuNo = Request.QueryString["stuNo"].ToString();22stringselectSql ="select * from Student where stuNo="+stuNo;23SqlCommand command =newSqlCommand(selectSql, connection);24SqlDataAdapter dataAdapter =newSqlDataAdapter(command);25DataSet dataSet =newDataSet();26connection.Open();27dataAdapter.Fill(dataSet,"Student");28this.Label2.Text ="学号:"+ dataSet.Tables["Student"].Rows[0]["stuNo"].ToString();29this.Label3.Text ="班级:"+ dataSet.Tables["Student"].Rows[0]["Class"].ToString();30this.Label4.Text ="姓名:"+ dataSet.Tables["Student"].Rows[0]["stuName"].ToString();31this.Label5.Text ="年龄:"+ dataSet.Tables["Student"].Rows[0]["Age"].ToString();32}33catch(Exception ex)34{35Response.Write("读取错误:"+ex.Message.ToString());36}37finally38{39connection.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控件的传递数据的,三个字典集合分别是:Keys、NewValues、OldValues。Keys字典包含字段的名称和值,通过它们唯一的标识将要更新或删除的记录,并始终包含键字段的原始值。NewValues字典包含正在编辑的行中的输入控件的当前值。OldValues字典包含除键字段以外的任何字段的原始值,键字段放在Keys字典中。
使用自动生成的删除列和更新列:
在Web.config文件中添加如下代码:
1<connectionStrings>2<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:ConnectionString %>"3ProviderName="System.Data.SqlClient"DataSourceMode="DataReader"SelectCommand="select * from Student"4UpdateCommand="update Student set stuNo=@stuNo,Class=@Class,stuName=@stuName,Age=@Age where stuNo=@stuNo"5DeleteCommand="delete from Student Where stuNo=@stuNo">6<UpdateParameters>7<asp:ParameterName="Class"Type="String"/>8<asp:ParameterName="stuName"Type="String"/>9<asp:ParameterName="Age"Type="String"/>10<asp:ParameterName="stuNo"Type="String"DefaultValue="007"/>11</UpdateParameters>12<DeleteParameters>13<asp:ParameterName="stuNo"Type="String"DefaultValue="007"/>14</DeleteParameters>15</asp:SqlDataSource>16<asp:GridViewID="GridView1"runat="server"DataSourceID="SqlDataSource1"AutoGenerateColumns="False"DataKeyNames="stuNo"17OnRowDeleted="GridView1_RowDeleted"OnRowUpdated="GridView1_RowUpdated"18AutoGenerateDeleteButton="True"AutoGenerateEditButton="True"CellPadding="4"ForeColor="#333333"GridLines="None">19<AlternatingRowStyleBackColor="White"/>20<Columns>21<asp:BoundFieldDataField="stuNo"HeaderText="学号"ReadOnly="True"/>22<asp:BoundFieldDataField="Class"HeaderText="班级"/>23<asp:BoundFieldDataField="stuName"HeaderText="姓名"/>24<asp:BoundFieldDataField="Age"HeaderText="年龄"/>25</Columns>26<EditRowStyleBackColor="#7C6F57"/>27<FooterStyleBackColor="#1C5E55"Font-Bold="True"ForeColor="White"/>28<HeaderStyleBackColor="#1C5E55"Font-Bold="True"ForeColor="White"/>29<PagerStyleBackColor="#666666"ForeColor="White"HorizontalAlign="Center"/>30<RowStyleBackColor="#E3EAEB"/>31<SelectedRowStyleBackColor="#C5BBAF"Font-Bold="True"ForeColor="#333333"/>32<SortedAscendingCellStyleBackColor="#F8FAFA"/>33<SortedAscendingHeaderStyleBackColor="#246B61"/>34<SortedDescendingCellStyleBackColor="#D4DFE1"/>35<SortedDescendingHeaderStyleBackColor="#15524A"/>36</asp:GridView>37</div>
View Code
Default.aspx.cs中的代码如下:
1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Web;5usingSystem.Web.UI;6usingSystem.Web.UI.WebControls;78namespaceWebApplication29{10publicpartialclassDefault : System.Web.UI.Page11{12protectedvoidPage_Load(objectsender, EventArgs e)13{1415}1617protectedvoidGridView1_RowDeleted(objectsender, GridViewDeletedEventArgs e)18{19this.GridView1.DataBind();20}2122protectedvoidGridView1_RowUpdated(objectsender, GridViewUpdatedEventArgs e)23{24this.GridView1.DataBind();25}26}27}
View Code
使用CommandField删除列和更新列:
在Web.config文件中添加如下代码:
1<connectionStrings>2<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:ConnectionString %>"3ProviderName="System.Data.SqlClient"DataSourceMode="DataReader"SelectCommand="select * from Student"4UpdateCommand="update Student set stuNo=@stuNo,Class=@Class,stuName=@stuName,Age=@Age where stuNo=@stuNo"5DeleteCommand="delete from Student Where stuNo=@stuNo">6<UpdateParameters>7<asp:ParameterName="Class"Type="String"/>8<asp:ParameterName="stuName"Type="String"/>9<asp:ParameterName="Age"Type="String"/>10<asp:ParameterName="stuNo"Type="String"DefaultValue="007"/>11</UpdateParameters>12<DeleteParameters>13<asp:ParameterName="stuNo"Type="String"DefaultValue="007"/>14</DeleteParameters>15</asp:SqlDataSource>16<asp:GridViewID="GridView1"runat="server"DataSourceID="SqlDataSource1"AutoGenerateColumns="False"DataKeyNames="stuNo"17OnRowDeleted="GridView1_RowDeleted"OnRowUpdated="GridView1_RowUpdated"18CellPadding="4"ForeColor="#333333"GridLines="None">19<AlternatingRowStyleBackColor="White"/>20<Columns>21<asp:BoundFieldDataField="stuNo"HeaderText="学号"ReadOnly="True"/>22<asp:BoundFieldDataField="Class"HeaderText="班级"/>23<asp:BoundFieldDataField="stuName"HeaderText="姓名"/>24<asp:BoundFieldDataField="Age"HeaderText="年龄"/>25<asp:CommandFieldShowDeleteButton="true"ShowEditButton="true"/>26</Columns>27<EditRowStyleBackColor="#7C6F57"/>28<FooterStyleBackColor="#1C5E55"Font-Bold="True"ForeColor="White"/>29<HeaderStyleBackColor="#1C5E55"Font-Bold="True"ForeColor="White"/>30<PagerStyleBackColor="#666666"ForeColor="White"HorizontalAlign="Center"/>31<RowStyleBackColor="#E3EAEB"/>32<SelectedRowStyleBackColor="#C5BBAF"Font-Bold="True"ForeColor="#333333"/>33<SortedAscendingCellStyleBackColor="#F8FAFA"/>34<SortedAscendingHeaderStyleBackColor="#246B61"/>35<SortedDescendingCellStyleBackColor="#D4DFE1"/>36<SortedDescendingHeaderStyleBackColor="#15524A"/>37</asp:GridView>38</div>
View Code
Default.aspx.cs中的代码如下:
1usingSystem;2usingSystem.Collections.Generic;3usingSystem.Linq;4usingSystem.Web;5usingSystem.Web.UI;6usingSystem.Web.UI.WebControls;78namespaceWebApplication29{10publicpartialclassDefault : System.Web.UI.Page11{12protectedvoidPage_Load(objectsender, EventArgs e)13{1415}1617protectedvoidGridView1_RowDeleted(objectsender, GridViewDeletedEventArgs e)18{19this.GridView1.DataBind();20}2122protectedvoidGridView1_RowUpdated(objectsender, GridViewUpdatedEventArgs e)23{24this.GridView1.DataBind();25}26}27}
View Code
DetailsView控件
DetailsView控件主要用来从与它联系的数据源中一次显示、编辑、插入或删除一条记录。通常,它将与GridView控件一起使用在主/详细方案中,GridView控件用来显示主要的数据目录,而DetailsView控件显示每条数据的详细信息。
DetailsView控件本身自带了编辑数据的功能,只要把属性AutoGenerateDeleteButton、 AutoGenerateInsertButton和AutoGenerateEditButton设置为“true”就可以启用DetailsView控件的编辑数据的功能,当然实际的数据操作过程还是在数据源控件中进行。
与GridView控件相比,DetailsView控件中支持进行数据的插入操作。
使用DetailsView控件显示数据:
在Web.config文件中添加如下代码:
1<connectionStrings>2<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:ConnectionString %>"3ProviderName="System.Data.SqlClient"DataSourceMode="DataSet"SelectCommand="select * from Student">4</asp:SqlDataSource>5<asp:DetailsViewID="DetailsView1"runat="server"DataSourceID="SqlDataSource1"BackColor="White"BorderColor="#DEDFDE"6BorderStyle="None"BorderWidth="1px"CellPadding="4"ForeColor="Black"GridLines="Vertical"AllowPaging="true">7<AlternatingRowStyleBackColor="White"/>8<EditRowStyleBackColor="#CE5D5A"Font-Bold="True"ForeColor="White"/>9<FooterStyleBackColor="#CCCC99"/>10<HeaderStyleBackColor="#6B696B"Font-Bold="True"ForeColor="White"/>11<PagerStyleBackColor="#F7F7DE"ForeColor="Black"HorizontalAlign="Right"/>12<RowStyleBackColor="#F7F7DE"/>13</asp:DetailsView>14</div>
View Code
在DetailsView控件中操作数据:
在Web.config文件中添加如下代码:
1<connectionStrings>2<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:ConnectionString %>"3ProviderName="System.Data.SqlClient"DataSourceMode="DataSet"SelectCommand="select * from Student"4InsertCommand="insert into Student(stuNo,Class,stuName,Age)5values(@stuNo,@Class,@stuName,@Age);select @stuNo=SCOPE_IDENTITY()"6UpdateCommand="update Student set stuNo=@stuNo,Class=@Class,stuName=@stuName,Age=@Age"7DeleteCommand="delete Student where stuNo=@stuNo">8<InsertParameters>9<asp:ParameterName="Class"Type="String"/>10<asp:ParameterName="stuName"Type="String"/>11<asp:ParameterName="Age"Type="Int32"/>12<asp:ParameterName="stuNo"Type="String"DefaultValue="009"/>13</InsertParameters>14<UpdateParameters>15<asp:ParameterName="Class"Type="String"/>16<asp:ParameterName="stuName"Type="String"/>17<asp:ParameterName="Age"Type="Int32"/>18<asp:ParameterName="stuNo"Type="String"DefaultValue="009"/>19</UpdateParameters>20<DeleteParameters>21<asp:ParameterName="stuNo"Type="String"DefaultValue="009"/>22</DeleteParameters>23</asp:SqlDataSource>24<asp:DetailsViewID="DetailsView1"runat="server"DataSourceID="SqlDataSource1"BackColor="White"BorderColor="#DEDFDE"25BorderStyle="None"BorderWidth="1px"CellPadding="4"ForeColor="Black"GridLines="Vertical"AllowPaging="True"26AutoGenerateDeleteButton="True"AutoGenerateEditButton="True"AutoGenerateInsertButton="True"DataKeyNames="stuNo">27<AlternatingRowStyleBackColor="White"/>28<EditRowStyleBackColor="#CE5D5A"Font-Bold="True"ForeColor="White"/>29<FooterStyleBackColor="#CCCC99"/>30<HeaderStyleBackColor="#6B696B"Font-Bold="True"ForeColor="White"/>31<PagerStyleBackColor="#F7F7DE"ForeColor="Black"HorizontalAlign="Right"/>32<RowStyleBackColor="#F7F7DE"/>33</asp:DetailsView>34</div>
View Code
ListView控件
ListView控件可以显示使用数据源控件或ADO.NET获得的数据 。
使用模板和样式定义的格式显示数据。利用ListView控件,可以逐项显示数据,也可以按组显示数据。
ListView 控件的特点:
1.支持绑定到数据源控件,例如SqlDataSource和ObjectDataSource。
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<addname="ConnectionString"connectionString="Data Source=追风的蜗牛;Initial Catalog=Student;Integrated Security=True"/>3</connectionStrings>
View Code
在Default.aspx中添加如下代码:
1<div>2<asp:SqlDataSourceID="SqlDataSource1"runat="server"ConnectionString="<%$ConnectionStrings:ConnectionString %>"3ProviderName="System.Data.SqlClient"DataSourceMode="DataReader"SelectCommand="select * from Student">4</asp:SqlDataSource>5<asp:ListViewID="ListView1"runat="server"DataSourceID="SqlDataSource1">6<LayoutTemplate>7<tablerunat="server"id="Table1">8<trrunat="server"id="itemPlaceHolder"/>9</table>10</LayoutTemplate>11<ItemTemplate>12<trrunat="server">13<tdid="td1"runat="server">14<asp:LabelID="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坐标值。