或许你会问,为什么需要web控件?web控间就好像vb里面的控间,可以在程序的任何地方引用,修改所有属性,有了它的配合才能完成代码分离。想一下,如果还是以前的html控间,我们当我们需要在某一个地方显示一段的内容是不是只有用<%= %>这样势必在html包含了程序的代码块,就达不到分离代码和界面的目的了。

     或许你还会问,为什么要代码分离那?原因很简单,不是每一个程序员都是合格的师,如果我们需要修改代码的时候会破坏网页设计师原来的设定,是不是不方便那,程序代码和html代码混合在一起对于程序员来说也不方便修改代码。当然,我们的网页设计师也需要学习一点web控间的知识。 

下面来一个一个介绍:

一、label控件:

主要属性:

text 标签所显示的文本

范例:<:label id=”lblmessage” text=”aaa” runat=”server”/>

需要注意的是我们不能遗漏runat=”server”字样,还有所有的web控件都要包含在<form runat=”server”></form>中间

下面看一个完整的例子,在程序里面修改label的text属性,以此改变网页的显示。

<script runat=”server” language=””>

void page_load()

{

lblmessage.text=”hello world!”;

}

</script>

<html>

<head><title>label.aspx</title></head> 

<body>

<form runat=”server”>

<asp:label id=”lblmessage” runat=”server”/>

</form>

</body>

</html>

————–

<script runat=”server” language=”vb”>

sub page_load

lblmessage.text=”hello world!”

end sub

</script>

<html>

<head><title>label.aspx</title></head> 

<body>

<form runat=”server”>

<asp:label id=”lblmessage” runat=”server”/>

</form>

</body>

</html> 

二、textbox控件:

主要属性: 

text 控件显示的文本;

maxlength 文本框可以添加的最多的字符数(多行文本框无效);

readonly 只读;

textmode 有下面几个有效值 multiline,password,singleline;

rows 指定文本框的垂直尺寸

三、button控件:

有三种:button 标准的表单按钮;imagebutton 显示图像的表单按钮;linkbutton 显示作为超链接样子button

button基本上就是text属性来设置按钮上面的文字;

imagebutton基本上就是imageurl来设置按钮上图象的地址;alternativetext 在不支持图片的时候显示的文字;

linkbutton基本上就是text属性来设置按钮上的文字;

他们三者共同的属性就是causesvalidation=true/false来设置按钮提交的表单是不是被检验(后面将说到检验控件)

他们三者共同的方法就是onclick就是点击按钮的时候触发的函数,下面举一个例子:<script runat=”server” language=”c#”>

void btncounter_onclick(object sender,eventargs e)

{

btncounter.text=”clicked”;



</script>

<html>

<head><title>label.aspx</title></head> 

<body>

<form runat=”server”>

<asp:button text=”unclicked” onclick=”btncounter_onclick” id=”btncounter” runat=”server”/>

</form>

</body>

</html> 

———————

<script runat=”server” language=”vb”>

sub btn_counter_onclick(s as object,e as eventargs)

btncounter.text=”clicked” 

end sub

</script>

<html>

<head><title>label.aspx</title></head> 

<body>

<form runat=”server”>

<asp:button text=”unclicked” onclick=”btncounter_onclick” id=”btncounter” runat=”server”/>

</form>

</body>

</html>