目录
1、五种基本的服务器控件类型
• HTML server controls
• Web server controls
• Validation server controls
• User controls
• Custom server controls
每一个控件都有自己的属性Properties、方法Methods和对应的事件Events。
2、服务器控件的对象模型
3、一些重要的WebControl类的属性
属性有时候也有自己的属性,这些叫子属性(subproperties)。例如:Font属性是一个复杂的对象,它有自己的一些属性,比如名字Name、大小Size。编程调用属性的时候使用点来引用属性(e.g., Label1.Text.Font.Size=10;)。当在子属性声明使用时,使用连字符来表示(e.g., <asp:Label id="Label1" Font-Size="10" runat="server" /> ).
4、Control类的一些重要属性
5、通过编程来控制属性
这些属性都是强类型化的(strongly typed),也就是说每一个属性都有固定的数据类型,有些是字符串型,有些是数字型,有些是枚举类型。
5.1 事件属性Event Properties
所有的控件都支持事件。事件属性的声明方法是通过前缀On来时表示,例如:<asp:button id="btnOne" runat="server" OnClick="btnOne_Click" />;而编程方式添加事件属性方式是:btnOne.Click += new EventHandler(btnOne_Click)。
Click事件的类型是EventHandler,它是一个委托类型。委托类型必须指定事件处理器方法(event handling method)的名称(如上面的例子就是:btnOne_Click); 这个方法必须有一个由EventHandler指定的签名。按照约定的规则,.NET Framework 中所有的事件委托都是具有两个参数(with two parameters),无返回类型的方法(void methods)。Source object that raised the event and the data for the event, usually contained within an EventArgs object (or an object derived from it). Thus, the method signature of your sample event handler would be void btnOne_Click(object source, EventArgs e)
5.2 量度属性Unit-Based Measurement Properties
所有度量单位的属性都是通过Unit Structure来处理,度量单位比如厘米cm, 英寸inch,和像素pixels在UnitType枚举中定义,如下图:
5.3 颜色属性Color-Based Properties
颜色属性使用Color structure来定义,每一种颜色的定义至少可以通过四种方法,例如:
//Color的命名空间using System.Drawing;// Set the color using predefined valuelabMsg.ForeColor = Color.Gold;
// Set the color using a predefined namelabMsg.ForeColor = Color.FromName("Gold");// Set the color using RGBlabMsg.ForeColor = Color.FromArgb(204, 153, 0);
// Set the color using hexadecimal HTML colorlabMsg.ForeColor = Color.FromName("#CC9900");5.4 集合属性Collection Propertie
有一些属性不是简单的结构或枚举类型,而是其它对象的集合。最典型的例子就是DropDownList的Item属性。Item属性包括0个或多个ListItem对象。这些集合属性有它们自己的方法和属性来定义集合的大小,以及添加、返回和删除集合中的元素。例如:
DropDownList drpSample = new DropDownList();// Create the item, then add to collectionListItem li = new ListItem("Item 2");
drpSample.Items.Add(li);
// Combine the creation and addition to collection stepsdrpSample.Items.Add(new ListItem("Item 1"));
5.5 附加的控件属性Additional Control Attributes
除了控件本身已经定义的属性外,我们还可以通过脚本语言来对添加控件的HTML属性(HTML attributes). Attributes是一个”名称-值”对(name-value pairs),它们被添加到HTML中。这中属性的主要作用是添加一些控件在客户端的行为。例如:
myButton.Attributes.Add("onclick","alert('Posting information...')");myButton.Attributes["onmouseout"] = "document.bgColor='green';";
myButton.Attributes["onmouseover"] = "document.bgColor='blue';";
上面这些代码在返回的HTML中表示为如下的形式:
<input type="submit" name="myButton" value="Click Me"
onclick="alert('Posting information...');" id="myButton"
onmouseout="document.bgColor='green';"
onmouseover="document.bgColor='blue';" />






浙公网安备 33010602011771号