asp.net服务器控件
6. CheeckBox服务器控件
Web窗体上的复选框允许用户选择集合中的条目,或者把一个条目的值指定为yes/on。
7. CheckBoxList服务器控件
与CheckBox控件相似,但是此控件允许操作一组条目。
示例,动态填充CheckBoxList控件:
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CBLDemo.aspx.cs" Inherits="CBLDemo" %>
<!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:Button ID="Button1" runat="server" Text="Submit Choices"
onclick="Button1_Click" />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="CompanyName"
DataValueField="CompanyName">
</asp:CheckBoxList>
<br />
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CompanyName] FROM [Customers]"></asp:SqlDataSource>
</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 CBLDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "You selected :<br/>";
foreach (ListItem li in CheckBoxList1.Items)
{
if (li.Selected == true)
{
Label1.Text += li.Text + "<br/>";
}
}
}
}
8. RadioButton服务器控件
此控件与CheckBox非常相似,它支持在页面上放置一组单选按钮,并用GroupName属性来控制分组。
示例:
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RBDemo.aspx.cs" Inherits="RBDemo" %>
<!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:RadioButton ID="RadioButton1" runat="server" Text="VB" GroupName="LC"
oncheckedchanged="RadioButton_CheckedChanged" AutoPostBack="True" />
<asp:RadioButton ID="RadioButton2" runat="server" Text="C#" GroupName="LC"
oncheckedchanged="RadioButton_CheckedChanged" AutoPostBack="True" />
</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 RBDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void RadioButton_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
Response.Write("You selected VB");
}
else
{
Response.Write("You selected C#");
}
}
}
9. RadioButtonList服务器控件
此控件可以在页面上显示一个单选按钮集合。
示例,捕捉选中值:
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RBLDemo.aspx.cs" Inherits="RBLDemo" %>
<!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:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True"
onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem>Russian</asp:ListItem>
<asp:ListItem>Finnish</asp:ListItem>
<asp:ListItem>Swedish</asp:ListItem>
</asp:RadioButtonList>
</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 RBLDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write("You selected :" + RadioButtonList1.SelectedItem.ToString());
}
}
10. Image服务器控件
此控件可以在服务器端代码中操作显示在Web页面上的图片。
11. Table服务器控件
控制页面布局。
示例,动态添加行:
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Tdemo.aspx.cs" Inherits="Tdemo" %>
<!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:Table ID="Table1" runat="server" Caption="Test Table">
<asp:TableRow runat="server" Font-Bold="true" 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:Table>
</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 Tdemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TableRow tr = new TableRow();
TableCell fname = new TableCell();
fname.Text = "Scott";
tr.Cells.Add(fname);
TableCell lname = new TableCell();
lname.Text = "Hanselman";
tr.Cells.Add(lname);
Table1.Rows.Add(tr);
}
}
12. Calendar服务器控件
此控件允许直接在Web页面上放置一个功能完善的日历。
示例,打印选定时间:
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CDemo.aspx.cs" Inherits="CDemo" %>
<!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:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
</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 CDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Response.Write("You selected :" + Calendar1.SelectedDate.ToShortDateString());
}
}
注,选择输出日期的格式:
转换属性 | 输出示例 |
ToFileTime | 将选中时间转换为本地操作系统的文件时间:128571156000000000。 |
ToFileTimeUtc | 将选中时间转换为操作系统的文件时间(按Utc时区转换):128571156000000000。 |
ToLocalTime | 将国际标准时间转换成本地时间。 |
ToLongDataString | 将选中时间转换成长格式的可读字符串Thursday,June05,2008。 |
ToLongTimeString | 将选中时间转换成长格式的时间值(不包括日期)12:00:00 AM。 |
ToOADate | 将选中时间转换成OLE自动日期. |
ToShortDateString | 将选中时间转换成短格式的可读字符串6/4/2008。 |
ToShortTimeString | 将选中时间转换成短格式的时间值(不包括日期)12:00 AM。 |
ToString | 将选中时间转换成6/4/2008/ 12:00:00 AM格式。 |
ToUniversalTime | 将选中时间转换成国际标准时间6/4/2008/ 12:00:00 AM. |
示例,选择日期、星期或月份
.ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CSDemo.aspx.cs" Inherits="CSDemo" %>
<!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:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged" SelectionMode="DayWeekMonth">
</asp:Calendar><p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label></p>
</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 CSDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
Label1.Text = "<b><u>You selected the following date/dates:</u></b>";
for (int i = 0; i < Calendar1.SelectedDates.Count; i++)
{
Label1.Text += Calendar1.SelectedDates[i].ToShortDateString() + "<br/>";
}
}
}
示例,禁止选择当前时间以后的内容:
if(e.Day.Date < DateTime.Now)
{
e.Day.IsSelectable = false;
}
13. AdRotator服务器控件
次控件支持在Web页面上利用Xml文件显示广告。
XML代码:
<Advertisements>
<Ad>
<ImageUrl>广告图片路径</ImageUrl>
<NavigateUrl>点击图像转向路径</NavigateUrl>
<AlternateText>没有图片时显示的提示文字</AlternateText>
<Keyword>设定图片类别,以指定是否过滤广告</Keyword>
<Impressions>设置该广告显示的百分率</Impressions>
</Ad>
</Advertisements>