WEB服务器控件编写一个WEB控件至少要包含三个元素:ASP:XXX指明是哪一类控件,ID指明控件的标识符,Ruant 指明是在服务器端运行的。
如:<asp:TextBox ID="TextBox1"runat="server"></asp:TextBox><br/><asp:Label ID="Label1"runat="server"Text="Label"></asp:Label><br/><asp:Button ID="Button1"runat="server"Text="Button"/> </div>一、WEB服务器控件2、3如label1.Text=”您好”;TextBox1.TextMode=TextBoxMode.SingleLine;二、文字控件Label用于显示文字,其最常用的属性是Text,用于显示的文字内容。
TextBox控件用于输入文字信息,WEB用于文本输入工作的只有TextBox控件,通过设置控件的TextMode属性来区分文本,密码,多行文本输入方式。
注意:文本框内容是字符串类型,如果要做计算,需要作类型转换。
如convert.tosingle转成单精度,或single.parse()MultiLine多行Pasword密码输入Columns 以字符为单位指明文本框的显示宽度Rows 当TextMode为MultiLine时,指明文本框的行数MaxLength 在单行文本方式下,文本框可以输入的字符数Wrap 当TextMode为MultiLine时,是否自动换行,默认为TRUEReadOnly 输入框为只读,默认为FALSEDataBind 将数据源绑定到被调用的服务器控件及其所有子控件上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 = "请输入密码";}}}文本控件案例二(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属性必须相同。
成员说明Checked 是否选中该控件GroupName 获取或设置单选钮所属的组名Text 文本标签TextAlign 文本标签的对齐方式,文本出现在左边还是右边。
CheckedChanged 当checked的值在向服务器发送期间更改时发生选择控件案例一(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控件成员说明Items属性获取列表项控件的集合,有以下常用属性和方法Count属性:集合中对象数Add方法:将ListItem追加到集合的末尾Clear方法:从集合中移除所有的ListItem 对象Remove方法:从集合中移除指定ListItem对象RepeatColumns属性设置控件中显示的列数RepeatDirection属性水平还是垂直显示SelectedIndex属性选定项的索引序号SelectedValue属性选定项的值选择控件案例三(5_6):单选复选组应用IsPostBack != true判断页面是否首次加载或刷新。
因为每次在执行button_click时,都会先执行page_load,如果不想执行,则用IsPostBack != trueprotected 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 = "您没有选择你喜欢的球队";elses = ",你喜欢的球队是:" + s.Substring(0, s.Length - 1);Label3.Text = Label1.Text+ s; }练习:用单选按钮列表控件及复选框列表控件完成系别及课程选择。