编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件

编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>Label服务器控件

描述:需要在服务器端程序代码中更改静态文本的内容或其他属性,使用Label控件。

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm001.aspx.cs" Inherits="CH5.CH5_DemoForm001" %>

<!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 id="Head1" runat="server">
    
<title>Label 服务器控件使用示范</title>
</head>
<body>
    
<form id="form1" runat="server" defaultbutton="btnOk" defaultfocus="TextBox1">
    
<target="_blank" href="http://www.cnblogs.com/liminzhang">
        
<img style="border: 0" alt="" src="Images/CH5_DemoForm001_Banner.jpg" title="前往新势讯科技之章立民研究室" /></a>
    
<div>
        请输入姓名:
        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        
<asp:Button ID="btnOk" runat="server" Text="确定" Font-Size="11pt" 
            onclick
="btnOk_Click"></asp:Button>
        
<hr />
        
<asp:Label ID="lblMessage" runat="server" ForeColor="#C00000"></asp:Label>
    
</div>
    
</form>
</body>
</html>

后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm001 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
protected void btnOk_Click(object sender, EventArgs e)
        {
            lblMessage.Text 
=
                
"嗨! " + Server.HtmlEncode(TextBox1.Text) +
                
" 您好。" + "<br/>" +
                
"今天是 " + DateTime.Now.ToString() + "<br/>" +
                
"祝大家身体健康!";
        }
    }
}


编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>Literal服务器控件

描述:可原封不动显示内容,或者HTML编码后的内容,或智能(判断客户端,支持标记则显示,不支持则删掉标记)

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm002.aspx.cs" Inherits="CH5.CH5_DemoForm002" %>

<!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 id="Head1" runat="server">
    
<title>将用户所提供的数据编码后赋给 Literal 服务器控件</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:Literal ID="Literal1" runat="server" Text="欢迎光临章立民研究室"></asp:Literal>
        
<hr />        
        
<asp:Panel ID="MyPanel1" runat="server" Width="750px">
        
</asp:Panel>
        
<hr />
        
<asp:Panel ID="MyPanel2" runat="server" Width="750px">
        
</asp:Panel>        
    
</div>
    
</form>
</body>
</html>
后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm002 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
// 建立一个 Literal 服务器控件。
            Literal MyLiteral1 = new Literal();

            MyLiteral1.Mode 
= LiteralMode.PassThrough;
            MyLiteral1.Text 
= "现在的日期时间是: <b><font color = 'red'>" + System.DateTime.Now + "</font></b>";

            
// 将 Literal 服务器控件动态加入 Panel 容器中。
            MyPanel1.Controls.Add(MyLiteral1);

            
// 建立一个 Literal 服务器控件。
            Literal MyLiteral2 = new Literal();

            
// 要求使用 HtmlEnocde 方法来编码 Text 属性的内容。
            MyLiteral2.Mode = LiteralMode.Encode;

            MyLiteral2.Text 
= "现在的日期时间是: <b><font color = 'red'>" + System.DateTime.Now + "</font></b>";

            
// 将 Literal 服务器控件动态加入 Panel 容器中。
            MyPanel2.Controls.Add(MyLiteral2);
        }
    }
}


编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>TextBox服务器控件 

描述:限制输入的字符数MaxLength属性,是否自动回发AutoPostBack属性,TextChanged事件,设置快捷键AccessKey属性。

下拉列表选择某一行位,相关数据显示在只读TextBox里。

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm003.aspx.cs" Inherits="CH5.CH5_DemoForm003" %>

<!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 id="Head1" runat="server">
    
<title>快速查询界面</title>
    
<style type="text/css">
        .style1
        
{
            width
: 108%;
        
}
        .style2
        
{
            width
: 116px;
            text-align
: right;
        
}
    
</style>
</head>
<body>
    
<form id="form1" runat="server" defaultfocus="DropDownList1">
    
<div>
        
<table style="border-style: outset; border-width: medium">
            
<tr>
                
<td colspan="3" style="text-align: center; border-bottom: thin outset; border-bottom-color: #800080">
                    
<target="_blank" href="http://www.cnblogs.com/liminzhang">
                        
<img style="border: 0" alt="" src="Images/CH5_DemoForm003_Banner.jpg" title="前往新势讯科技之章立民研究室" /></a>
                
</td>
            
</tr>
            
<tr>
                
<td style="vertical-align: top;">
                    请选择员工的身份证号码:
<br />
                    
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
                        DataTextField
="身份证号码" DataValueField="身份证号码" Height="23px" Width="233px">
                    
</asp:DropDownList>
                    
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
                        SelectCommand
="SELECT [身份证号码] FROM [飞狐工作室] WHERE [自传] IS NOT NULL"></asp:SqlDataSource>
                
</td>
                
<td rowspan="3">
                    
<asp:FormView ID="FormView1" runat="server" DataKeyNames="身份证号码" DataSourceID="SqlDataSource2"
                        Width
="612px">
                        
<EditItemTemplate>
                            身份证号码:
                            
<asp:Label ID="身份证号码Label1" runat="server" Text='<%# Eval("身份证号码") %>' />
                            
<br />
                            姓名:
                            
<asp:TextBox ID="姓名TextBox" runat="server" Text='<%# Bind("姓名") %>' />
                            
<br />
                            家庭住址:
                            
<asp:TextBox ID="家庭住址TextBox" runat="server" Text='<%# Bind("家庭住址") %>' />
                            
<br />
                            自传:
                            
<asp:TextBox ID="自传TextBox" runat="server" Text='<%# Bind("自传") %>' />
                            
<br />
                            
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                                Text
="更新" />
                            
&nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
                                CommandName
="Cancel" Text="取消" />
                        
</EditItemTemplate>
                        
<InsertItemTemplate>
                            身份证号码:
                            
<asp:TextBox ID="身份证号码TextBox" runat="server" Text='<%# Bind("身份证号码") %>' />
                            
<br />
                            姓名:
                            
<asp:TextBox ID="姓名TextBox" runat="server" Text='<%# Bind("姓名") %>' />
                            
<br />
                            家庭住址:
                            
<asp:TextBox ID="家庭住址TextBox" runat="server" Text='<%# Bind("家庭住址") %>' />
                            
<br />
                            自传:
                            
<asp:TextBox ID="自传TextBox" runat="server" Text='<%# Bind("自传") %>' />
                            
<br />
                            
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                Text
="插入" />
                            
&nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
                                CommandName
="Cancel" Text="取消" />
                        
</InsertItemTemplate>
                        
<ItemTemplate>
                            
<table class="style1">
                                
<tr>
                                    
<td class="style2">
                                        姓名:
</td>
                                    
<td>
                                        
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("姓名") %>' ReadOnly="True"></asp:TextBox>
                                    
</td>
                                
</tr>
                                
<tr>
                                    
<td class="style2">
                                        家庭住址:
</td>
                                    
<td>
                                        
<asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("家庭住址") %>' Width="449px"
                                            ReadOnly="True">
</asp:TextBox>
                                    
</td>
                                
</tr>
                                
<tr>
                                    
<td class="style2">
                                        自传:
</td>
                                    
<td>
                                        
<asp:TextBox ID="txtAutobiography" runat="server" Height="221px" Text='<%# Bind("自传") %>'
                                            TextMode="MultiLine" Width="448px" ReadOnly="True">
</asp:TextBox>
                                    
</td>
                                
</tr>
                            
</table>
                        
</ItemTemplate>
                    
</asp:FormView>
                    
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
                        SelectCommand
="SELECT [身份证号码], [姓名], [家庭住址], [自传] FROM [飞狐工作室] WHERE ([身份证号码] = @身份证号码)">
                        
<SelectParameters>
                            
<asp:ControlParameter ControlID="DropDownList1" Name="身份证号码" PropertyName="SelectedValue"
                                Type
="String" />
                        
</SelectParameters>
                    
</asp:SqlDataSource>
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
&nbsp;</td>
            
</tr>
            
<tr>
                
<td>
                    
&nbsp;</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>


在文本框中输入姓名,按Enter键执行查询操作。

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm004.aspx.cs" Inherits="CH5.CH5_DemoForm004" %>

<!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 id="Head1" runat="server">
    
<title>TextChanged 事件使用示范</title>
    
<style type="text/css">
        .style1
        
{
            width
: 73%;
        
}
        .style2
        
{
            border-bottom
: medium #800080 outset;
            border-top
: medium #800000 ridge;
        
}
        .style3
        
{
            color
: #FF3300;
            font-weight
: bold;
            text-decoration
: underline;
        
}
        .style4
        
{
            width
: 434px;
        
}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<table class="style1">
            
<tr>
                
<td colspan="2" style="text-align:center;">
                    
<target="_blank" href="http://www.cnblogs.com/liminzhang">
                        
<img style="border: 0" alt="" src="Images/CH5_DemoForm004_Banner.jpg" title="前往新势讯科技之章立民研究室" /></a>
                
</td>
            
</tr>
            
<tr>
                
<td class="style4">
                    请输入您所要查找之员工的姓名并按下 Enter 键:
</td>
                
<td>
                    
<asp:TextBox ID="txtName" runat="server" Width="174px" AccessKey="N" 
                        AutoPostBack
="True" ontextchanged="txtName_TextChanged"></asp:TextBox>
                
</td>
            
</tr>
            
<tr>
                
<td class="style4">
                    
<asp:Label ID="lblMessage" runat="server" Text="" style="color: #FF3300"></asp:Label>
                
</td>
                
<td>
                    (ALT +
<span class="style3"> N</span></td>
            
</tr>
            
<tr>
                
<td class="style2" colspan="2">
                    
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" 
                        onprerender
="ListView1_PreRender">
                        
<AlternatingItemTemplate>
                            
<tr style="background-color: #FAFAD2;color: #284775;">
                                
<td>
                                    
<asp:Label ID="姓名Label" runat="server" Text='<%# Eval("姓名") %>' />
                                
</td>
                                
<td>
                                    
<asp:Label ID="地址Label" runat="server" Text='<%# Eval("地址") %>' />
                                
</td>
                                
<td>
                                    
<asp:Label ID="当前薪资Label" runat="server" Text='<%# Eval("当前薪资") %>' />
                                
</td>
                            
</tr>
                        
</AlternatingItemTemplate>
                        
<LayoutTemplate>
                            
<table id="Table1" runat="server">
                                
<tr id="Tr1" runat="server">
                                    
<td id="Td1" runat="server">
                                        
<table ID="itemPlaceholderContainer" runat="server" border="1" 
                                            style
="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                                            
<tr id="Tr2" runat="server" style="background-color: #FFFBD6;color: #333333;">
                                                
<th id="Th1" runat="server">
                                                    姓名
</th>
                                                
<th id="Th2" runat="server">
                                                    地址
</th>
                                                
<th id="Th3" runat="server">
                                                    当前薪资
</th>
                                            
</tr>
                                            
<tr ID="itemPlaceholder" runat="server">
                                            
</tr>
                                        
</table>
                                    
</td>
                                
</tr>
                                
<tr id="Tr3" runat="server">
                                    
<td id="Td2" runat="server" 
                                        style
="text-align: center;background-color: #FFCC66;font-family: Verdana, Arial, Helvetica, sans-serif;color: #333333;">
                                    
</td>
                                
</tr>
                            
</table>
                        
</LayoutTemplate>
                        
<InsertItemTemplate>
                            
<tr style="">
                                
<td>
                                    
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
                                    
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
                                
</td>
                                
<td>
                                    
<asp:TextBox ID="姓名TextBox" runat="server" Text='<%# Bind("姓名") %>' />
                                
</td>
                                
<td>
                                    
<asp:TextBox ID="地址TextBox" runat="server" Text='<%# Bind("地址") %>' />
                                
</td>
                                
<td>
                                    
<asp:TextBox ID="当前薪资TextBox" runat="server" Text='<%# Bind("当前薪资") %>' />
                                
</td>
                            
</tr>
                        
</InsertItemTemplate>
                        
<SelectedItemTemplate>
                            
<tr style="background-color: #FFCC66;font-weight: bold;color: #000080;">
                                
<td>
                                    
<asp:Label ID="姓名Label" runat="server" Text='<%# Eval("姓名") %>' />
                                
</td>
                                
<td>
                                    
<asp:Label ID="地址Label" runat="server" Text='<%# Eval("地址") %>' />
                                
</td>
                                
<td>
                                    
<asp:Label ID="当前薪资Label" runat="server" Text='<%# Eval("当前薪资") %>' />
                                
</td>
                            
</tr>
                        
</SelectedItemTemplate>
                        
<EmptyDataTemplate>
                            
<table id="Table2" runat="server" 
                                style
="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
                                
<tr>
                                    
<td>
                                        未返回数据。
</td>
                                
</tr>
                            
</table>
                        
</EmptyDataTemplate>
                        
<EditItemTemplate>
                            
<tr style="background-color: #FFCC66;color: #000080;">
                                
<td>
                                    
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                                    
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
                                
</td>
                                
<td>
                                    
<asp:TextBox ID="姓名TextBox" runat="server" Text='<%# Bind("姓名") %>' />
                                
</td>
                                
<td>
                                    
<asp:TextBox ID="地址TextBox" runat="server" Text='<%# Bind("地址") %>' />
                                
</td>
                                
<td>
                                    
<asp:TextBox ID="当前薪资TextBox" runat="server" Text='<%# Bind("当前薪资") %>' />
                                
</td>
                            
</tr>
                        
</EditItemTemplate>
                        
<ItemTemplate>
                            
<tr style="background-color: #FFFBD6;color: #333333;">
                                
<td>
                                    
<asp:Label ID="姓名Label" runat="server" Text='<%# Eval("姓名") %>' />
                                
</td>
                                
<td>
                                    
<asp:Label ID="地址Label" runat="server" Text='<%# Eval("地址") %>' />
                                
</td>
                                
<td>
                                    
<asp:Label ID="当前薪资Label" runat="server" Text='<%# Eval("当前薪资") %>' />
                                
</td>
                            
</tr>
                        
</ItemTemplate>
                    
</asp:ListView>
                    
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
                        SelectCommand
="SELECT [姓名], [地址], [当前薪资] FROM [章立民研究室] WHERE ([姓名] = @姓名)">
                        
<SelectParameters>
                            
<asp:Parameter Name="姓名" Type="String" />
                        
</SelectParameters>
                    
</asp:SqlDataSource>
                
</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>

后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm004 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
protected void txtName_TextChanged(object sender, EventArgs e)
        {
            SqlDataSource1.SelectParameters[
"姓名"].DefaultValue = txtName.Text;
        }

        
protected void ListView1_PreRender(object sender, EventArgs e)
        {
            
if (ListView1.Items.Count == 0)
            {
                lblMessage.Text 
= "您所查找的姓名不存在...";
            }
            
else
            {
                lblMessage.Text 
= "找到了...";
            }
        }
    }
}


 

编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>CheckBox服务器控件

描述:点击"是否显示自传数据" ,自动提交表单。移到"是否显示自传数据",自动变背景色。移到"性别CheckBox",自动变背景色。"性别CheckBox"绑定数据库bit字段。

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm005.aspx.cs" Inherits="CH5.CH5_DemoForm005" %>

<!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 id="Head1" runat="server">
    
<title>快速查看界面 - 示范使用 CheckBox 服务器控件</title>
    
<style type="text/css">
        .style2
        
{
            text-align
: right;
        
}
    
</style>
</head>
<body>
    
<form id="form1" runat="server" defaultfocus="DropDownList1">
    
<div>
        
<table style="border-style: outset; border-width: medium">
            
<tr>
                
<td colspan="3" style="text-align: center; border-bottom: thin outset; border-bottom-color: #800080;">
                    
<target="_blank" href="http://www.cnblogs.com/liminzhang">
                        
<img style="border: 0" alt="" src="Images/CH5_DemoForm005_Banner.jpg" title="前往新势讯科技之章立民研究室" /></a>
                
</td>
            
</tr>
            
<tr>
                
<td style="vertical-align: top;">
                    请选择员工的身份证号码:
<br />
                    
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1"
                        DataTextField
="身份证号码" DataValueField="身份证号码" Height="23px" Width="233px">
                    
</asp:DropDownList>
                    
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
                        SelectCommand
="SELECT [身份证号码] FROM [飞狐工作室] WHERE [自传] IS NOT NULL"></asp:SqlDataSource>
                    
<br />
                    
<br />
                    
<asp:CheckBox ID="ShowContentOrNot" runat="server" AutoPostBack="True" Text="是否显示自传数据"
                        Checked
="True" oncheckedchanged="ShowContentOrNot_CheckedChanged" />
                    
<br />
                
</td>
                
<td rowspan="3">
                    
<asp:FormView ID="FormView1" runat="server" DataKeyNames="身份证号码" DataSourceID="SqlDataSource2"
                        Width
="612px" onitemcreated="FormView1_ItemCreated">
                        
<EditItemTemplate>
                            身份证号码:
                            
<asp:Label ID="身份证号码Label1" runat="server" Text='<%# Eval("身份证号码") %>' />
                            
<br />
                            姓名:
                            
<asp:TextBox ID="姓名TextBox" runat="server" Text='<%# Bind("姓名") %>' />
                            
<br />
                            家庭住址:
                            
<asp:TextBox ID="家庭住址TextBox" runat="server" Text='<%# Bind("家庭住址") %>' />
                            
<br />
                            自传:
                            
<asp:TextBox ID="自传TextBox" runat="server" Text='<%# Bind("自传") %>' />
                            
<br />
                            
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                                Text
="更新" />
                            
&nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False"
                                CommandName
="Cancel" Text="取消" />
                        
</EditItemTemplate>
                        
<InsertItemTemplate>
                            身份证号码:
                            
<asp:TextBox ID="身份证号码TextBox" runat="server" Text='<%# Bind("身份证号码") %>' />
                            
<br />
                            姓名:
                            
<asp:TextBox ID="姓名TextBox" runat="server" Text='<%# Bind("姓名") %>' />
                            
<br />
                            家庭住址:
                            
<asp:TextBox ID="家庭住址TextBox" runat="server" Text='<%# Bind("家庭住址") %>' />
                            
<br />
                            自传:
                            
<asp:TextBox ID="自传TextBox" runat="server" Text='<%# Bind("自传") %>' />
                            
<br />
                            
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                Text
="插入" />
                            
&nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False"
                                CommandName
="Cancel" Text="取消" />
                        
</InsertItemTemplate>
                        
<ItemTemplate>
                            
<table class="style1">
                                
<tr>
                                    
<td class="style2">
                                        姓名:
</td>
                                    
<td>
                                        
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("姓名") %>' ReadOnly="True"></asp:TextBox>
                                    
</td>
                                
</tr>
                                
<tr>
                                    
<td class="style2">
                                        性别:
</td>
                                    
<td>
                                        
<asp:CheckBox ID="chkGender" runat="server" Checked='<%# Bind("性别") %>' />
                                    
</td>
                                
</tr>
                                
<tr>
                                    
<td class="style2">
                                        家庭住址:
</td>
                                    
<td>
                                        
<asp:TextBox ID="txtAddress" runat="server" Text='<%# Bind("家庭住址") %>' Width="449px"
                                            ReadOnly="True">
</asp:TextBox>
                                    
</td>
                                
</tr>
                                
<tr>
                                    
<td class="style2">
                                        自传:
</td>
                                    
<td>
                                        
<asp:TextBox ID="txtAutobiography" runat="server" Height="221px" Text='<%# Bind("自传") %>'
                                            TextMode="MultiLine" Width="448px" ReadOnly="True">
</asp:TextBox>
                                    
</td>
                                
</tr>
                            
</table>
                        
</ItemTemplate>
                    
</asp:FormView>
                    
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:chtNorthwind %>"
                        SelectCommand
="SELECT [身份证号码], [姓名], [性别], [家庭住址], [自传] FROM [飞狐工作室] WHERE ([身份证号码] = @身份证号码)">
                        
<SelectParameters>
                            
<asp:ControlParameter ControlID="DropDownList1" Name="身份证号码" PropertyName="SelectedValue"
                                Type
="String" />
                        
</SelectParameters>
                    
</asp:SqlDataSource>
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
&nbsp;</td>
            
</tr>
            
<tr>
                
<td>
                    
&nbsp;</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>
后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm005 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
// 设定当鼠标指针移入复选框 □ 是否显示自传数据 时,方块的背景色。
            ShowContentOrNot.InputAttributes.Add("onmouseover""this.style.backgroundColor = 'red'");

            
// 设定当鼠标指针移出复选框 □ 是否显示自传数据 时,方块的背景色。
            ShowContentOrNot.InputAttributes.Add("onmouseout""this.style.backgroundColor = 'white'");

            
// 设定当鼠标指针移入复选框 □ 是否显示自传数据 时,文字的颜色与背景色。
            ShowContentOrNot.LabelAttributes.Add("onmouseover""this.style.color = 'red';this.style.backgroundColor = 'yellow'");

            
// 设定当鼠标指针移出复选框 □ 是否显示自传数据 时,文字的颜色与背景色。
            ShowContentOrNot.LabelAttributes.Add("onmouseout""this.style.color = 'black';this.style.backgroundColor = 'white'");

        }

        
protected void ShowContentOrNot_CheckedChanged(object sender, EventArgs e)
        {
            HideOrShowAutobiography();
        }

        
protected void FormView1_ItemCreated(object sender, EventArgs e)
        {
            
// 找到 FormView 服务器控件当中用以显示性别的 CheckBox 服务器控件。
            CheckBox chkGender = (CheckBox)(FormView1.Row.FindControl("chkGender"));

            
// 设定当鼠标指针移入显示性别的 CheckBox 服务器控件时,方块的背景色。
            chkGender.InputAttributes.Add("onmouseover""this.style.backgroundColor = 'blue'");

            
// 设定当鼠标指针移出显示性别的 CheckBox 服务器控件时,方块的背景色。
            chkGender.InputAttributes.Add("onmouseout""this.style.backgroundColor = 'white'");

            HideOrShowAutobiography();
        }

        
protected void HideOrShowAutobiography()
        {
            
// 找到 FormView 服务器控件当中用以显示自传数据的 TextBox 服务器控件。
            TextBox txtAutobiography = (TextBox)(FormView1.FindControl("txtAutobiography"));

            
if (ShowContentOrNot.Checked)
            {
                
// 将显示自传数据的 TextBox 服务器控件显示出来。
                txtAutobiography.Visible = true;
            }
            
else
            {
                
// 将显示自传数据的 TextBox 服务器控件隐藏起来。
                txtAutobiography.Visible = false;
            }
        }
    }
}


编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>RadioButton服务器控件

描述:单选。获取单选值。

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm006.aspx.cs" Inherits="CH5.CH5_DemoForm006" %>

<!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 id="Head1" runat="server">
    
<title>RadioButton 服务器控件使用示范</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        请选取您所要订购的期限:
        
<br /><br />
        
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="SubScribe" Text="投资专家三个月">
        
</asp:RadioButton>
        
<br />        
        
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="SubScribe" Text="投资专家半年">
        
</asp:RadioButton>
        
<br />        
        
<asp:RadioButton ID="RadioButton3" runat="server" GroupName="SubScribe" Text="投资专家一年">
        
</asp:RadioButton>
        
<br />        
        
<asp:RadioButton ID="RadioButton4" runat="server" GroupName="SubScribe" Text="投资专家两年+黄金组合">
        
</asp:RadioButton>
        
<br /><br />
        请选择您个人的投资属性:
        
<br /><br />
        
<asp:RadioButton ID="RadioButton5" runat="server" GroupName="Personal" Text="保守型">
        
</asp:RadioButton>
        
<br />        
        
<asp:RadioButton ID="RadioButton6" runat="server" GroupName="Personal" Text="积极型">
        
</asp:RadioButton>
        
<br />        
        
<asp:RadioButton ID="RadioButton7" runat="server" GroupName="Personal" Text="稳健型">
        
</asp:RadioButton>        
        
<br /><br />
        
<asp:Button ID="btnOk" runat="server" Text="送出" onclick="btnOk_Click"></asp:Button>
        
<br /><br />
        
<asp:Label ID="Message" runat="server" ForeColor="Red"></asp:Label>
    
</div>
    
</form>
</body>
</html>

后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm006 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
protected void btnOk_Click(object sender, EventArgs e)
        {
            
if (RadioButton1.Checked)
            {
                Message.Text 
= "您选择的是<b>" + RadioButton1.Text + "</b>";
            }
            
else if (RadioButton2.Checked)
            {
                Message.Text 
= "您选择的是<b>" + RadioButton2.Text + "</b>";
            }
            
else if (RadioButton3.Checked)
            {
                Message.Text 
= "您选择的是<b>" + RadioButton3.Text + "</b>";
            }
            
else if (RadioButton4.Checked)
            {
                Message.Text 
= "您选择的是<b>" + RadioButton4.Text + "</b>";
            }

            
if (RadioButton5.Checked)
            {
                Message.Text 
+= "。您的投资属性是<b>" + RadioButton5.Text + "</b>。";
            }
            
else if (RadioButton6.Checked)
            {
                Message.Text 
+= "。您的投资属性是<b>" + RadioButton6.Text + "</b>。";
            }
            
else if (RadioButton7.Checked)
            {
                Message.Text 
+= "。您的投资属性是<b>" + RadioButton7.Text + "</b>。";
            }
        }
    }
}


编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>Button服务器控件 

使用Button服务器控件来触发查询操作
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm007.aspx.cs" Inherits="CH5.CH5_DemoForm007" %>

<!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 id="Head1" runat="server">
    
<title>Button 控件使用示范</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        请输入您所要查找之员工的姓名:
        
<asp:Label ID="Label1" runat="server" Font-Bold="True">(ALT+<u>Z</u></asp:Label>
        
<asp:TextBox ID="txtName" runat="server" MaxLength="10" Width="124px" AccessKey="Z"></asp:TextBox>
        
<br />
        
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Text="不可以空白"
            ControlToValidate
="txtName" Display="Dynamic">务必输入姓名,不可以空白。</asp:RequiredFieldValidator>
        
<hr />
        
<asp:Button ID="btnSearch" runat="server" Text="开始查找" ToolTip="开始查找您所输入之姓名的员工数据"
            AccessKey
="S"></asp:Button>
        
<asp:Label ID="Label2" runat="server">(ALT+<u>S</u></asp:Label>
        
<hr />
        
<asp:GridView ID="LimingchStudio_GridView" runat="server" 
            AutoGenerateColumns
="False" BackColor="White" BorderColor="#E7E7FF" 
            BorderStyle
="None" BorderWidth="1px" CellPadding="3" DataKeyNames="员工号码" 
            DataSourceID
="LimingchStudio_SqlDataSource" GridLines="Horizontal" 
            EmptyDataText
="找不到您所指定姓名的员工数据...">
            
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
            
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
            
<Columns>
                
<asp:BoundField DataField="员工号码" HeaderText="员工号码" InsertVisible="False" 
                    ReadOnly
="True" SortExpression="员工号码" />
                
<asp:BoundField DataField="身份证号码" HeaderText="身份证号码" SortExpression="身份证号码" />
                
<asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" />
                
<asp:BoundField DataField="性别" HeaderText="性别" SortExpression="性别" />
                
<asp:BoundField DataField="地址" HeaderText="地址" SortExpression="地址" />
                
<asp:BoundField DataField="部门" HeaderText="部门" SortExpression="部门" />
            
</Columns>
            
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
            
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
            
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
            
<AlternatingRowStyle BackColor="#F7F7F7" />
        
</asp:GridView>
        
        
<asp:SqlDataSource ID="LimingchStudio_SqlDataSource" runat="server" 
            ConnectionString
="<%$ ConnectionStrings:chtNorthwind %>" 
            SelectCommand
="SELECT [员工号码], [身份证号码], [姓名], [性别], [地址], [部门] FROM [章立民研究室] WHERE ([姓名] = @姓名)">
            
<SelectParameters>
                
<asp:ControlParameter ControlID="txtName" Name="姓名" PropertyName="Text" 
                    Type
="String" />
            
</SelectParameters>
        
</asp:SqlDataSource>
        
    
</div>
    
</form>
</body>
</html>


示范怎样使用命令按钮

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm008.aspx.cs" Inherits="CH5.CH5_DemoForm008" %>

<!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 id="Head1" runat="server">
    
<title>命令按钮使用示范</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<table>
            
<tr>
                
<td>
                    
&nbsp;
                
</td>
                
<td>
                    
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="TextBox1"
                        Display
="Dynamic" ErrorMessage="不可以空白">不可以空白</asp:RequiredFieldValidator>
                    
<asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="TextBox1"
                        Display
="Dynamic" Operator="DataTypeCheck" Text="务必输入整数" Type="Integer"></asp:CompareValidator>
                
</td>
                
<td>
                    
<asp:Button ID="btnAdd" runat="server" CommandName="相加" Text="相加" />
                
</td>
            
</tr>
            
<tr>
                
<td>
                    请输入数字:
                
</td>
                
<td>
                    
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                
</td>
                
<td>
                    
<asp:Button ID="btnSubtract" runat="server" CommandName="相减" Text="相减" />
                
</td>
            
</tr>
            
<tr>
                
<td>
                    请输入数字:
                
</td>
                
<td>
                    
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                
</td>
                
<td>
                    
<asp:Button ID="btnMultiply" runat="server" CommandName="相乘" Height="27px" Text="相乘" />
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
&nbsp;
                
</td>
                
<td>
                    
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox2"
                        Display
="Dynamic" ErrorMessage="务必输入整数" Operator="DataTypeCheck" Type="Integer">务必输入整数</asp:CompareValidator>
                    
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="TextBox2"
                        Display
="Dynamic" ErrorMessage="不可以空白">不可以空白</asp:RequiredFieldValidator>
                
</td>
                
<td>
                    
<asp:Button ID="btnDivide" runat="server" CommandName="相除" Text="相除" />
                
</td>
            
</tr>
            
<tr>
                
<td colspan="3">
                    
<asp:Label ID="CaculateResult" runat="server" Font-Bold="True" ForeColor="#C00000"></asp:Label>
                
</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>

后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm008 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            btnAdd.Click 
+= new System.EventHandler(this.Calculate);
            btnSubtract.Click 
+= new System.EventHandler(this.Calculate);
            btnMultiply.Click 
+= new System.EventHandler(this.Calculate);
            btnDivide.Click 
+= new System.EventHandler(this.Calculate);
        }

        
protected void Calculate(Object sender, System.EventArgs e)
        {
            
int op1 = Convert.ToInt32(TextBox1.Text);
            
int op2 = Convert.ToInt32(TextBox2.Text);
            
double result = 0;

            
switch (((Button)(sender)).CommandName)
            {
                
case "相加":
                    result 
= op1 + op2;
                    
break;

                
case "相减":
                    result 
= op1 - op2;
                    
break;

                
case "相乘":
                    result 
= op1 * op2;
                    
break;

                
case "相除":
                    
if (op2 > 0)
                    {
                        result 
= op1 / op2;
                    }
                    
else
                    {
                        result 
= 0;
                    }
                    
break;

            }

            CaculateResult.Text 
= "运算结果是: " + result.ToString();
        }

    }
}


示范替按钮加上鼠标移入与移出的变化效果
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm010.aspx.cs" Inherits="CH5.CH5_DemoForm010" %>

<!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 id="Head1" runat="server">
    
<title>示范替按钮加上鼠标移入与移出的变化效果</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:Button ID="Button1" runat="server" Text="请按我" Font-Size="12pt"
          onmouseover
="this.style.height = '80px';this.style.color = 'red';this.style.backgroundColor='yellow';this.style.fontWeight='bold'"
          
            onmouseout
="this.style.height = '';this.style.color = 'black';this.style.backgroundColor='buttonface';this.style.fontWeight='normal'" 
            onclick
="Button1_Click">
        
</asp:Button>
        
<br />
        
<asp:Label ID="Message" runat="server"></asp:Label>
    
</div>
    
</form>
</body>
</html>


 

编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>ImageButton服务器控件 

ImageButton 控件坐标检测示范
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm011.aspx.cs" Inherits="CH5.CH5_DemoForm011" %>

<!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 id="Head1" runat="server">
    
<title>ImageButton 控件坐标检测示范</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<table>
            
<tr>
                
<td style="padding: 5px 5px 5px 5px;">
                    
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="Images/ButtonSet1.jpg"
                        BorderStyle
="Ridge" onclick="ImageButton1_Click"></asp:ImageButton>
                
</td>
                
<td style="padding: 5px 5px 5px 5px;">
                    
<asp:Label ID="Label1" runat="server">X 坐标:</asp:Label>
                    
<br />
                    
<asp:Label ID="Label2" runat="server">Y 坐标:</asp:Label>
                
</td>
                
<td style="padding: 5px 5px 5px 5px;">
                    
<asp:Label ID="Message" runat="server" ForeColor="Red" Font-Bold="True"></asp:Label>
                
</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>

后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm011 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            
int x = e.X;
            
int y = e.Y;

            Label1.Text 
= "X 坐标:" + x.ToString();
            Label2.Text 
= "Y 坐标:" + y.ToString();

            
if (y >= 0 && y <= 56)
            {
                Message.Text 
= "您已按下【实时新闻】按钮";
            }
            
else if (y >= 57 && y <= 112)
            {
                Message.Text 
= "您已按下【法人动态】按钮";
            }
            
else if (y >= 113 && y <= 168)
            {
                Message.Text 
= "您已按下【智能选股】按钮";
            }
            
else if (y > 168)
            {
                Message.Text 
= "您已按下【技术分析】按钮";
            }
        }
    }
}



鼠标移到 ImageButton 上方时的样式变换
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm012.aspx.cs" Inherits="CH5.CH5_DemoForm012" %>

<!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 id="Head1" runat="server">
    
<title>鼠标移到 ImageButton 上方时的样式变换</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="Images/ButtonSet21.jpg"
            BorderStyle
="Outset" ToolTip="欢迎大家加入本公司会员"
            onmouseover
="this.src='Images/ButtonSet22.jpg';this.style.border = 'Inset';"
            onmouseout
="this.src='Images/ButtonSet21.jpg';this.style.border = 'Outset'">
        
</asp:ImageButton>
    
</div>
    
</form>
</body>
</html>


 

编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>HyperLink服务器控件

示范动态创建 HyperLink 控件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm013.aspx.cs" Inherits="CH5.CH5_DemoForm013" %>

<!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 id="Head1" runat="server">
    
<title>示范动态创建 HyperLink 控件</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<h3>本应用程序项目的 ASP.NET 范例网页清单</h3>
        
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    
</div>
    
</form>
</body>
</html>
后台
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;
using System.IO;
using Microsoft.VisualBasic.Devices;

namespace CH5
{
    
public partial class CH5_DemoForm013 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

            Computer MyComputer 
= new Computer();

            
// 取得实体目录。
            string strPhysicalDir = MyComputer.FileSystem.GetParentPath(Server.MapPath("/CH5"));

            
// 取得档案列表。
            FileInfo[] DemoWebForms =
                MyComputer.FileSystem.GetDirectoryInfo(strPhysicalDir).GetFiles(
"CH5_Demo*.aspx");


            
// 这是 LINQ 的写法。
            var DemoWebFormFiles = from DemoWebFormFile in DemoWebForms select DemoWebFormFile.Name;

            
foreach (var myDemoWebForm in DemoWebFormFiles)
            {
                
// 动态建立一个 HyperLink 对象。
                HyperLink myHyperLink = new HyperLink();

                
// 设定 HyperLink 对象的属性。                
                myHyperLink.Text = myDemoWebForm;
                myHyperLink.NavigateUrl 
= myDemoWebForm;
                myHyperLink.Target 
= "_blank";

                
this.PlaceHolder1.Controls.Add(myHyperLink);

                Literal myLiteral 
= new Literal();
                myLiteral.Text 
= "<Br/>";

                
this.PlaceHolder1.Controls.Add(myLiteral);
            }
        }
    }
}


 

编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>Image服务器控件 

描述:遍历目录中的图片,赋值给超链接控件,设置显示样式,用查询字符串来传递给第二张网页。于第二张网页中用Image控件显示。

第一张网页

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm014.aspx.cs" Inherits="CH5.CH5_DemoForm014" %>

<!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 id="Head1" runat="server">
    
<title>简单相册</title>
    
<style type="text/css">
        #form1
        
{
            text-align
: center;
        
}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<h3>
            请点击您所要查询的照片
</h3>
        
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    
</div>
    
</form>
</body>
</html>

后台
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;
using System.IO;
using Microsoft.VisualBasic.Devices;

namespace CH5
{
    
public partial class CH5_DemoForm014 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            Computer MyComputer 
= new Computer();

            
// 取得实体目录。
            string strPhysicalDir = MyComputer.FileSystem.GetParentPath(Server.MapPath("/CH5"));

            
// 取得档案列表。
            FileInfo[] DuckPhotos =
                MyComputer.FileSystem.GetDirectoryInfo(strPhysicalDir 
+ "\\Photo").GetFiles("Thumbnail-Duck*.jpg");

            
// 这是 LINQ 的写法。
            var DuckPhotoFiles = from DuckPhotoFile in DuckPhotos select DuckPhotoFile.Name;

            
int counter = 0;

            
foreach (var myDuckFile in DuckPhotoFiles)
            {

                
// 动态建立一个 HyperLink 对象。
                HyperLink myHyperLink = new HyperLink();

                
// 设定 HyperLink 对象的属性。
                myHyperLink.ImageUrl = "~/Photo/" + myDuckFile;
                myHyperLink.NavigateUrl 
= "DemoForm014_Target.aspx?ThumbnailPhotoFileName=" + myDuckFile;
                myHyperLink.Target 
= "_blank";
                myHyperLink.BorderStyle 
= BorderStyle.Outset;


                
this.PlaceHolder1.Controls.Add(myHyperLink);

                counter 
+= 1;

                
// 让每一列只显示四张缩图。
                if (counter % 4 == 0)
                {
                    Literal myLiteral 
= new Literal();
                    myLiteral.Text 
= "<Br/>";

                    
this.PlaceHolder1.Controls.Add(myLiteral);
                }
            }
        }
    }
}

第二张网页

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoForm014_Target.aspx.cs" Inherits="CH5.DemoForm014_Target" %>

<!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 id="Head1" runat="server">
    
<title>照片全图</title>
    
<style type="text/css">
        #form1
        
{
            text-align
: center;
        
}
    
</style>    
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:Image ID="Image1" runat="server" />
    
</div>
    
</form>
</body>
</html>
后台
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;

namespace CH5
{
    
public partial class DemoForm014_Target : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
// 取得查询字符串之 ThumbnailPhotoFileName 字段的值,也就是照片缩图文件名称。
            string ThumbnailPhotoFileName = Request.QueryString["ThumbnailPhotoFileName"];
            
// 根据照片缩图文件名称来转换出照片全图文件名称。
            string FullSizePhotoFilePath = ThumbnailPhotoFileName.Substring("Thumbnail-".Length);

            
// 设定 Image 服务器控件的相关属性以便显示出照片全图。
            Image1.ImageUrl = "~/Photo/" + FullSizePhotoFilePath;
            Image1.AlternateText 
= FullSizePhotoFilePath;
            Image1.BorderStyle 
= BorderStyle.Ridge;
            Image1.BorderWidth 
= new Unit("10px");
        }
    }
}


 

编程实现>ASP.NET 3.5开发范例精讲精析>探讨基础的ASP.NET服务器控件>Calendar服务器控件 

制作一个简单的日历界面。描述:用户选择某个日期,便返回那个日期。

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm015.aspx.cs" Inherits="CH5.CH5_DemoForm015" %>

<!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 id="Head1" runat="server">
    
<title>Calendar 控件使用示范</title>
    
<style type="text/css">
        #form1
        {
            text
-align: center;
        }
    
</style>    
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<h3>日历控件</h3>
        
<asp:Calendar ID="MyCalendar" runat="server" 
            onselectionchanged
="MyCalendar_SelectionChanged"></asp:Calendar>        
        
<asp:Label ID="Message" runat="server" ForeColor="Red"></asp:Label>
    
</div>
    
</form>
</body>
</html>
后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm015 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
protected void MyCalendar_SelectionChanged(object sender, EventArgs e)
        {
            Message.Text 
= "用户选择的日期是: " + MyCalendar.SelectedDate.ToLongDateString();
        }
    }
}


描述:不论用户选择某单一日期,某周,某月,都可通过下列程序代码取得用户所选择的日期和数目。

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm016.aspx.cs" Inherits="CH5.CH5_DemoForm016" %>

<!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 id="Head1" runat="server">
    
<title>日期选择示范</title>
    
<style type="text/css">
        #form1
        
{
            text-align
: center;
        
}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<table class="style1">
            
<tr>
                
<td style="vertical-align:top;">
                    
<asp:Calendar ID="MyCalendar" runat="server" PrevMonthText='<IMG src="Images/R_FIRST.GIF">'
                        NextMonthText='
<IMG src="Images/R_LAST.GIF">' SelectionMode="DayWeekMonth" SelectWeekText='<IMG src="Images/ADD.JPG">'
                        SelectMonthText='
<IMG src="Images/Arrow1.jpg">' BorderColor="Navy" BorderWidth="2px"
                        Width="346px" Height="322px" 
                        onselectionchanged="MyCalendar_SelectionChanged">
                        
<TodayDayStyle Font-Bold="True" BorderWidth="3px" ForeColor="Red" BorderStyle="Ridge"
                            BorderColor
="#E0E0E0" BackColor="Yellow"></TodayDayStyle>
                        
<SelectorStyle BackColor="#FFC0C0"></SelectorStyle>
                        
<DayHeaderStyle Font-Size="12pt" Font-Names="标楷体" BackColor="#FFFFC0"></DayHeaderStyle>
                        
<SelectedDayStyle ForeColor="Blue" BackColor="#80FF80"></SelectedDayStyle>
                        
<TitleStyle Font-Bold="True" BorderWidth="2px" ForeColor="#400040" BorderColor="Navy"
                            BackColor
="#C0FFC0"></TitleStyle>
                        
<WeekendDayStyle Font-Italic="True" Font-Bold="True" ForeColor="Red"></WeekendDayStyle>
                        
<OtherMonthDayStyle BackColor="#C0FFFF"></OtherMonthDayStyle>
                    
</asp:Calendar>
                    
<br />
                    
<asp:Button ID="btnClear" runat="server" Text="不选择任何日期" Font-Size="12pt" 
                        onclick
="btnClear_Click"></asp:Button>
                
</td>
                
<td style="text-align:left;">
                    
<asp:Label ID="Message" runat="server"></asp:Label>
                
</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>

后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm016 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {

        }

        
protected void MyCalendar_SelectionChanged(object sender, EventArgs e)
        {
            CountSelection();
        }

        
protected void btnClear_Click(object sender, EventArgs e)
        {
            MyCalendar.SelectedDates.Clear();
            CountSelection();
        }

        
private void CountSelection()
        {
            
int i;
            
int DateNumber = MyCalendar.SelectedDates.Count;
            Message.Text 
= "用户共选择下列 " + DateNumber.ToString() + " 个日期:" + "<br>";
            
for (i = 0; i <= DateNumber - 1; i++)
            {
                Message.Text 
+= MyCalendar.SelectedDates[i].ToShortDateString() + "<br>";
            }
        }

    }
}


描述:下拉框选择星期一,就把该月份星期一的所有日子列出来。

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm017.aspx.cs" Inherits="CH5.CH5_DemoForm017" %>

<!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 id="Head1" runat="server">
    
<title>示范以程序控制方式选择多个日期</title>
    
<style type="text/css">
        #form1
        
{
            text-align
: center;
        
}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<table>
            
<tr>
                
<td colspan="2">
                    
<asp:Calendar ID="MyCalendar" runat="server" Width="710px" PrevMonthText=" " NextMonthText=" "
                        SelectionMode
="DayWeekMonth" SelectWeekText="选一整周" SelectMonthText="选整个月" BorderColor="Navy"
                        BorderWidth
="2px" Height="322px" NextPrevFormat="ShortMonth" 
                        ShowGridLines
="True" onselectionchanged="MyCalendar_SelectionChanged">
                        
<TodayDayStyle Font-Bold="True" BorderWidth="3px" ForeColor="Red" BorderStyle="Ridge"
                            BorderColor
="#E0E0E0" BackColor="Yellow"></TodayDayStyle>
                        
<SelectorStyle BackColor="#FFC0C0"></SelectorStyle>
                        
<DayHeaderStyle Font-Size="12pt" Font-Names="标楷体" BackColor="#FFFFC0"></DayHeaderStyle>
                        
<SelectedDayStyle ForeColor="Blue" BackColor="#80FF80"></SelectedDayStyle>
                        
<TitleStyle Font-Bold="True" BorderWidth="2px" ForeColor="#400040" BorderColor="Navy"
                            BackColor
="#C0FFC0"></TitleStyle>
                        
<WeekendDayStyle Font-Italic="True" Font-Bold="True" ForeColor="Red"></WeekendDayStyle>
                        
<OtherMonthDayStyle BackColor="#C0FFFF"></OtherMonthDayStyle>
                    
</asp:Calendar>
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
<asp:Label ID="Label1" runat="server" Width="260px">请问您要选择本月的星期几:</asp:Label>
                    
<asp:DropDownList ID="WeekDayDropList" runat="server" Width="130px" Font-Size="12pt"
                        AutoPostBack
="True" 
                        onselectedindexchanged
="WeekDayDropList_SelectedIndexChanged">
                        
<asp:ListItem Value="-1">【请选择】</asp:ListItem>
                        
<asp:ListItem Value="1">星期一</asp:ListItem>
                        
<asp:ListItem Value="2">星期二</asp:ListItem>
                        
<asp:ListItem Value="3">星期三</asp:ListItem>
                        
<asp:ListItem Value="4">星期四</asp:ListItem>
                        
<asp:ListItem Value="5">星期五</asp:ListItem>
                        
<asp:ListItem Value="6">星期六</asp:ListItem>
                        
<asp:ListItem Value="0">星期天</asp:ListItem>
                    
</asp:DropDownList>
                
</td>
                
<td>
                    
<asp:Button ID="btnClear" runat="server" Font-Size="12pt" Text="不选择任何日期" 
                        Width
="172px" onclick="btnClear_Click">
                    
</asp:Button>
                
</td>
            
</tr>
            
<tr>
                
<td colspan="2">
                    
<hr />
                
</td>
            
</tr>
            
<tr>
                
<td colspan="2">
                    
<asp:Label ID="Message" runat="server" ForeColor="Red" Font-Bold="True"></asp:Label>
                
</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>

后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm017 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                MyCalendar.VisibleDate 
= MyCalendar.TodaysDate;
            }

        }

        
protected void MyCalendar_SelectionChanged(object sender, EventArgs e)
        {
            CountSelection();
        }

        
protected void WeekDayDropList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
if (System.Convert.ToInt32(WeekDayDropList.SelectedItem.Value) < 0)
            {
                
return;
            }

            
int current_day = MyCalendar.VisibleDate.Day;
            
int current_month = MyCalendar.VisibleDate.Month;
            
int current_year = MyCalendar.VisibleDate.Year;

            MyCalendar.SelectedDates.Clear();

            
// 将使用者所选取之该月的所有特定星期几加至 SelectedDates 集合中
            int i;

            
// DaysInMonth 方法回传回指定月份的天数
            for (i = 1; i <= System.DateTime.DaysInMonth(current_year, current_month); i++)
            {
                
// 将 DateTime 结构的新执行个体初始化为指定的年、月、与日
                DateTime theDate = new DateTime(current_year, current_month, i);

                
if ((int)theDate.DayOfWeek == System.Convert.ToInt32(WeekDayDropList.SelectedItem.Value))
                {
                    MyCalendar.SelectedDates.Add(theDate);
                }
            }

            CountSelection();

        }

        
protected void btnClear_Click(object sender, EventArgs e)
        {
            MyCalendar.SelectedDates.Clear();
            CountSelection();
            WeekDayDropList.SelectedIndex 
= 0;
        }

        
protected void CountSelection()
        {
            
int i;
            
int DateNumber = MyCalendar.SelectedDates.Count;
            Message.Text 
= "用户共选择下列 " + DateNumber.ToString() + " 个日期:" + "<br>";
            
for (i = 0; i <= DateNumber - 1; i++)
            {
                Message.Text 
+= MyCalendar.SelectedDates[i].ToShortDateString() + "<br>";
            }
        }

    }
}


描述:连续选择某年某月某日到某年某月某日

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm018.aspx.cs" Inherits="CH5.CH5_DemoForm018" %>

<!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 id="Head1" runat="server">
    
<title>选择日期范围</title>
    
<style type="text/css">
        #form1
        
{
            text-align
: center;
        
}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<table>
            
<tr>
                
<td>
                    
<asp:Calendar ID="MyCalendar" runat="server" PrevMonthText='<IMG src="Images/R_FIRST.GIF">'
                        NextMonthText='
<IMG src="Images/R_LAST.GIF">' SelectionMode="DayWeekMonth" SelectWeekText='<IMG src="Images/ADD.JPG">'
                        SelectMonthText='
<IMG src="Images/Arrow1.jpg">' BorderColor="Navy" BorderWidth="2px"
                        Width="346px" Height="322px" 
                        onselectionchanged="MyCalendar_SelectionChanged">
                        
<TodayDayStyle Font-Bold="True" BorderWidth="3px" ForeColor="Red" BorderStyle="Ridge"
                            BorderColor
="#E0E0E0" BackColor="Yellow"></TodayDayStyle>
                        
<SelectorStyle BackColor="#FFC0C0"></SelectorStyle>
                        
<DayHeaderStyle Font-Size="12pt" Font-Names="标楷体" BackColor="#FFFFC0"></DayHeaderStyle>
                        
<SelectedDayStyle ForeColor="Blue" BackColor="#80FF80"></SelectedDayStyle>
                        
<TitleStyle Font-Bold="True" BorderWidth="2px" ForeColor="#400040" BorderColor="Navy"
                            BackColor
="#C0FFC0"></TitleStyle>
                        
<WeekendDayStyle Font-Italic="True" Font-Bold="True" ForeColor="Red"></WeekendDayStyle>
                        
<OtherMonthDayStyle BackColor="#C0FFFF"></OtherMonthDayStyle>
                    
</asp:Calendar>
                    
<br />
                    
<asp:Button ID="btnSelect" runat="server" Text="选择当前月份的 10 到 20 号" Font-Size="12pt"
                        Width
="284px" onclick="btnSelect_Click"></asp:Button>
                
</td>
                
<td>
                    
<asp:Label ID="Message" runat="server" ForeColor="#0000C0"></asp:Label>
                
</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>


后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm018 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                MyCalendar.VisibleDate 
= MyCalendar.TodaysDate;
            }

        }

        
protected void btnSelect_Click(object sender, EventArgs e)
        {
            
int current_year = MyCalendar.VisibleDate.Year;
            
int current_month = MyCalendar.VisibleDate.Month;
            DateTime BeginningDate 
= new DateTime(current_year, current_month, 10);
            DateTime EndingDate 
= new DateTime(current_year, current_month, 20);

            MyCalendar.SelectedDates.Clear();

            
// 选取一段日期范围
            MyCalendar.SelectedDates.SelectRange(BeginningDate, EndingDate);
            CountSelection();

        }

        
protected void MyCalendar_SelectionChanged(object sender, EventArgs e)
        {
            CountSelection();
        }

        
protected void CountSelection()
        {
            
int i;
            
int DateNumber = MyCalendar.SelectedDates.Count;
            Message.Text 
= "用户共选择下列 " + DateNumber.ToString() + " 个日期:" + "<br>";
            
for (i = 0; i <= DateNumber - 1; i++)
            {
                Message.Text 
+= MyCalendar.SelectedDates[i].ToShortDateString() + "<br>";
            }
        }
    }
}


描述:快速导航到某年某月

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm019.aspx.cs" Inherits="CH5.CH5_DemoForm019" %>

<!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 id="Head1" runat="server">
    
<title>日历快速导航示范</title>
    
<style type="text/css">
        #form1
        
{
            text-align
: center;
        
}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<br />
        
<table >
            
<tr>
                
<td>
                    
<asp:Calendar ID="MyCalendar" runat="server" PrevMonthText='<IMG src="Images/WZTOP.JPG">'
                        NextMonthText='
<IMG src="Images/WZEND.JPG">' SelectionMode="DayWeekMonth" SelectWeekText='<IMG src="Images/bullet5.gif">'
                        SelectMonthText='
<IMG src="Images/Arrow1.jpg">' BorderColor="Navy" BorderWidth="2px"
                        Width="526px" Height="322px" ShowGridLines="True" 
                        onselectionchanged="MyCalendar_SelectionChanged" 
                        onvisiblemonthchanged="MyCalendar_VisibleMonthChanged">
                        
<TodayDayStyle Font-Bold="True" BorderWidth="3px" ForeColor="Red" BorderStyle="Ridge"
                            BorderColor
="#E0E0E0" BackColor="Yellow"></TodayDayStyle>
                        
<SelectorStyle BackColor="#FFC0FF"></SelectorStyle>
                        
<DayHeaderStyle Font-Size="12pt" Font-Names="标楷体" BackColor="#FFFFC0"></DayHeaderStyle>
                        
<SelectedDayStyle ForeColor="Blue" BackColor="#80FF80"></SelectedDayStyle>
                        
<TitleStyle Font-Bold="True" BorderWidth="2px" ForeColor="#400040" BorderColor="Navy"
                            BackColor
="#C0FFC0"></TitleStyle>
                        
<WeekendDayStyle Font-Italic="True" Font-Bold="True" ForeColor="Red"></WeekendDayStyle>
                        
<OtherMonthDayStyle BackColor="#C0FFFF"></OtherMonthDayStyle>
                    
</asp:Calendar>
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
<asp:Label ID="Label1" runat="server" Width="120px">请选择年份:</asp:Label>
                    
<asp:DropDownList ID="YearDropList" runat="server" Width="77px" Font-Size="12pt">
                        
<asp:ListItem Value="2000">2000</asp:ListItem>
                        
<asp:ListItem Value="2001">2001</asp:ListItem>
                        
<asp:ListItem Value="2002">2002</asp:ListItem>
                        
<asp:ListItem Value="2003">2003</asp:ListItem>
                        
<asp:ListItem Value="2004">2004</asp:ListItem>
                        
<asp:ListItem Value="2005">2005</asp:ListItem>
                        
<asp:ListItem Value="2006">2006</asp:ListItem>
                        
<asp:ListItem Value="2007">2007</asp:ListItem>
                        
<asp:ListItem Value="2008">2008</asp:ListItem>
                        
<asp:ListItem Value="2009">2009</asp:ListItem>
                        
<asp:ListItem Value="2010">2010</asp:ListItem>
                    
</asp:DropDownList>
                    
<asp:Label ID="Label2" runat="server" Width="120px">请选择月份:</asp:Label>
                    
<asp:DropDownList ID="MonthDropList" runat="server" Width="58px" Font-Size="12pt">
                        
<asp:ListItem Value="1">1</asp:ListItem>
                        
<asp:ListItem Value="2">2</asp:ListItem>
                        
<asp:ListItem Value="3">3</asp:ListItem>
                        
<asp:ListItem Value="4">4</asp:ListItem>
                        
<asp:ListItem Value="5">5</asp:ListItem>
                        
<asp:ListItem Value="6">6</asp:ListItem>
                        
<asp:ListItem Value="7">7</asp:ListItem>
                        
<asp:ListItem Value="8">8</asp:ListItem>
                        
<asp:ListItem Value="9">9</asp:ListItem>
                        
<asp:ListItem Value="10">10</asp:ListItem>
                        
<asp:ListItem Value="11">11</asp:ListItem>
                        
<asp:ListItem Value="12">12</asp:ListItem>
                    
</asp:DropDownList>
                    
<asp:Button ID="btnOk" runat="server" Font-Size="12pt" Text="确定" 
                        onclick
="btnOk_Click"></asp:Button>
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
<hr />
                
</td>
            
</tr>
            
<tr>
                
<td>
                    
<asp:Label ID="Message" runat="server" ForeColor="#C00000" Font-Bold="True"></asp:Label>
                
</td>
            
</tr>
        
</table>
    
</div>
    
</form>
</body>
</html>

后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm019 : System.Web.UI.Page
    {
        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                MyCalendar.VisibleDate 
= MyCalendar.TodaysDate;
            }
        }

        
protected void MyCalendar_SelectionChanged(object sender, EventArgs e)
        {
            DisplaySelection();
        }

        
protected void btnOk_Click(object sender, EventArgs e)
        {
            
int current_year = System.Convert.ToInt32(YearDropList.SelectedItem.Value);
            
int current_month = System.Convert.ToInt32(MonthDropList.SelectedItem.Value);

            
// 将使用者所选取之年份与月份的第一个日期指派给 VisibleDate 属性,
            
// 以便立即巡览至该年的该月份。
            MyCalendar.VisibleDate = new DateTime(current_year, current_month, 1);

            DateTime BeginningDate 
= new DateTime(current_year, current_month, 10);
            DateTime EndingDate 
= new DateTime(current_year, current_month, 20);

            MyCalendar.SelectedDates.Clear();

            
// 固定将 10 号到 20 号这一段日期选取起来
            MyCalendar.SelectedDates.SelectRange(BeginningDate, EndingDate);

            DisplaySelection();
        }

        
protected void MyCalendar_VisibleMonthChanged(object sender, MonthChangedEventArgs e)
        {
            MyCalendar.SelectedDates.Clear();

            DateTime BeginningDate 
= new DateTime(e.NewDate.Year, e.NewDate.Month, 10);
            DateTime EndingDate 
= new DateTime(e.NewDate.Year, e.NewDate.Month, 20);

            
// 固定将 10 号到 20 号这一段日期选取起来
            MyCalendar.SelectedDates.SelectRange(BeginningDate, EndingDate);

            DisplaySelection();
        }

        
protected void DisplaySelection()
        {
            
int i;
            
int DateNumber = MyCalendar.SelectedDates.Count;
            Message.Text 
= "VisibleDate 属性当前的设置值是:" + MyCalendar.VisibleDate.ToShortDateString() + "<br>" + "用户共选择下列 " + DateNumber.ToString() + " 个日期:" + "<br>";
            
for (i = 0; i <= DateNumber - 1; i++)
            {
                Message.Text 
+= MyCalendar.SelectedDates[i].ToShortDateString() + "<br>";
            }
        }

    }
}


描述:自定义个别日期——重要

前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CH5_DemoForm020.aspx.cs" Inherits="CH5.CH5_DemoForm020" %>

<!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 id="Head1" runat="server">
    
<title>将自定义内容加入日期中</title>
    
<style type="text/css">
        #form1
        
{
            text-align
: center;
        
}
    
</style>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:Calendar ID="MyCalendar" runat="server" PrevMonthText='<IMG src="Images/WZTOP.JPG">'
            NextMonthText='
<IMG src="Images/WZEND.JPG">' SelectionMode="DayWeekMonth" SelectWeekText='<IMG src="Images/bullet5.gif">'
            SelectMonthText='
<IMG src="Images/Arrow1.jpg">' BorderColor="Navy" BorderWidth="2px"
            Width="660px" Height="424px" ShowGridLines="True" 
            ondayrender="MyCalendar_DayRender" 
            onselectionchanged="MyCalendar_SelectionChanged">
            
<SelectorStyle BackColor="#FFC0FF"></SelectorStyle>
            
<DayHeaderStyle Font-Size="12pt" Font-Names="标楷体" BackColor="#FFFFC0"></DayHeaderStyle>
            
<SelectedDayStyle ForeColor="Blue" BackColor="#80FF80"></SelectedDayStyle>
            
<TitleStyle Font-Bold="True" BorderWidth="2px" ForeColor="#400040" BorderColor="Navy"
                BackColor
="#C0FFC0"></TitleStyle>
            
<WeekendDayStyle Font-Italic="True" Font-Bold="True" ForeColor="Red"></WeekendDayStyle>
            
<OtherMonthDayStyle BackColor="#C0FFFF"></OtherMonthDayStyle>
        
</asp:Calendar>
        
<br />
        
<asp:Label ID="Label1" runat="server" Width="120px">请选择年份:</asp:Label>
        
<asp:DropDownList ID="YearDropList" runat="server" Width="77px" Font-Size="12pt">
            
<asp:ListItem Value="2000">2000</asp:ListItem>
            
<asp:ListItem Value="2001">2001</asp:ListItem>
            
<asp:ListItem Value="2002">2002</asp:ListItem>
            
<asp:ListItem Value="2003">2003</asp:ListItem>
            
<asp:ListItem Value="2004">2004</asp:ListItem>
            
<asp:ListItem Value="2005">2005</asp:ListItem>
            
<asp:ListItem Value="2006">2006</asp:ListItem>
            
<asp:ListItem Value="2007">2007</asp:ListItem>
            
<asp:ListItem Value="2008">2008</asp:ListItem>
            
<asp:ListItem Value="2009">2009</asp:ListItem>
            
<asp:ListItem Value="2010">2010</asp:ListItem>
        
</asp:DropDownList>
        
<asp:Label ID="Label2" runat="server" Width="120px">请选择月份:</asp:Label>        
        
<asp:DropDownList ID="MonthDropList" runat="server" Width="58px" Font-Size="12pt">
            
<asp:ListItem Value="1">1</asp:ListItem>
            
<asp:ListItem Value="2">2</asp:ListItem>
            
<asp:ListItem Value="3">3</asp:ListItem>
            
<asp:ListItem Value="4">4</asp:ListItem>
            
<asp:ListItem Value="5">5</asp:ListItem>
            
<asp:ListItem Value="6">6</asp:ListItem>
            
<asp:ListItem Value="7">7</asp:ListItem>
            
<asp:ListItem Value="8">8</asp:ListItem>
            
<asp:ListItem Value="9">9</asp:ListItem>
            
<asp:ListItem Value="10">10</asp:ListItem>
            
<asp:ListItem Value="11">11</asp:ListItem>
            
<asp:ListItem Value="12">12</asp:ListItem>
        
</asp:DropDownList>
        
<asp:Button ID="btnOk" runat="server" Font-Size="12pt" Text="确定" 
            onclick
="btnOk_Click"></asp:Button>
        
<hr />
        
<asp:Label ID="Message" runat="server" ForeColor="#C00000" Font-Bold="True"></asp:Label>
        
<hr />
    
</div>
    
</form>
</body>
</html>

后台
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;

namespace CH5
{
    
public partial class CH5_DemoForm020 : System.Web.UI.Page
    {

        
// 建立一个用来储存自订内容的数组
        string[,] SpecialDays = new string[1332];

        
protected void Page_Load(object sender, EventArgs e)
        {
            
if (!IsPostBack)
            {
                MyCalendar.VisibleDate 
= MyCalendar.TodaysDate;
            }

            
int current_month = DateTime.Now.Month;
            
int current_day = DateTime.Now.Day;

            
// 将自订内容指派给数组
            SpecialDays[current_month, current_day] = "大家好";
            SpecialDays[
11= "新年快乐";
            SpecialDays[
13= "公司团拜";
            SpecialDays[
14= "喝春酒";
            SpecialDays[
126= "老妈的生日";
            SpecialDays[
28= "生日快乐";
            SpecialDays[
214= "情人节快乐";
            SpecialDays[
228= "部门聚餐";
            SpecialDays[
329= "青年节";
            SpecialDays[
41= "愚人节";
            SpecialDays[
51= "股东大会";
            SpecialDays[
61= "公司旅游";
            SpecialDays[
71= "新的会计年度";
            SpecialDays[
88= "父亲节";
            SpecialDays[
101= "身体检查";
            SpecialDays[
926= "生日快乐";
            SpecialDays[
111= "年度会报";
            SpecialDays[
1225= "圣诞节快乐";
            SpecialDays[
1231= "吃年饭";
        }

        
protected void MyCalendar_DayRender(object sender, DayRenderEventArgs e)
        {
            
if (e.Day.IsOtherMonth)
            {
                e.Cell.Controls.Clear();
                
// 此举使非本月的日期不会显示出来
            }
            
else
            {
                
// 建立样式对象
                Style SpecialDayStyle = new Style();

                SpecialDayStyle.BackColor 
= System.Drawing.Color.Red;
                SpecialDayStyle.ForeColor 
= System.Drawing.Color.DarkBlue;
                SpecialDayStyle.BorderColor 
= System.Drawing.Color.DarkGreen;
                SpecialDayStyle.BorderWidth 
= new Unit(5);
                SpecialDayStyle.BorderStyle 
= BorderStyle.Ridge;

                
try
                {
                    
string MyDay = SpecialDays[e.Day.Date.Month, e.Day.Date.Day];

                    
if (MyDay != "")
                    {
                        
// 于储存格中加入 LiteralControl 控件以便显示静态文字
                        e.Cell.Controls.Add(new LiteralControl("<br>" + MyDay));
                        
if (MyDay == "情人节快乐")
                        {
                            System.Web.UI.WebControls.Image MyImage 
= new System.Web.UI.WebControls.Image();
                            MyImage.ImageUrl 
= "Images/Valentine.jpg";

                            
// 于储存格中加入图片
                            e.Cell.Controls.Add(MyImage);
                        }

                        
// 替储存格套用样式
                        e.Cell.ApplyStyle(SpecialDayStyle);
                    }
                }
                
catch (Exception exc)
                {
                    Response.Write(exc.ToString());
                }
            }

        }

        
protected void MyCalendar_SelectionChanged(object sender, EventArgs e)
        {
            DisplaySelection();
        }

        
protected void btnOk_Click(object sender, EventArgs e)
        {
            
int current_year = System.Convert.ToInt32(YearDropList.SelectedItem.Value);
            
int current_month = System.Convert.ToInt32(MonthDropList.SelectedItem.Value);

            
// 将使用者所选择之年份与月份的第一个日期指派给 VisibleDate 属性,
            
// 以便立即巡览至该年的该月份。
            MyCalendar.VisibleDate = new DateTime(current_year, current_month, 1);

            MyCalendar.SelectedDates.Clear();

            DisplaySelection();
        }

        
protected void DisplaySelection()
        {
            
int i;
            
int DateNumber = MyCalendar.SelectedDates.Count;

            Message.Text 
= "VisibleDate 属性当前的设置值是:" + MyCalendar.VisibleDate.ToShortDateString() + "<br>" + "用户共选择下列 " + DateNumber.ToString() + " 个日期:" + "<br>";

            
for (i = 0; i <= DateNumber - 1; i++)
            {
                Message.Text 
+= MyCalendar.SelectedDates[i].ToShortDateString() + "<br>";
            }
        }

    }
}


 

 

 

posted @ 2010-10-18 21:31  草珊瑚  阅读(499)  评论(0编辑  收藏  举报