WEB服务器控件
编写一个WEB控件至少要包含三个元素:ASP:XXX指明是哪一类控件,ID指明控件的标识符,Ruant指明是在服务器端运行的。如:
一、WEB服务器控件
如label1.Text=”您好”;
TextBox1.TextMode=TextBoxMode.SingleLine;
二、文字控件
Label用于显示文字,其最常用的属性是Text,用于显示的文字内容。 TextBox控件用于输入文字信息,WEB用于文本输入工作的只有TextBox控件,通过设置控件的TextMode属性来区分文本,密码,多行文本输入方式。注意:文本框内容是字符串类型,如果要做计算,需要作类型转换。如convert.tosingle转成单精度,或single.parse()
文字控件案例一(5_3):制作登录界面
ID 属性设置
Label1 Text=用户名
Label2 Text=密码
LblMessage Text=””
protected void BtnRest_Click(object sender, EventArgs e)
{
TxtUserName.Text = "";
TxtPassWord.Text = "";
LblMessage.Text = "";
}
protected void BtnSumit_Click(object sender, EventArgs e)
{
if ((TxtUserName.Text.Trim() != "") && (TxtPassWord.Text.Trim() != "")) {
LblMessage.Text = "用户名:" + TxtUserName.Text + ":" + "密码" + TxtPassWord.Text;
}
else if (TxtUserName.Text.Trim() == "")
{
LblMessage.Text = "请输入用户名";
}
else
{
LblMessage.Text = "请输入密码";
}
}
}
文本控件案例二(5_1):显示日期
protected void Page_Load(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
this.lbltime1.Text = now.ToString();
this.lbltime2.Text = now.ToShortDateString();
this.lbltime3.Text = now.ToLongDateString();
this.lbltime4.Text = now.ToLongTimeString();
this.lbltime5.Text = now.ToShortTimeString();
}
文本控件案例三(5_2):显示金额
protected void Button1_Click(object sender, EventArgs e)
{
int money = Convert.ToInt32(TextBox1.Text);
Label1.Text = money.ToString("C");
Label2.Text = money.ToString("$#,###.00");
}
练习:制作页面,完成阶乘运算。
三、选择控件
选择控件包含CheckBox CheckBoxList DropDownList ListBox RadioButton RadioButtonList等控件
1、RadioButton
是多选一的控件,因此该控件还有一个专门的GroupName属性,同一组别的RadioButton 控件的GroupName属性必须相同。
控件的主要成员
选择控件案例一(5_4):单选按钮应用
添加两个单选按钮控件,Text的值分别设为“男”,“女”,GroupName的值为sex。
protected void Button1_Click(object sender, EventArgs e)
{
if (RadioButton1.Checked == true) Label1.Text = "性别:" +
RadioButton1.Text;
if (RadioButton2.Checked == true) Label1.Text = "性别:" +
RadioButton2.Text;
}
练习:完成选择系单选设计功能。
2、CheckBox控件
属性和RadioButton一样。
选择控件案例二(5_5):复选控件应用
添加三个复选按钮,Text的值按照以上图中设置。
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
string str1 = "你的选择是: ";
if (CheckBox1.Checked) str1 += CheckBox1.Text + " ";
if (CheckBox2.Checked) str1 += CheckBox2.Text + " ";
if (CheckBox3.Checked) str1 += CheckBox3.Text ;
Label1.Text = str1;
}
练习:完成选择课程复选功能设计
3、CheckBoxList和RadioButtonList控件
主要成员
选择控件案例三(5_6):单选复选组应用
IsPostBack != true判断页面是否首次加载或刷新。因为每次在执行button_click时,都会先执行page_load,如果不想执行,则用IsPostBack != true
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack != true)
{
string[] player = new string[3] { "姚明", "科比", "邓肯" };
string[] team=new string[3]{"小牛","太阳","火箭"};
RadioButtonList1.DataSource = player;
RadioButtonList1.DataBind();
CheckBoxList1.DataSource = team;
CheckBoxList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "你最喜欢的球员是";
Label1.Text += RadioButtonList1.SelectedValue;
int i = 0;
string s = "";
for (i = 0; i <= CheckBoxList1.Items.Count - 1; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
s += CheckBoxList1.Items[i].Value + "、";
}
}
if (s == "")
s = "您没有选择你喜欢的球队";
else
s = ",你喜欢的球队是:" + s.Substring(0, s.Length - 1);
Label3.Text = Label1.Text+ s; }
练习:用单选按钮列表控件及复选框列表控件完成系别及课程选择。也可以通过以下语句对控件进行初始化:
if (IsPostBack != true)
{
RadioButtonList1.Items.Add("星期一");
RadioButtonList1.Items.Add("星期二");
RadioButtonList1.Items.Add("星期三");
CheckBoxList1.Items.Add("一");
CheckBoxList1.Items.Add("二");
CheckBoxList1.Items.Add("三");
}
或者
ArrayList zy1 = new ArrayList();
zy1.Add("星期一");
zy1.Add("星期二");
zy1.Add("星期三");
RadioButtonList1.DataSource = zy1;
RadioButtonList1.DataBind();
ArrayList zy2 = new ArrayList();
zy2.Add("一");
zy2.Add("二");
zy2.Add("三");
CheckBoxList1.DataSource = zy2;
CheckBoxList1.DataBind();
总结:
CheckBoxList1.DataSource 数据源
CheckBoxList1.DataBind()数据源绑定控件
CheckBoxList1.Items.Count 列表框中包含项数
CheckBoxList1.SelectedIndex 选中项的索引值,选中项的索引为-1,即没有选中项CheckBoxList1.SelectedValue选定项的值
CheckBoxList1.Items.Add()在列表框中加入某项;
CheckBoxList1.Items.Remove()在列表框中清除某一项
CheckBoxList11.Items.Clear()在列表框中清除所有项;
CheckBoxList1.Items[i].Text第i项的文本
CheckBoxList1.Items[i].Selected第i项是否选中
CheckBoxList1.Items[i] 第i项
选择控件案例四(5_7):动态加载控件
IsPostBack页面是否首次加载或刷新。
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string hobbys = "旅游,运动,阅读,登山,音乐,泡吧,喝酒,品茶,下厨,模型";
foreach(string hobby in hobbys.Split(new char[]{','} ) )
{ ListItem item=new ListItem();//checkboxlist中每一项的类型
item.Text = hobby;
item.Value = hobby;
this.cdlhobby.Items.Add(item );
continue;
}}
}
protected void Button1_Click(object sender, EventArgs e)
{
https://www.doczj.com/doc/d210108771.html,bel1.Text = "你选择的爱好是:";
foreach (ListItem item in this.cdlhobby.Items)
{
if (item.Selected == true)
{
https://www.doczj.com/doc/d210108771.html,bel1.Text += item.Text + ",";
}
}
}
4、DropDownList控件
是一种多选一的控件,该控件功能与RadioButton控件相似。在使用时需要和子控件listItem配合。
若要判断控件哪个选项被选中,有以下两种方式
(1)使用控件的SelectedIndex属性
该属性表示被选择项的索引编号,从“0”开始计算;其值若为“-1”,表示没有任何项被选择。(2)使用控件的SelectedItem属性
通过该属性的Text、Value属性可以获取该项的文本或值。SelectedItem.Text
选择控件案例五(5_8)
protected void Page_Load(object sender, EventArgs e)
{
string[] zy = new string[3] { "教师", "学生", "工人" };
if (IsPostBack != true)
{
DropDownList1.DataSource = zy;
DropDownList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedItem.Text;
//Label1.Text = DropDownList1.SelectedValue;
//Label1.Text = DropDownList1.SelectedIndex.ToString();
}
练习:用下拉列表选择系别,用arraylist初始化。
选项的初始化也可以使用下面的代码
if(IsPostBack!=true)
{
DropDownList1.Items.Add("教师");
DropDownList1.Items.Add("学生");
DropDownList1.Items.Add("工人");
}
5、ListBox控件
该控件是一种可单选/多选设置的控件,该控件功能灵活,既可以设置成单选方式,也可以设置成多选方式。
主要成员
选择控件案例六(5_11)
protected void Page_Load(object sender, EventArgs e)
{
Button1.Text = "提交";
Label1.Text = "";
ListBox1.SelectionMode =ListSelectionMode.Multiple;
if (IsPostBack != true)
{
ArrayList sz = new ArrayList();
sz.Add("高数");
sz.Add("英语");
sz.Add("大学语文");
sz.Add("https://www.doczj.com/doc/d210108771.html,");
ListBox1.DataSource = sz;
ListBox1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "你选择的是:"+ListBox1.SelectedValue;
}
分析结果:
想要显示所有选定的项,程序应该怎样改?
练习:判定是否选了高数课程,如果是,显示本学期不能开此课程。
总结:
ListBox1.DataSource 数据源
ListBox1.DataBind()数据源绑定控件
ListBox1.Items.Count 列表框中包含项数
ListBox1.SelectedIndex == -1选中项的索引为-1,即没有选中项
ListBox1.SelectedValue选定项的值
ListBox1.SelectionMode设定控件的选择模式:Multiple表示可多选,Single表示只能单选。
ListBox1.Items.Add()在列表框中加入某项;
ListBox1.Items.Remove()在列表框中清除某一项
ListBox1.Items.Clear()在列表框中清除所有项;
ListBox1.Items[i].Text第i项的文本
ListBox1.Items[i].Selected第i项是否选中
ListBox1.Items[i] 第i项。例ListBox2.Items.Remove(ListBox2.Items[i])移除第i项。
选择控件案例六(5_9)
using System.Collections;
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack != true)
{
ArrayList city = new ArrayList();
city.Add("北京");
city.Add("上海");
city.Add("天津");
city.Add("重庆");
city.Add("沈阳");
ListBox1.DataSource = city;
ListBox1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ListBox1.Items.Count > 0)
{
int i;
for (i = 0; i <= ListBox1.Items.Count - 1; i++)
ListBox2.Items.Add(ListBox1.Items[i].Text);
ListBox1.Items.Clear();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
int i;
for (i = 0; i <= ListBox2.Items.Count - 1; i++)
ListBox1.Items.Add(ListBox2.Items[i].Text);
ListBox2.Items.Clear();
}
protected void Button2_Click(object sender, EventArgs e)
{
int i;
if (ListBox1.Items.Count > 0 && ListBox1.SelectedIndex == -1)
{ }
else
{
for (i = 0; i <= ListBox1.Items.Count - 1; i++)
{
if (ListBox1.Items[i].Selected)
{
ListBox2.Items.Add(ListBox1.Items[i].Text);
ListBox1.Items.Remove(ListBox1.Items[i]);
}
}
}
}
protected void Button3_Click(object sender, EventArgs e)
{
int i;
if (ListBox2.Items.Count > 0 && ListBox2.SelectedIndex == -1)
{ }
else
{
for (i = 0; i <= ListBox2.Items.Count - 1; i++)
{
if (ListBox2.Items[i].Selected)
{
ListBox1.Items.Add(ListBox2.Items[i].Text);
ListBox2.Items.Remove(ListBox2.Items[i]);
}
}
}
}(一班)
选择控件案例七(5_10)控件的联动
protected void Page_Load(object sender, EventArgs e)
{
//DropDownList1.AutoPostBack = true;
if (IsPostBack != true)
{
string[] zy = new string[2] { "临汾", "吕梁" };
DropDownList1.DataSource = zy;
DropDownList1.DataBind();
DropDownList1.SelectedIndex = 0;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
ArrayList sz = new ArrayList();
if (DropDownList1.Items[0].Selected)
{
sz.Add("霍州");
sz.Add("候马");
}
if (DropDownList1.Items[1].Selected)
{
sz.Add("吕梁");
sz.Add("中阳");
}
ListBox1.DataSource = sz;
ListBox1.DataBind();
}
注意:
AutoPostBack是用在一个控件中的值变换会带来另一个控件值变换时,需要设置为true,它的AutoPostBack属性默认为False,如果不把属性改为True,他的SelectedIndexChanged()事件中的代码就不会执行
动态加载控件(dtjz.aspx)
Label lab1=new Label();
Label lab2 = new Label();
RadioButtonList rd1=new RadioButtonList() ;
protected void Page_Load(object sender, EventArgs e)
{
{
lab1.Text="一个完整的计算机系统包括";
PlaceHolder1.Controls.Add(lab1);
rd1.Items.Add("A");
rd1.Items.Add("B");
rd1.Items.Add("C");
rd1.Items.Add("D");
PlaceHolder1.Controls.Add(rd1);
Button btn1=new Button();
btn1.Text="确定";
btn1.Click+=new EventHandler(btn1_Click);
PlaceHolder1.Controls.Add(btn1);
PlaceHolder1.Controls.Add(lab2);
}
}
protected void btn1_Click(object sender, EventArgs e)
{
lab2.Text="
你选择的是:"+rd1.SelectedValue;
}
文件上传控件
protected void Button1_Click(object sender, EventArgs e)
{
string fileExt = System.IO.Path.GetExtension(File1.FileName);
string dir = Server.MapPath(TextBox1.Text);
string fd = dir + "/banner.gif";
if ((Directory.Exists(dir)) && (File1.HasFile))
{
if (fileExt != ".gif")
Response.Write("");
else
{
File1.PostedFile.SaveAs(fd);
Response.Write("");
Label1.Text = "文件类型:"+File1.PostedFile.ContentType;
Label1.Text += "
文件大小:" + File1.PostedFile.ContentLength;
Label1.Text += "
客户端路径:" + File1.PostedFile.FileName;
Label1.Text += "
图片预览:
" + "+"; }
}
}
四、按钮控件
1、Button
2、ImageButton
从功能上看和Button控件一样,但是该控件是用图片作为按钮的表面。有两个属性:
ImageUrl
设置图片所在位置
按钮控件案例一(5_12)
protected void Page_Load(object sender, EventArgs e)
{
ImageButton1.ImageUrl = "images/tx.png";
ImageButton1.ToolTip = "请选择腾讯";
ImageButton2.ImageUrl = "images/wy.png";
ImageButton2.ToolTip = "请选择网易";
ImageButton3.ImageUrl = "images/xl.gif";
ImageButton3.ToolTip = "请选择新浪";
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e) {
Response.Redirect("https://www.doczj.com/doc/d210108771.html,");
}
protected void ImageButton2_Click(object sender, ImageClickEventArgs e) {
Response.Redirect("https://www.doczj.com/doc/d210108771.html,");
}
protected void ImageButton3_Click(object sender, ImageClickEventArgs e) {
Response.Redirect("https://www.doczj.com/doc/d210108771.html,");
}
3、i mage
属性ImageUrl:设置图片所在位置
按钮控件案例一(5_13)
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack != true)
{ DropDownList1.Items.Add("images/tx.png");
DropDownList1.Items.Add("images/wy.png");
DropDownList1.Items.Add("images/xl.gif");
Image1.ImageUrl = "images/xl.gif";
DropDownList1.AutoPostBack = true;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Image1.ImageUrl = DropDownList1.SelectedValue;
}
思考:IsPostBack != true在事件中的作用,如果去掉该判断语句,会出现什么问题?
应用二
五、超链接控件
1、HyperLink
属性:
Text 设置文本标题
NavigateUrl设置控件链接到的url
ImageUrl设置控件显示的图像路径
该控件创建一个可以跳转到其他页的链接,该链接既可以显示为文本,也可以显示为图像。若显示文本,则设置Text属性,若显示图像,则设置ImageUrl属性。如果同时设置二者,则ImageUrl属性优先;如果图像不可用,则显示文本。
2、LinkButton
控件具有和Button相同的属性
超链接控件案例一(5_14)
protected void Page_Load(object sender, EventArgs e)
{
HyperLink1.Text = "网易";
HyperLink1.NavigateUrl = "https://www.doczj.com/doc/d210108771.html,";
HyperLink1.ImageUrl = "imags/4.jpg";
LinkButton1.Text = "网易";
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.Redirect("https://www.doczj.com/doc/d210108771.html,");
}
六、页面显示控制控件
1、panel
利用该控件,可以将页面中的相关控件分组管理并进行显示隐藏。
显示控件案例一(5_15)
2、table控件
显示控件案例二(5_16)
T able由Rows集合组成,而Rows由若干个T ableRow组成,每个T ableRow由Cells集合组成,而Cells由若干个TableCell组成.
protected void Page_Load(object sender, EventArgs e)
{
int i;
if (IsPostBack == false)
{
select1.Items.Clear();
select2.Items.Clear();
for (i = 1; i <= 10; i++)
{
select1.Items.Add(i.ToString());
select2.Items.Add(i.ToString());
}
Table1.Rows.Clear();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int i, j;
int m = Convert.ToInt32(select1.Text);
int n = Convert.ToInt32(select2.Text);
for (i = 0; i <= m - 1; i++)
{
TableRow tr = new TableRow();
for (j = 0; j { TableCell td = new TableCell(); td.Text= "第" + (i + 1) + "行第" + (j + 1) + "列"; tr.Cells.Add(td); } Table1.Rows.Add(tr); } } 七、WEB验证控件 网页可供用户输入信息。为避免用户输入一些不规范的信息,就必须要在网页程序中加入检查功能,这种功能就是表单验证功能。在.NET中有专门的表单验证控件用于表单信息的验证。 3、可以使用验证控件的控件 并非所有的WEB服务器控件都能使用验证控件,可以使用验证控件的表单控件如下: 在一个网页程序中可以利用上述的几种验证控件对表单数据信息进行验证,如果一个网页中所有验证控件都通过了验证,那么该网页自身的Page对象的IsV alid属性将会是true;反之,如果网页Page对象的IsV alid属性是false值,则说明网页中至少有一个验证控件未能验证通过。 RequiredFieldValidator控件案例(5_17) 此控件用于对用户必须填写的关键性输入域进行验证。 在页面中添加一个按钮、一个标签、一个RequiredFieldV alidator控件,设置它的ControlToVAlidate为TextBox1,Text为“请输入姓名”。 RangeValidator控件案例(5_18) 此控件限制用户输入指定范围内的数据。 CompareValidator控件 对两个控件进行比较,或者用于控件和常量之间的比较。 常用属性有: ControlToV alidate:要验证的控件ID ControlToCompare:要比较的控件ID WEB服务器控件 编写一个WEB控件至少要包含三个元素:ASP:XXX指明是哪一类控件,ID指明控件的标识符,Ruant 指明是在服务器端运行的。如: WEB服务器控件
MultiLine多行 Pasword密码输入 Columns 以字符为单位指明文本框的显示宽度 Rows 当TextMode为MultiLine时,指明文本框的行数 MaxLength 在单行文本方式下,文本框可以输入的字符数 Wrap 当TextMode为MultiLine时,是否自动换行,默认为TRUE ReadOnly 输入框为只读,默认为FALSE DataBind 将数据源绑定到被调用的服务器控件及其所有子控件上 TextChanged 当文本框内容发生变化时,触动。 文字控件案例一(5_3):制作登录界面 控件类型ID 属性设置说明 Label Label1 Text=用户名用于显示静态文本Label Label2 Text=密码用于显示静态文本Label LblMessage Text=””用于显示提示文本或登 录信息 TexBox TxtUserName TextMode=SingleLine 用于输入用户名TexBox TxtPassWord TextMode=Password 用于输入密码 Button BtnSumit Text=提交向服务器发送登录信息Button BtnRest Text=重置清除文本框内容 { TxtUserName.Text = ""; TxtPassWord.Text = ""; LblMessage.Text = ""; } protected void BtnSumit_Click(object sender, EventArgs e) { if ((TxtUserName.Text.Trim() != "") && (TxtPassWord.Text.Trim() != "")) { LblMessage.Text = "用户名:" + TxtUserName.Text + ":" + "密码" + TxtPassWord.Text; } else if (TxtUserName.Text.Trim() == "") { LblMessage.Text = "请输入用户名"; } else { LblMessage.Text = "请输入密码"; }
第8章Web服务器控件 ?特点 会根据Client 端的状况产生一个或多个适当的HTML 服务器控件,它可以自动侦测Client 端浏览器的种类,并自动调整成适合浏览器的输出。 Web 服务器控件还拥有一个非常重要的功能,那就是支持数据绑定(Data Binding );这种能力可以和数据源连结,用来显示或修改数据源的数据。 它们提供了一些能够简化开发工作的特性,其中包括: 丰富而一致的对象模型:WebControl 基类实现了对所有控件通用的大量属性,这些属性包括ForeColor、BackColor、Font、Enabled 等。属性和方法的名称是经过精心挑选的,以提高在整个框架和该组控件中的一致性。通过这些组件实现的具有明确类型的对象模型将有助于减少编程错误。 对浏览器的自动检测:Web 控件能够自动检测客户机浏览器的功能,并相应地调整它们所提交的HTML,从而充分发挥浏览器的功能。 数据绑定:在Web 窗体页面中,可以对控件的任何属性进行数据绑定。此外,还有几种Web 控件可以用来提交数据源的内容。 在HTML 标记中,Web 控件会表示为具有命名空间的标记,即带有前缀的标记。前缀用于将标记映射到运行时组件的命名空间。标记的其余部分是运行时类自身的名称。与HTML 控件相似,这些标记也必须包含runat="server" 属性。下面是一个声明的示例: <asp:TextBox id="textBox1" runat="server" Text="基督山伯爵"> </asp:TextBox> 介绍几种常见的web控件 4.2.1 Label Web控件 ?作用:用来显示文字。 ?语法:
《10.3Web服务器控件》 主题: (1)Web服务器控件简述 Web服务器控件具有以下特性: ●功能丰富的对象模型,该模型具有类型安全编程功能。由于Web服务器控件是 https://www.doczj.com/doc/d210108771.html,框架全新封装提供的新控件,因此其具有严格的类型安全检查。 ●自动浏览器检测。Web服务器控件可以检测浏览器的功能,并为基本型和丰富型 (HTML 4.0或以上) 浏览器创建适当的输出。 ●对于某些控件,可以使用模板来自定义控件的外观。 ●对于某些控件,可以指定控件的事件是立即发送到服务器,还是先缓存然后在提交 窗体时引发。 ●可将事件从嵌套控件(例如表中的按钮)传递到容器控件。 在设计时的“HTML”视图中,显示在页中的控件的格式如下:
示不设置该属性 ●BackColor属性:表示Web 服务器控件的背景色,默认为空,表示不设置该属性。 ●BorderColor属性:表示Web 控件的边框颜色,默认为空,表示不设置该属性。●BorderStyle属性:表示Web 服务器控件的边框样式,默认为NotSet,其取值为下 列值之一: ?NotSet 不设置边框样式。 ?None 无边框 ?Dotted 虚线边框。 ?Dashed 点划线边框。 ?Solid 实线边框。 ?Double 双实线边框。 ?Groove 用于凹陷边框外观的凹槽状边框。 ?Ridge 用于凸起边框外观的突起边框。 ?Inset 用于凹陷控件外观的内嵌边框。 ?Outset 用于凸起控件外观的外嵌边框。 ●BorderWidth属性:表示Web 服务器控件的边框宽度,默认为空,表示不设置该属 性。 ●Enabled属性:表示是否启用Web 服务器控件。 ●Font属性:表示Web 服务器控件关联的字体属性。 ●ForeColor属性:表示Web 服务器控件的前景色(通常是文本颜色)。 ●Height属性:表示Web 服务器控件的高度。 ●Style属性:包含要在Web 服务器控件的外部标记上呈现的HTML 样式属性 ●TabIndex属性:Web 服务器控件的选项卡索引。默认为0,表示不设置此属性 ●ToolTip属性:表示当鼠标指针悬停在Web 服务器控件上时显示的文本。 ●Width属性:表示Web 服务器控件的宽度。 2.其他的常用属性 ●ID属性:继承自System.Web.UI.Control类,所有Web服务器控件都可以通过该属 性来唯一标识和引用。 ●Text属性:所有接受用户输入,显示数据和提示数据的Web服务器控件都可以通 过Text属性来表示用于在控件上显示的文本,例如TextBox、Lablel、Button、CheckBox等控件。 ●AutoPostBack属性:所有发送窗体或单击按钮时将其数据回传到服务器的Web控 件都具有AutoPostBack属性,该属性是布尔类型。表示当用户修改控件中的文本并使焦点离开该控件时,是否向服务器自动回送。True表示每当用户更改文本框中的文本并使焦点离开该控件时,都会向服务器自动回送,否则为false。默认为false。 ●EnableViewState属性:继承自System.Web.UI.Control类,此属性用于定义控件的
实训2 web服务器控件的取值和属性设置 09计算机网络史丹 一、实训目的 1、掌握标签Label,文本框TextBox,单选按钮组RadioButtonList和多选按钮组CheckLBoxList,还有一个按钮Button控件的属性设置 2、学会如何获取上面这些控件的值 3、学会如何编写代码 二、实现功能及要求 练习添加新的Web窗体,设计一个用户注册界面,包括:姓名、账号、密码、性别、爱好、电子邮箱、说明;单击确定按钮后,在标签上输出用户的输入信息。 三、界面设计及后台代码 在https://www.doczj.com/doc/d210108771.html,应用程序开发界面左边的工具栏中,拖出8个标签Label,5个文本框TextBox,1个单选按钮组RadioButtonList和1个多选按钮组CheckLBoxList,还有一个按钮Button。分别在8个标签属性框找到Text属性输入对应的内容,在button的属性框里找到Text的属性,然后编写相应的内容;在单选按钮组RadioButtonList和多选按钮组CheckLBoxList 有一个项目集合(Items Collection)属性,单击后会出现添加项目窗口并可设置项目的显示文本和相应的值,并在属性框里设置他们的排列方式为水平排列。 如下图所示: 然后,双击确定按钮,在按钮的Click事件中写入如下代码,又双击复选框组CheckBoxList,在其中编写如下代码: using System; using System.Collections; using https://www.doczj.com/doc/d210108771.html,ponentModel; using System.Data; using System.Drawing;
谈谈HtmlControl与WebControl的区别以及各自应该什么时候用 Web控件和Html控件虽然好多功能相同并且长得很像 但是它们的内部实现机制是完全不一样的 Web控件要比Html控件执行效率要好 1. 使用起来也相当方便,举个简单的例子,例如Button的生成: Html控件是将庞大控件集合全部弄到页面中,用到哪个功能,就设置一下属性,如下: 这样会占用相当大的控件资源 Web控件是将集成式的拆解成单功能的: