700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 风影ASP.NET基础教学 2 web服务器控件

风影ASP.NET基础教学 2 web服务器控件

时间:2020-05-10 21:39:43

相关推荐

风影ASP.NET基础教学 2 web服务器控件

在中,web标准服务器控件是ASPNET的核心部件,也是Web Form编程模型的最基本元素。相对于Html服务器控件而言,它具有更多的内置功能和可编程性。它不仅包括简单的按钮和文本框这样的简单控件,还有一些特殊用途的控件如日期、菜单和树形控件

1.与Html服务器控件的区别

html服务器控件不可以自动识别服务器达到自动变化的效果

编程扩展web服务器控件很容易可以扩展新的功能

Html服务器控件事件大多都是在客户端,而服务器控件是在服务端

2.WebControl基类

在中,所有的web服务器控件都定义在System.Web.UI.Controls命名空间下,都派生自WebControl。那么就有了一些公共属性

2.1常用属性

3. 单位

Web服务器控件的宽度和高度和类似的属性都是以单位进行设置的。单位是以对象(Unit结构)的形式出现的。Unit对象可以接收像素px和百分比%等这样的度量单位

aspnet页面标签代码

<asp:Button ID="Button1" runat="server" Width="100%" Height="30px" Text="Button" />

后置代码

Button1.Height = new Unit("20px");

Button1.Width = new Unit("100%");

Button1.Height = Unit.Pixel(20);

Button1.Width = Unit.Percentage(80);

Button1.Height = new Unit(20, UnitType.Pixel);

Button1.Width = new Unit(80, UnitType.Percentage);

4.颜色

int alpha = 255;

int red = 255;

int green = 255;

int blue = 0;

Button1.ForeColor = Color.FromArgb(red, green, blue);

Button1.ForeColor = Color.FromArgb(alpha, red, green, blue);

Button1.ForeColor = Color.FromName("Red");

Button1.ForeColor = Color.Red;

Button1.ForeColor = ColorTranslator.FromHtml("Red");

5. 字体

Font属性完整的引用了FontInfo对象,属性如下

注意:Names属性要和Name属性同步,设置任何一个都会影响到另一个,只需要设置Name属性为Names属性集合中的一个即可

Button1.Font.Name = "微软雅黑";

Button1.Font.Bold = true;

Button1.Font.Names = new string[] { "微软雅黑,宋体,黑体" };

Button1.Font.Size = FontUnit.XSmall;

Button1.Font.Size = FontUnit.Point(16);

Button1.Font.Italic = true;

Button1.Font.Underline = true;

6.默认按钮

<form id="form1" runat="server" defaultbutton="Button1">

<div>

<asp:Button ID="Button1" runat="server" Width="100%" Height="30px" Text="Button" />

</div>

</form>

当用户按下Enter的时候会触发defaultbutton的click事件。这个属性只能填入 Button,ImageButton和LinkButton

7.数据显示控件

7.1Label

我们在Winform已经学过Label了,顾名思义,就是显示文本到html页面里,它会生成一个Span的网页标记。最常用的属性是Text

7.2Literal

这个控件也是在Web页面上输出一个静态文本但是这个控件不会添加任何其他的多余标记,只是普通的文本输出

显示源代码结果:

8.数据输入控件

8.1 TextBox

8.2CheckBox控件

这个大家也比较熟悉了,依然是Checked属性来判断复选框是否被选中了。但是有所不同的是,这个CheckBox有一个Text属性,可以直接把复选框的内容也一块显示出来

<asp:CheckBox ID="CheckBox1" runat="server" Text="我是复选框"/>

结果:

生成的html代码:

我们会发现,微软人性化的帮我们把内容和Check控件绑定起来了。

8.3CheckBoxList

相比之前的CheckBox而言,CheckBoxList可以创建一组复选框。

<asp:CheckBoxList ID="CheckBoxList1" runat="server">

<asp:ListItem Text="aaa" Value="a" />

<asp:ListItem Text="bbb" Value="b" />

<asp:ListItem Text="ccc" Value="c" Selected="True" />

</asp:CheckBoxList>

结果:

生成的源代码

<table id="CheckBoxList1">

<tr>

<td><input id="CheckBoxList1_0" type="checkbox" name="CheckBoxList1$0" value="a" /><label for="CheckBoxList1_0">aaa</label></td>

</tr><tr>

<td><input id="CheckBoxList1_1" type="checkbox" name="CheckBoxList1$1" value="b" /><label for="CheckBoxList1_1">bbb</label></td>

</tr><tr>

<td><input id="CheckBoxList1_2" type="checkbox" name="CheckBoxList1$2" checked="checked" value="c" /><label for="CheckBoxList1_2">ccc</label></td>

</tr>

</table>

我们惊奇的发现微软我们生成了一个table 万恶的table啊能不能有其他方式呢。当然有了。

RepeatLayout="Flow"我们可以根据自己的意愿来组织html的生成效果。

8.3RadioButton 和RadioButtonList

同上

9.数据提交控件

9.1Button

它也是我们较常见的控件之一。

当然它和Winform里的Button有所不同,它可以作为命令按钮。Commed,我们可以用同一个处理方法来处理不同按钮的事件,达到更高的重用性,如何区分这些按钮呢,当然Sender是一个选择,另一个就是CommedName 和CommedArgument这两个属性,我们使用OnCommed事件就可以轻松获取这两个属性

代码

后置代码

生成的效果

这个OnCommed事件是所有按钮型控件都可以使用的。如LinkButton和ImageButton

10 图像显示控件

Image 和ImageMap这里掠过讲解了。

先写到这里吧,有问题请留言。第一篇写到讲义里了。后面我会整理后放出。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。