十四ASP.Net Web服务器控件
1.Label服务器控件
属性 | 说明 |
Text | 指定控件显示的文本内容。 |
AccessKey | 指定激活控件的与Alt键配合的组合键。 |
AssociatedControlID | 指定被激活的空间ID。 |
注:Alt组合键有些已被系统指定,包括F、E、V、I、O、A、W、H。
2.Literal服务器控件
控件用于在浏览器上显示整个过程中不发生变化的文本。
Mode属性 | 说明 |
Transform | 写入文本受Html标签影响,在外部截取时会剔除Html标签。 |
PassThrough | 写入文本受Html标签影响,在外部截取时不会剔除Html标签。 |
Encode | 显示写入文本的Html标签。 |
3.TextBox服务器控件
该控件在窗体上提供一个文本框,让终端用户输入文本。
控件介绍,TextMode属性:
TextMode属性 | 说明 |
SingleLine | 单行文本输入。 |
MultiLine | 多行文本输入。 |
Password | 密码输入。 |
控件介绍,Foucs()方法:可以把终端用户的光标动态的放置在某个指定的窗体元素上。
控件介绍,AutoPostBack:自动回传方法,本控件是指在完成文本框的填写后,光标移出文本框时触发的事件。
控件介绍,AutoCompleteType:自动完成,属性选项中有设置好的类型和内容。
4. Button服务器控件
此控件是提交窗体的常用元素。
控件介绍,CausesValidation属性:触发按钮事件不会激活验证控件。
控件介绍,Commandname属性:在后台集中处理按钮事件,然后利用此属性进行分流。
使用客户端JavaScript的按钮:OnClientClick绑定JavaScript函数,与OnClick无缝连接,可以同事处理两组事件。
5.LinkButton服务器控件
具有超链接外形的Button控件的变体。
6. ImageButton服务器控件
可以使用定制图像作为窗体的Button控件的变体。使用ImageUrl来指定图片的路径。
7. HyperLink服务器控件
此控件可以通过编程的方式处理Web页面上的任意超链接。包括文字(Text)和图像(ImageUrl),转向路径(NavigateUrl)。
8. DorpDownList服务器控件
次控件在用于使终端用户在中大型集合中选择一项。
示例,绑定数据源:
.ASPX部分
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DDPDemo.aspx.cs" Inherits="DDPDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Select transporation type:
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>Select an Item</asp:ListItem>
<asp:ListItem>Car</asp:ListItem>
<asp:ListItem>Airplane</asp:ListItem>
<asp:ListItem>Train</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" Visible="False">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="Select Options"
onclick="Button1_Click" />
</div>
</form>
</body>
</html>
.CS部分
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class DDPDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string[] CarArray = new string[4] {
"Ford","Honda","BWM","Dodge"
};
string[] AirplaneArray = new string[3]{
"Boeing 777","Boeing 747","Boeing 737"
};
string[] TrainArray = new string[3] {
"Bullet Train","Amtrack","Tram"
};
if (DropDownList1.SelectedValue == "Car")
{
DropDownList2.DataSource = CarArray;
}
else if (DropDownList1.SelectedValue == "Airplane")
{
DropDownList2.DataSource = AirplaneArray;
}
else
{
DropDownList2.DataSource = TrainArray;
}
DropDownList2.DataBind();
DropDownList2.Visible = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("You selected <b>" +
DropDownList1.SelectedValue.ToString() + ":" +
DropDownList2.SelectedValue.ToString() + "</b>"
);
}
}
注:这个示例包括了DropDownList的两种数据绑定方式(自定义数据绑定,指定数据源绑定)。
示例,集合可视化删除:
.ASPX部分
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DDP2Demo.aspx.cs" Inherits="DDP2Demo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>First Choice</asp:ListItem>
<asp:ListItem Enabled="False">Second Choice</asp:ListItem>
<asp:ListItem>Third Choice</asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
.CS部分
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class DDP2Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("You selected item number" +
DropDownList1.SelectedValue + "<br />");
Response.Write("You didin't select item number" +
DropDownList1.Items[1].Value
);
}
}
9. ListBox服务器控件
显示一个数据项集合,并允许终端用户在集合中选择多项。
示例,添加选项,选择多项:
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LBDemo.aspx.cs" Inherits="LBDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add an additional item"
onclick="Button1_Click" />
<br />
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem>Hematite</asp:ListItem>
<asp:ListItem>Haltite</asp:ListItem>
<asp:ListItem>Limonite</asp:ListItem>
<asp:ListItem>Magnetite</asp:ListItem>
</asp:ListBox>
<br />
<asp:Button ID="Button2" runat="server" Text="Submit" onclick="Button2_Click" />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
</form>
</body>
</html>
.CS
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class LBDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
ListBox1.Items.Add(TextBox1.Text.ToString());
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "You selected from the ListBox:<br/>";
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected == true)
{
Label1.Text += li.Text + "<br/>";
}
}
}
}