这是在这儿的第1篇随笔,因今天休息,便试着用ajax写了写出生年月日选择,初学...
.aspx文件代码
<%@ Page Language="C#" ResponseEncoding="gb2312"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
if (!Page.IsPostBack)
{
ds = AjaxMethod.GetYears();
this.DropDownList4.DataSource = ds.Tables["years"];
this.DropDownList4.DataTextField = "YEARS";
this.DropDownList4.DataValueField = "YEARS";
this.DropDownList4.DataBind();
ds = AjaxMethod.GetMonths();
this.DropDownList5.DataSource = ds.Tables["months"];
this.DropDownList5.DataTextField = "MONTHS";
this.DropDownList5.DataValueField = "MONTHS";
this.DropDownList5.DataBind();
this.DropDownList4.Attributes.Add("onclick", "daysResult();");
this.DropDownList5.Attributes.Add("onclick", "daysResult();");
}
}
/// <summary>
/// 添加年份(1900-2100)及日期
/// </summary>
protected void insert_years()
{
string str = string.Empty;
//添加年份部分
for (int i = 1900; i < 2100; i++)
{
str = string.Empty;
//判断是否为闰年
if (((i % 4) == 0 && (i % 100) != 0) || (i % 400) == 0)
{
str = "INSERT INTO YEARS(YEARS,IS_LEAP) VALUES(" + i + ",1)";
int k = new Mssql().ExecuteSql(str);
}
else
{
str = "INSERT INTO YEARS(YEARS,IS_LEAP) VALUES(" + i + ",0)";
int k = new Mssql().ExecuteSql(str);
}
}
//添加日期
for (int i = 1; i < 31; i++)
{
//小月
str = string.Empty;
str = "INSERT INTO DAYS(DAYS,IS_MONTH) VALUES(" + i + ",0)";
int k = new Mssql().ExecuteSql(str);
}
for (int i = 1; i < 32; i++)
{
//大月
str = string.Empty;
str = "INSERT INTO DAYS(DAYS,IS_MONTH) VALUES(" + i + ",1)";
int k = new Mssql().ExecuteSql(str);
}
for (int i = 1; i < 29; i++)
{
//2月
str = string.Empty;
str = "INSERT INTO DAYS(DAYS,IS_MONTH) VALUES(" + i + ",2)";
int k = new Mssql().ExecuteSql(str);
}
for (int i = 1; i < 30; i++)
{
//闰年2月
str = string.Empty;
str = "INSERT INTO DAYS(DAYS,IS_MONTH) VALUES(" + i + ",3)";
int k = new Mssql().ExecuteSql(str);
}
}
</script>
<!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>years_months_days</title>
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<meta content="C#" name="CODE_LANGUAGE">
</head>
<script language="javascript">
//生日年月日
function daysResult(){
/*获取年份和月分,并判断是否是闰年及月份,并获取相应的天数*/
var years = document.getElementById("DropDownList4").value;
var months = document.getElementById("DropDownList5").value;
var is_month =0;
if((years % 4 == 0 && years % 100 != 0) || years % 400 ==0 ){
if(months == 2){
is_month=3;
}
else if(months == 1 || months == 3 || months == 5 || months == 7 || months == 8 || months == 10 || months == 12){
is_month=1;
}
else{
is_month=0;
}
}
else{
if(months == 2){
is_month=2;
}
else if(months == 1 || months == 3 || months == 5 || months == 7 || months == 8 || months == 10 || months == 12){
is_month=1;
}
else{
is_month=0;
}
}
//alert(is_month);
AjaxMethod.GetDays(is_month,get_days_Result_CallBack);
}
function get_days_Result_CallBack(response){
if(response.value != null){
document.all("DropDownList6").length = 0;
var ds = response.value;
if(ds != null && typeof(ds)=="object" && ds.Tables != null){
for(var i=0;i<ds.Tables[0].Rows.length;i++){
var name = ds.Tables[0].Rows[i].DAYS;
var id=ds.Tables[0].Rows[i].DAYS_ID;
document.all("DropDownList6").options.add(new Option(name,id));
}
}
}
return;
}
</script>
<body>
<form id="form1" runat="server">
<table border="1" style="color:BlanchedAlmond">
<tr>
<td style="100px" align="right"><font style="color:Black">您的生日:</font></td>
<td align="left" colspan="3">
<asp:DropDownList ID="DropDownList4" runat=server></asp:DropDownList><font style="color:Black">年</font>
<asp:DropDownList ID="DropDownList5" runat=server></asp:DropDownList><font style="color:Black">月</font>
<asp:DropDownList ID="DropDownList6" runat=server></asp:DropDownList><font style="color:Black">日</font>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
if (!Page.IsPostBack)
{
ds = AjaxMethod.GetYears();
this.DropDownList4.DataSource = ds.Tables["years"];
this.DropDownList4.DataTextField = "YEARS";
this.DropDownList4.DataValueField = "YEARS";
this.DropDownList4.DataBind();
ds = AjaxMethod.GetMonths();
this.DropDownList5.DataSource = ds.Tables["months"];
this.DropDownList5.DataTextField = "MONTHS";
this.DropDownList5.DataValueField = "MONTHS";
this.DropDownList5.DataBind();
this.DropDownList4.Attributes.Add("onclick", "daysResult();");
this.DropDownList5.Attributes.Add("onclick", "daysResult();");
}
}
/// <summary>
/// 添加年份(1900-2100)及日期
/// </summary>
protected void insert_years()
{
string str = string.Empty;
//添加年份部分
for (int i = 1900; i < 2100; i++)
{
str = string.Empty;
//判断是否为闰年
if (((i % 4) == 0 && (i % 100) != 0) || (i % 400) == 0)
{
str = "INSERT INTO YEARS(YEARS,IS_LEAP) VALUES(" + i + ",1)";
int k = new Mssql().ExecuteSql(str);
}
else
{
str = "INSERT INTO YEARS(YEARS,IS_LEAP) VALUES(" + i + ",0)";
int k = new Mssql().ExecuteSql(str);
}
}
//添加日期
for (int i = 1; i < 31; i++)
{
//小月
str = string.Empty;
str = "INSERT INTO DAYS(DAYS,IS_MONTH) VALUES(" + i + ",0)";
int k = new Mssql().ExecuteSql(str);
}
for (int i = 1; i < 32; i++)
{
//大月
str = string.Empty;
str = "INSERT INTO DAYS(DAYS,IS_MONTH) VALUES(" + i + ",1)";
int k = new Mssql().ExecuteSql(str);
}
for (int i = 1; i < 29; i++)
{
//2月
str = string.Empty;
str = "INSERT INTO DAYS(DAYS,IS_MONTH) VALUES(" + i + ",2)";
int k = new Mssql().ExecuteSql(str);
}
for (int i = 1; i < 30; i++)
{
//闰年2月
str = string.Empty;
str = "INSERT INTO DAYS(DAYS,IS_MONTH) VALUES(" + i + ",3)";
int k = new Mssql().ExecuteSql(str);
}
}
</script>
<!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>years_months_days</title>
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<meta content="C#" name="CODE_LANGUAGE">
</head>
<script language="javascript">
//生日年月日
function daysResult(){
/*获取年份和月分,并判断是否是闰年及月份,并获取相应的天数*/
var years = document.getElementById("DropDownList4").value;
var months = document.getElementById("DropDownList5").value;
var is_month =0;
if((years % 4 == 0 && years % 100 != 0) || years % 400 ==0 ){
if(months == 2){
is_month=3;
}
else if(months == 1 || months == 3 || months == 5 || months == 7 || months == 8 || months == 10 || months == 12){
is_month=1;
}
else{
is_month=0;
}
}
else{
if(months == 2){
is_month=2;
}
else if(months == 1 || months == 3 || months == 5 || months == 7 || months == 8 || months == 10 || months == 12){
is_month=1;
}
else{
is_month=0;
}
}
//alert(is_month);
AjaxMethod.GetDays(is_month,get_days_Result_CallBack);
}
function get_days_Result_CallBack(response){
if(response.value != null){
document.all("DropDownList6").length = 0;
var ds = response.value;
if(ds != null && typeof(ds)=="object" && ds.Tables != null){
for(var i=0;i<ds.Tables[0].Rows.length;i++){
var name = ds.Tables[0].Rows[i].DAYS;
var id=ds.Tables[0].Rows[i].DAYS_ID;
document.all("DropDownList6").options.add(new Option(name,id));
}
}
}
return;
}
</script>
<body>
<form id="form1" runat="server">
<table border="1" style="color:BlanchedAlmond">
<tr>
<td style="100px" align="right"><font style="color:Black">您的生日:</font></td>
<td align="left" colspan="3">
<asp:DropDownList ID="DropDownList4" runat=server></asp:DropDownList><font style="color:Black">年</font>
<asp:DropDownList ID="DropDownList5" runat=server></asp:DropDownList><font style="color:Black">月</font>
<asp:DropDownList ID="DropDownList6" runat=server></asp:DropDownList><font style="color:Black">日</font>
</td>
</tr>
</table>
</form>
</body>
</html>
AjaxMethod.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class AjaxMethod
{
public AjaxMethod()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
年份
月份
日期
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class AjaxMethod
{
public AjaxMethod()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
年份
月份
日期
}
DataBase.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class DataBase
{
// 连接数据源
private SqlConnection con = null;
private static ConnectionStringSettings settings;
private static string conStr;
public DataBase()
{
settings = ConfigurationManager.ConnectionStrings["ConnectionString"];
conStr = settings.ToString();
con = new SqlConnection(conStr);
}
/// <summary>
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns>DataSet</returns>
public DataSet returnDS(string sql)
{
DataSet ds=new DataSet();
try
{
SqlCommand cmd =new SqlCommand(sql,con);
cmd.CommandTimeout=20;
this.Open();
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
adapter.Fill(ds,"tempTable");
}
catch(Exception e)
{
ds = null;
throw(e);
}
finally
{
this.Close();
}
return ds;
}
/// <summary>
/// 打开数据库连接.
/// </summary>
private void Open()
{
if(con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
else if(con.State == System.Data.ConnectionState.Broken)
{
con.Close();
con.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (con != null)
{
con.Close();
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
if (con != null)
{
con.Dispose();
con = null;
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class DataBase
{
// 连接数据源
private SqlConnection con = null;
private static ConnectionStringSettings settings;
private static string conStr;
public DataBase()
{
settings = ConfigurationManager.ConnectionStrings["ConnectionString"];
conStr = settings.ToString();
con = new SqlConnection(conStr);
}
/// <summary>
/// 根据SQL查询返回DataSet对象,如果没有查询到则返回NULL
/// </summary>
/// <param name="sql">查询语句</param>
/// <returns>DataSet</returns>
public DataSet returnDS(string sql)
{
DataSet ds=new DataSet();
try
{
SqlCommand cmd =new SqlCommand(sql,con);
cmd.CommandTimeout=20;
this.Open();
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
adapter.Fill(ds,"tempTable");
}
catch(Exception e)
{
ds = null;
throw(e);
}
finally
{
this.Close();
}
return ds;
}
/// <summary>
/// 打开数据库连接.
/// </summary>
private void Open()
{
if(con.State == System.Data.ConnectionState.Closed)
{
con.Open();
}
else if(con.State == System.Data.ConnectionState.Broken)
{
con.Close();
con.Open();
}
}
/// <summary>
/// 关闭数据库连接
/// </summary>
public void Close()
{
if (con != null)
{
con.Close();
}
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
if (con != null)
{
con.Dispose();
con = null;
}
}
}
数据库脚本
CREATE TABLE YEARS
(
YEARS_ID INT IDENTITY PRIMARY KEY,
YEARS INT,--年份
IS_LEAP TINYINT--是否闰年
)
CREATE TABLE MONTHS
(
MONTHS_ID TINYINT IDENTITY PRIMARY KEY,
MONTHS TINYINT
)
CREATE TABLE DAYS
(
DAYS_ID TINYINT IDENTITY PRIMARY KEY,
DAYS TINYINT,
IS_MONTH TINYINT--根据月份选择天数
)
(
YEARS_ID INT IDENTITY PRIMARY KEY,
YEARS INT,--年份
IS_LEAP TINYINT--是否闰年
)
CREATE TABLE MONTHS
(
MONTHS_ID TINYINT IDENTITY PRIMARY KEY,
MONTHS TINYINT
)
CREATE TABLE DAYS
(
DAYS_ID TINYINT IDENTITY PRIMARY KEY,
DAYS TINYINT,
IS_MONTH TINYINT--根据月份选择天数
)
还需要Ajax.dll.
注:工程是VS2005写的.