文章目录
服务器控件
Label
Label服务器控件用于在浏览器上显示文本。这是一个服务器控件,所以可以在服务
器端代码中动态地修改文本。在上面使用asp:Label控件的示例中,该控件使用Text属性
指定控件的内容,如下所示:
<asp:Label lD="Label1" Runat= "server" Text="Hello World" />
如果不使用Text属性,还可以把要显示的内容放在asp:Labe1元素之间,如下所示:
<asp:Label ID="Labell" Runat="server1">Hello World</asp:Label>
也可以通过编程提供控件的内容
Labell.Text = "Hello ASP、NET";
Literal
Literal服务器控件的工作方式非常类似于Label服务器控件。这个控件过去总是用于
要在浏览器上显示,且在整个过程屮不发生变化的文本(字面状态)。Label控件在文本的外
部加上元素,就可以改变输出,如下所示:
<span id="Labe11">Here is some text</span>
TextBox
顾名思义,该控件在窗体上提供一个文本框,让终端用户输入文本。可以把TextBox控件映射为窗体h使用的3个不同的HTML元素。首先,TextBox控件可以用作标准的HTML文本框,如下面的代码所示;
<asp:TextBox ID="TextBoxI" Runat="server"></asp:TextBox>
其次,TextBox控件允许终端用户把密码输入到窗体上。这只需把TextBox控件的TextMode属性改为Password即可,如下所示:
<asp:TextBox ID="TextBoxI" Runat="server" TextMode="Password"></asp: TextBox>
第三,TextBox服务器控件可以用作多行文本框。完成这个任务的代码如下所示:
<asp:TextBox ID = "TextBox1" Runat="server" TextMode="MultiLine"
Width="300px” Height="150px"></asp:TextBox>
Button
按钮是提交窗体的常用元素。在大多数情况下,都是通过Button控件的OnClick事件来处理包含在体中的各项
<asp:Button ID="Buttonl" Runat="serverr" OnClick="Buttonl_Click" />
客户端JavaScript的按钮
可以创建一个页面,其中有一个JavaScript事件和一个服务器端事件,它们在单击按钮时触发
<script language = "javascript">
function AlertHello()
{
}
</script>
html xmlns="http://www.w3.org/1999/xhtml">
〈head runat="server">
<title>Button Server Control/title>
</head>
<body>
<form id="form1" runat="server">
<asp:Button ID="Buttonl" runat="server" Text = "Button"
OnClientClick="AlertHello()" OnClick="Buttonl_Click" />
</form>
</body>
</html>
-- C#
<script runat="server">
protected void Buttonl_Click(object sender, EventArgs e)
{
}
</script>
LinkButton
LinkButton服务器控件是Button控件的一个变体,它基本上与Button控件相同,但
LinkButton控件采用超链接的形式,且这不是一般的超链接。终端用户单击该链接时,它 的行为与按钮类似。如果Web窗体上有非常多的按钮,这就是一个理想的控件,
LinkButton服务器控件的构造代码如下所示:
<asp:LinkButton ID="LinkButtonl" runat="server" OnClick="LinkButtonl_Click">
Submit your name to our database
</asp:LinkButton>
ImageButton
ImageButton控件也是Button控件的~个变体,它几乎与Button控件完全相同,可以使用定制图像作为窗体的按钮.
<asp: ImageButton ID="ImageButtonl1" runat="server" OnClick="ImageButtonl_Click" ImageUrl="MyButton.jpg" />
HyperLink
HyperLink服务器控件可以通过编程处理Web页面上的任意超链接。超链接允许终端
用户从一个页面移动到另一个页面。使用该控件的Text属性可以设置超链接的文本:
<asp:HyperLink id="HyperLinkl" runat="server" Text="Go to this page here”
NavigateUrl="/Default2.aspx"> </asp:HyperLink>
DropDownList服务器控件
DropDownList服务器控件可以把HTML选择框放在Web页面上,并对它编程操作。 如果集合中有许多项,希望终端用户从中选择一项时,使用DropDownList服务器控件是很理想的。该控件通常用于中大型集合。
DropDownList控件生成的选择框会显示一项,允许终端用户从较大的项目列表中选择 一项。根据选择框中的选项数,终端用户可能需要在一列项目中滚动。注意下拉列表中的 滚动条是浏览器根据其版本和列表中包含的项数自动创建的。
<asp:DropDownList ID="DropDownLis1" runat="server">
<asp:ListItem>Car</asp:ListItem>
<asp:ListItem>Airplane</asp:ListItem>
<asp:ListItem>Train</asp:Listltem>
</asp:DropDownList>
ListBox
ListBox服务器控件的功能类似于DropDownList控件:它也显示一个条目集合。但ListBox控件的操作不同于DropDownList控件,它可以给终端用户显示集合中的更多内容而DropDownList控件不可能做到这一点。
ListBox控件的代码如下所示:
<asp: ListBox ID="ListBoxl" runat="server" SelectionMode = "Multiple">
<asp: ListItem>Hematite</asp:Listltem>
<asp:ListItem>Halite</asp:Listltem>
<asp:ListItem>Limonite</asp:Listltem>
<asp:ListItem>Magnetite</asp:Listltem>
</asp:ListBox>
SelectionMode属性的值可以是Single或Multiple。把其值设置为Multiple,将允许终端用户在列表框中选择多项。
CheckBox
CheckBox控件允许把一个复选框放在窗体上
<asp:CheckBox ID="CheckBoxl” Runat="server" Text="Donate $10 to our cause!"
OnCheckedChanged="CheckBoxl_CheckedChanged" AntoPostBack ="true" />
CheckBoxList
CheckBoxList服务器控件非常类似于CheckBox控件,但前者允许操作一组条目,而
不是一个条目。CheckBoxList服务器控件实例是相关条目的集合,每个条目都有一个复
选框。
<asp:CheckBoxList ID="CheckBoxList1" Runat="server" DataSourceID= "SqlDataSourcel" DataTextField ="CompanyN3me" RepeatColumns= "3" BorderColor="Black" BorderStyle="Solid" Borderwidth="lpx">
</asp:CheckBoxList>
RadioButton
RadioBunon服务器控件非常类似于CheckBox服务器控件,它在Web贞面上放置一个 单选按钮。但与复选框不同,窗体上的一个单选按钮没有什么意义、单选按钮一般至少需 要两个选项。
<asp:RadioButton ID="RadioBu11on1" Runat="server" Text="Yes" GroupName="Set1" />
<asp:RadioButton ID="RadioButton2" Runat="server" Text="No" GroupNaiDe="Setl" />
RadioButtonList 服务器控件
RadioButtonList服务器控件允许在 Web页面上显示一个单选按钮集合。
RadioButtonList控件非常类似于CheckBoxList和其他列表控件,因为它允许遍历用户选择
的条目,以进行计数或执行其他操作。
<asp:RadioButtonList ID="RadioButtonList1" Runat="server">
<asp:Listitem Selected="True">English</asp:Listitem>
<asp:ListItem>Russian</a.sp:Listltem>
<asp:ListItem>Finnish</asp:Listltem>
<asp:ListItein>Swedish</asp:ListItem>
</asp:RadioButtonList>
Table
Table是Web页面上常用的元素,因为HTML 元素非常适合于控制Web页面的
布局。Table服务器控件的构建代码如下所示|
<asp:Table ID="Tablel" Runat="server">
<asp:tableRow Runat="server" Font-Bold="Truer" ForeColor="Black" BackColor="Silver">
<asp:TableHeaderCell>First Name</asp:TableHeaderCell>
<asp:TableHeaderCell>Last Name</asp:TableHeaderCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Bill</asp:TableCell>
<asp:TableCell>Evjen</asp:TableCell>
</asp:TableRow>
<asp:TableRow>
<asp:TableCell>Devin</asp:TableCell>
<asp:TableCell>Rader</asp:TableCell>
</asp:TableRow>
</asp:Table>