实例4-2 通过键盘快捷键激活特定文本框
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Label.aspx.cs" Inherits="Chap4_Label" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server" AccessKey="N" AssociatedControlID="txtName" Text="用户名(N):"></asp:Label>
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox><br/>
<asp:Label ID="lblPassword" runat="server" AccessKey="M" AssociatedControlID="txtPassword" Text="密码(M):"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
实例 4-3 综合运用TextBox控件
源程序:TextBox.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TextBox.aspx.cs" Inherits="Chap4_TextBox" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
用户名:<asp:TextBox ID="txtName" runat="server" AutoPostBack="true" OnTextChanged="TxtName_TextChanged" Width="150px">
</asp:TextBox>
<asp:Label ID="lblValidate" runat="server"></asp:Label><br/>
密码:<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" Style="position: relative; left: 16px" Width="150px"></asp:TextBox><br />
邮箱:<asp:TextBox ID="txtMail" runat="server" AutoCompleteType="Email" Style="position: relative; left: 16px" Width="150px"></asp:TextBox><br />
<asp:Button ID="btnSubmit" runat="server" Text="确认" />
</div>
</form>
</body>
</html>
源程序:TextBox.aspx.cs
using System;
public partial class Chap4_TextBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
txtName.Focus();
}
protected void TxtName_TextChanged(object sender, EventArgs e)
{
if (txtName.Text == "leaf")
{
lblValidate.Text = "用户名已占用!";
}
else
{
lblValidate.Text = "√";
}
}
}
实例4-4 利用Button控件执行客户端脚本
源程序:ClientClick.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientClick.aspx.cs" Inherits="Chap4_ClientClick" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnDelete" runat="server" Text="删除" OnClick="BtnDelete_Click" OnClientClick="return confirm('确定要删除记录吗?')"/>
</div>
</form>
</body>
</html>
源程序:ClientClick.aspx.cs
using System;
public partial class Chap4_ClientClick : System.Web.UI.Page
{
protected void BtnDelete_Click(object sender, EventArgs e)
{
Response.Write("删除成功!");
}
}
实例4-5 实现联动的下拉列表
源程序:DropDownList.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownList.aspx.cs" Inherits="Chap4_DropDownList" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlYear" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DdlYear_SelectedIndexChanged"></asp:DropDownList>年
<asp:DropDownList ID="ddlMonth" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DdlMonth_SelectedIndexChanged"></asp:DropDownList>月
<asp:DropDownList ID="ddlDay" runat="server"></asp:DropDownList>日
</div>
</form>
</body>
</html>
源程序:DropDownList.aspx.cs代码
using System;
using System.Web.UI.WebControls;
public partial class Chap4_DropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindYear();
BindMonth();
BindDay();
}
}
protected void BindYear()
{
ddlYear.Items.Clear();
int startyear = DateTime.Now.Year - 10;
int currentYear = DateTime.Now.Year;
for (int i = startyear; i <= currentYear; i++)//向ddlYear添加最近10年的年份
{
ddlYear.Items.Add(new ListItem(i.ToString()));
}
ddlYear.SelectedValue = currentYear.ToString();//设置ddlYear的默认项
}
protected void BindMonth()
{
ddlMonth.Items.Clear();
for (int i = 1;i <= 12;i++)
{
ddlMonth.Items.Add(i.ToString());
}
}
protected void BindDay()
{
ddlDay.Items.Clear();
string year = ddlYear.SelectedValue;//获取ddlYear中选定项的值
string month = ddlMonth.SelectedValue;
//获取相应年、月对应的天数
int days = DateTime.DaysInMonth(int.Parse(year), int.Parse(month));
for (int i = 1; i <= days;i++)//向ddlDay添加相应年、月对应的天数
{
ddlDay.Items.Add(i.ToString());
}
}
protected void DdlYear_SelectedIndexChanged(object sender, EventArgs e)
{
BindDay();
}
protected void DdlMonth_SelectedIndexChanged(object sender, EventArgs e)
{
BindDay();
}
}