ASPNET 4.0 基础教程(C#)第三章 Web控件

上传人:灯火****19 文档编号:122027104 上传时间:2020-02-29 格式:PPT 页数:73 大小:1.41MB
返回 下载 相关 举报
ASPNET 4.0 基础教程(C#)第三章 Web控件_第1页
第1页 / 共73页
ASPNET 4.0 基础教程(C#)第三章 Web控件_第2页
第2页 / 共73页
ASPNET 4.0 基础教程(C#)第三章 Web控件_第3页
第3页 / 共73页
ASPNET 4.0 基础教程(C#)第三章 Web控件_第4页
第4页 / 共73页
ASPNET 4.0 基础教程(C#)第三章 Web控件_第5页
第5页 / 共73页
点击查看更多>>
资源描述

《ASPNET 4.0 基础教程(C#)第三章 Web控件》由会员分享,可在线阅读,更多相关《ASPNET 4.0 基础教程(C#)第三章 Web控件(73页珍藏版)》请在金锄头文库上搜索。

1、内容 Web控件类控件事件列表控件表控件验证控件 第3章ASP NET服务器控件 一 基本控件 ASP NET提供了与HTML元素相对应的基本的Web控件 诸如Label TextBox控件等 一 基本控件 示例 参考本书例3 1 二 Web控件类 Web控件类都被放置在System Web UI WebControls命名空间下面 二 Web控件类 1 基本属性 Web控件的基类WebControl定义了一些可以应用于几乎所有的Web控件的基本属性 见书中的表3 2 二 Web控件类 1 基本属性 单位Web控件提供了诸如Borderwidth Width和Hight属性来控制控件显示的大小

2、 可以使用一个数值加一个度量单位设置这些属性 这些度量单位包括相素 pixels 百分比等 在设置这些属性时 必须添加单位符号px 表示相素 或 百分比 以指明使用的单位类型 二 Web控件类 1 基本属性 示例 在页面中定义 在代码中定义 1 TextBox1 Height Unit Pixel 20 2 TextBox1 Width Unit Pixel 300 二 Web控件类 1 基本属性 2 枚举Web控件的一些属性的值只能为类库提供的枚举值 示例 例如 设置一个控件的BackColor的属性 可以从颜色的枚举值中选取一个值 textBox1 BackColor Color Red

3、1 基本属性 3 颜色在 NET框架中 命名空间System Drawing提供了一个Color对象 使用该对象可以设置控件的颜色属性 创建颜色对象的方式有如下几种 使用ARGB alpha red green blue 颜色值 可以为每个值指定一个从0到255的整数 其中alpha表示颜色的透明度 当alpha的值为255时表明完全不透明 red表示红色 当red的值为255时表示颜色为纯红色 green表示绿色 当green的值为255时表示颜色为纯绿色 blue表示蓝色 当blue的值为255时表示颜色为纯蓝色 使用颜色的枚举值 可供挑选的颜色名有140个 使用HTML颜色名 可以使用类

4、ColorTranslator把字符串转换成颜色值 1 基本属性 示例 intalpha 255 red 0 green 255 blue 0 textBox1 BackColor Color FromArgb alpha red green blue textBox1 BackColor Color Red textBox1 BackColor ColorTranslator FromHtml Blue 1 基本属性 4 字体控件的字体属性依赖于定义在命名空间System Web UI WebControls中的对象FontInfo FontInfo提供的属性如下表 1 基本属性 示例 设置

5、按钮Button1的字体属性Button1 Font Name Verdana 设置字体为VerdanaButton1 Font Bold true 加粗Button1 Font Size FontUnit Small 设置字体的相对大小Button1 Font Size FontUnit Point 14 设置字体的实际大小为14像素 二 Web控件的事件 1 事件模型 Web控件的事件的工作方式与传统的HTML标记的客户端事件工作方式有所不同 这是因为HTML标记的客户端事件是在客户端引发和处理的 而ASP NET页面中的Web控件的事件是在客户端引发 在服务器端处理 Web控件的事件模型

6、是这样来描述的 客户端捕捉到事件信息 然后通过HTTPPOST将事件信息传输到服务器 而且页框架必须解释该POST以确定所发生的事件 然后在要处理该事件的服务器上调用代码中的相应方法 下图描述了Web控件的模型 二 Web控件的事件 1 事件模型 二 Web控件的事件 2 基本的事件 基于以上的事件模型 Web控件事件可能会影响到页面的性能 因此 Web控件仅仅提供有限的一组的事件 如下表所示 二 Web控件的事件 3 事件的绑定 在处理Web控件时 需要把事件绑定到方法 事件处理程序 一个事件就是一条信息 例如 某按钮被单击 在应用程序中 必须将信息转换成代码中的方法调用 事件消息与特定方法

7、之间的绑定是通过事件委托来实现的 在ASP NET页面中 如果控件是以声明的方式在页中创建的 则就不需要显示地对委托进行编码 二 Web控件的事件 3 事件的绑定 例如把一个Button控件的Click事件绑定到名为ButtonClick的方法 代码如下 如果控件是被动态创建的 则就需要使用代码动态地绑定事件到方法 Buttonb newButton b Text 按钮 b Click newSystem EventHandler ButtonClick 三 列表控件 1 ListBox控件 ListBox控件用于创建多选的下拉列表 而可选项是通过ListItem元素来定义的 ListBox控

8、件提供如右表所示的属性 1 ListBox控件 ListBox控件提供如下表所示的方法 例3 2 页面代码 Page Load事件函数中的代码 protectedvoidPage Load objectsender EventArgse if Page IsPostBack 数据生成DataSetds newDataSet ds Tables Add stu ds Tables stu Columns Add stuNo typeof int ds Tables stu Columns Add stuName typeof string ds Tables stu Columns Add st

9、uScore typeof int ds Tables stu Rows Add newobject 1 张一 100 ds Tables stu Rows Add newobject 2 王二 100 ds Tables stu Rows Add newobject 3 李三 100 ds Tables stu Rows Add newobject 4 赵四 100 ds Tables stu Rows Add newobject 5 周五 100 绑定数据到ListBox控件this ListBox1 DataSource ds Tables stu this ListBox1 DataV

10、alueField stuNo this ListBox1 DataTextField stuName this ListBox1 DataBind 示例 事件 ListBox1 SelectedIndexChanged protectedvoidListBox1 SelectedIndexChanged objectsender EventArgse this Label1 Text 你选择的学生是 学号 this ListBox1 SelectedValue ToString 姓名 this ListBox1 SelectedItem Text ToString 2 DropDownLis

11、t控件 DropDownList控件提供可为用户单选的下拉列表框 该控件类似于ListBox控件 只不过它只在框中显示选定项和下拉按钮 而当用户单击下拉按钮时将显示可选项的列表 2 DropDownList控件 DropDownList控件提供如下表所示的属性 2 DropDownList控件 DropDownList控件提供如下表所示的方法 2 DropDownList控件 示例 页面代码 2 DropDownList控件 Page Load事件函数中的代码 if Page IsPostBack 数据生成DataSetds newDataSet ds Tables Add stu ds Ta

12、bles stu Columns Add stuNo typeof int ds Tables stu Columns Add stuName typeof string ds Tables stu Columns Add stuScore typeof int ds Tables stu Rows Add newobject 1 张一 100 ds Tables stu Rows Add newobject 2 王二 100 ds Tables stu Rows Add newobject 3 李三 100 ds Tables stu Rows Add newobject 4 赵四 100

13、ds Tables stu Rows Add newobject 5 周五 100 绑定数据到ListBox控件this DropDownList1 DataSource ds Tables stu this DropDownList1 DataValueField stuNo this DropDownList1 DataTextField stuName this DropDownList1 DataBind 3 CheckBoxList控件 CheckBoxList控件用来创建多项选择复选框组 该复选框组可以通过将控件绑定到数据源动态创建 3 CheckBoxList控件 CheckBo

14、xList控件提供如下表所示的属性 3 CheckBoxList控件 CheckBoxList控件提供如下表所示的方法 示例 页面代码 Page Load事件函数中的代码 if Page IsPostBack 数据生成DataSetds newDataSet ds Tables Add stu ds Tables stu Columns Add stuNo typeof int ds Tables stu Columns Add stuName typeof string ds Tables stu Columns Add stuScore typeof int ds Tables stu R

15、ows Add newobject 1 苹果 100 ds Tables stu Rows Add newobject 2 香蕉 100 ds Tables stu Rows Add newobject 3 梨 100 ds Tables stu Rows Add newobject 4 哈密瓜 100 ds Tables stu Rows Add newobject 5 仙人果 100 绑定数据到ListBox控件this CheckBoxList1 DataSource ds Tables stu this CheckBoxList1 DataValueField stuNo this C

16、heckBoxList1 DataTextField stuName this CheckBoxList1 DataBind 4 RadioButtonList控件 RadioButtonList控件为网页开发人员提供了一组单选按钮 这些按钮可以通过绑定动态生成 RadioButtonList控件提供如下表所示的属性 4 RadioButtonList控件 RadioButtonList控件提供如下表所示的方法 示例 页面代码 Page Load事件函数中的代码 if Page IsPostBack 数据生成DataSetds newDataSet ds Tables Add stu ds Tables stu Columns Add stuNo typeof int ds Tables stu Columns Add stuName typeof string ds Tables stu Columns Add stuScore typeof int ds Tables stu Rows Add newobject 1 乒乓球 100 ds Tables stu Rows Add n

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号