ASP.NET工程中日历控件的典型应用

2013 年 8 月 24 日3990

  很多朋友问我,如何动态选择日期呢,如何选择完日期以后,关闭本页同时把日期传入上一页呢?这个问题也曾经困扰我许久,在ASP中我知道如何做,可是在ASP.NET中却有点迷惑。

  为此,我查阅了很多的资料,终于研究通了,现在分享给大家。

  源代码如下:calendar.aspx.cs

usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
namespaceWebApplication_rd
{
///<summary>
///SummarydescriptionforWebForm1.
///</summary>
publicclasscalendar:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.LabelLabel1;
protectedSystem.Web.UI.WebControls.DataGridDataGrid1;

privatevoidPage_Load(objectsender,System.EventArgse)
{
if(!Page.IsPostBack)
BindData();
}
privatevoidBindData()
{
SqlConnectioncon=newSqlConnection("server=localhost;database=Northwind;uid=sa;pwd=;");
SqlCommandcmd=newSqlCommand("SELECTTOP10*FROMOrders",con);
try
{
con.Open();
DataGrid1.DataSource=cmd.ExecuteReader();
DataGrid1.DataBind();
con.Close();
}
catch(Exceptionex)
{
Trace.Warn(ex.Message);
}
}

#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
//
InitializeComponent();
base.OnInit(e);
}

///<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
{
this.DataGrid1.CancelCommand+=newSystem.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelItemCommand);
this.DataGrid1.EditCommand+=newSystem.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditItemCommand);
this.DataGrid1.UpdateCommand+=newSystem.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateItemCommand);
this.DataGrid1.ItemDataBound+=newSystem.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_OnItemDataBound);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
privatevoidDataGrid1_EditItemCommand(objectsender,System.Web.UI.WebControls.DataGridCommandEventArgse)
{
DataGrid1.EditItemIndex=e.Item.ItemIndex;
BindData();
}
privatevoidDataGrid1_CancelItemCommand(objectsender,System.Web.UI.WebControls.DataGridCommandEventArgse)
{
DataGrid1.EditItemIndex=-1;
BindData();
}
privatevoidDataGrid1_UpdateItemCommand(objectsender,System.Web.UI.WebControls.DataGridCommandEventArgse)
{
Int32iLastCellIndex=e.Item.Cells.Count-1;//TheIndexforthelastcell
StringsNewDate=((TextBox)e.Item.Cells[iLastCellIndex].FindControl("txtDate")).Text;//GetthevalueoftheTextBox
Label1.Text="Yousetthedate"+sNewDate;
//Adddatabaseupdatinghere
DataGrid1.EditItemIndex=-1;
BindData();
}
privatevoidDataGrid1_OnItemDataBound(objectsender,System.Web.UI.WebControls.DataGridItemEventArgse)
{
if(e.Item.ItemType==ListItemType.EditItem)
{
Int32iLastCellIndex=e.Item.Cells.Count-1;//TheIndexforthelastcell
StringsTextBoxName=e.Item.Cells[iLastCellIndex].FindControl("txtDate").ClientID;//TherenderednameoftheTextBox
StringsLink="<ahref=\"javascript:pickDate(''''"+
sTextBoxName+
"'''');\"><IMGSRC=\"i/iCalendar.gif\"border=\"0\"align=\"absmiddle\"></a>";//TheHTMLtoaddtotheEditcell
e.Item.Cells[iLastCellIndex].Controls.Add(newLiteralControl(sLink));//AddtheHTML
}
}
}
}

  CalendarPopUp.aspx.cs

usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
namespaceWebApplication_rd
{
///<summary>
///SummarydescriptionforCalendarPopUp.
///</summary>
publicclassCalendarPopUp:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.LinkButtonLinkButton1;
protectedSystem.Web.UI.WebControls.CalendarCalendar1;

privatevoidPage_Load(objectsender,System.EventArgse)
{
//Putusercodetoinitializethepagehere
}
#regionWebFormDesignergeneratedcode
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:ThiscallisrequiredbytheASP.NETWebFormDesigner.
//
InitializeComponent();
base.OnInit(e);
}

///<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
{
this.LinkButton1.Click+=newSystem.EventHandler(this.LinkButton1_Click);
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
privatevoidLinkButton1_Click(objectsender,System.EventArgse)
{
System.Text.StringBuildersbScript=newSystem.Text.StringBuilder();
sbScript.Append("<scriptlanguage=''''javascript''''>");//Openingscripttag
sbScript.Append(Environment.NewLine);//Newline
sbScript.Append("window.opener.document.all[''''");//Referencetooriginalwindow
sbScript.Append(Request.QueryString["src"]);//Controltosetvalueon
sbScript.Append("''''].value=''''");//Setthevalue
sbScript.Append(Calendar1.SelectedDate.ToShortDateString());//Thedate
sbScript.Append("'''';");//Endofline
sbScript.Append(Environment.NewLine);//Newline
sbScript.Append("window.close();");//Closethiswindow
sbScript.Append(Environment.NewLine);//Newline
sbScript.Append("</script>");//Closingscripttag
//Addthescripttothepage
this.Page.Controls.Add(newLiteralControl(sbScript.ToString()));
}
}
}

0 0