ASP.Net 3.5学习笔记(C#)七

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#的一些介绍和特点。 ASP.NET 3.5是一种基于C#的Web应用程序开发框架,是微软公司的一款开放式源代码的Web应用程序开发技术,用于创建动态、跨平台和高性能的Web应用程序。 ASP.NET 3.5包含了许多新的功能和特点,比如:支持AJAX,这意味着Web应用程序可以实现部分页面刷新,使页面更加流畅;具有LINQ(语言集成查询)的支持,能够与数据库进行交互,方便开发人员对数据库中的数据进行处理;可以快速、简便地集成多种数据源。 ASP.NET 3.5也提供了一些较好的安全性保证,比如可以在网页级别和控件级别上进行角色和安全定义,这使得应用程序的数据连接和报表的处理更加安全,同时ASP.NET也提供了内置的加密和安全策略、访问控制以及安全审核等功能。 ASP.NET 3.5还提供了一些优秀的标准控件,使得页面设计灵活性更高,同时可以自定义控件、页面和应用程序的外观和行为。还有基于AJAX的AJAX控件,能够在客户端和服务器之间异步地传递数据,提高了交互式Web应用程序的响应时间和用户体验。 此外,ASP.NET 3.5还提供了丰富的设计模式和代码模板,以及对Master页和视图页支持,简化了Web编程,使得开发人员能够更快、更容易地创建和维护动态网站。 总体来说,ASP.NET 3.5是一款优秀的Web开发框架,提供了强大的功能和特点,可以使Web开发者更快速、更容易地进行开发和维护Web应用程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值