One of the typical applications of the calendar control in the ASP.NET project

zhaozj2021-02-16  49

Many friends ask me, how to dynamically select the date, how to close the date after the date is closed on this page at the same time? This problem has also plagued me for a long time, I know how to do it in ASP, but it is a bit confused in ASP.NET.

To this end, I have reviewed a lot of information, I finally study, now share it to everyone.

The source code is as follows: Calendar.aspx.cs

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.ui.WebControls; use system.web.ui.htmlcontrols;

namespace WebApplication_rd {///

/// Summary description for WebForm1 /// public class calendar: System.Web.UI.Page {protected System.Web.UI.WebControls.Label Label1; protected System. .Web.ui.webcontrols.DataGrid DataGrid1; Private Void Page_load (Object Sender, System.EventArgs E) {if (! Page.ispostback) binddata ();

Private void binddata () {SQLCONNECTION Con = New SqlConnection ("Server = localhost; database = northwind; uid = sa; pwd =;"); sqlcommand cmd = new SQLCommand ("SELECT TOP 10 * from Orders", CON);

Try {con.open (); DataGrid1.datasource = cmd.executeRead (); dataGrid1.database () ;con.close ();} catch (exception ex) {trace.warn (ex. measureage);}}

#Region Web Form Designer generated code override protected void oninit (Eventargs e) {//// Codegen: this call is required by the asp.net web form designer. // initializecomponent (); base.onit (e);} / //

/// Required method for Designer support -. do not modify /// the contents of this method with the code editor /// private void InitializeComponent () {this.DataGrid1.CancelCommand = new System.Web.UI.WebControls.DataGridCommandEventHandler (this.DataGrid1_CancelItemCommand); this.DataGrid1.EditCommand = new System.Web.UI.WebControls.DataGridCommandEventHandler (this.DataGrid1_EditItemCommand); this.DataGrid1.UpdateCommand = new System.Web .UI.WebControls.DataGridCommandEventHandler (this.DataGrid1_UpdateItemCommand); this.DataGrid1.ItemDataBound = new System.Web.UI.WebControls.DataGridItemEventHandler (this.DataGrid1_OnItemDataBound); this.Load = new System.EventHandler (this.Page_Load); } #Endregion

Private void DataGrid1_EdititeMCommand (Object sender, system.web.ui.webcontrols.dataGridCommandeventArgs e) {dataGrid1.edititemindex = E.Item.itemindex; binddata ();

Private void DataGrid1_cancelitemCommand (Object sender, System.Web.ui.WebControls.DataGridCommandeventArgs e) {datagrid1.edititemindex = -1; binddata ();

private void DataGrid1_UpdateItemCommand (object sender, System.Web.UI.WebControls.DataGridCommandEventArgs e) {Int32 iLastCellIndex = e.Item.Cells.Count-1; // The Index for the last cell String sNewDate = ((TextBox) e.Item .Cells [ilthcellindex] .FindControl ("txtdate"))). Text; // get the value of the textbox label1.text = "you set the date" snewdate; // add database updating here DataGrid1.edititeMindex = -1; BindData ();} private void DataGrid1_OnItemDataBound (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) {if (e.Item.ItemType == ListItemType.EditItem) {Int32 iLastCellIndex = e.Item.Cells.Count-1 ; // The Index for the last cell String sTextBoxName = e.Item.Cells [iLastCellIndex] .FindControl ( "txtDate") ClientID; // The rendered name of the TextBox String sLink = " "; // The html to add to the Edit Cell E.Item.c Ells [ilastcellindex] .controls.add (new literalcontrol (slink)); // add the html}}

}

Calendarpopup.aspx.cs

using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls Using system.Web.ui.htmlcontrols;

namespace WebApplication_rd {///

/// Summary description for CalendarPopUp /// public class CalendarPopUp:. System.Web.UI.Page {protected System.Web.UI.WebControls.LinkButton LinkButton1; protected System .Web.UI.WebControls.Calendar Calendar1; private void Page_Load (object sender, System.EventArgs e) {// Put user code to initialize the page here} #region Web Form Designer generated code override protected void OnInit (EventArgs e) { /// codegen: this call is required by the asp.net web form design; base.onit (e);} /// /// Required Method for Designer Support - Do Not modify /// the contents of this method with the code editor /// private void InitializeComponent () {this.LinkButton1.Click = new System.EventHandler (this.LinkButton1_Click);. this.Load = new System.EventHandler (this.page_load);

} #Endregion

private void LinkButton1_Click (object sender, System.EventArgs e) {System.Text.StringBuilder sbScript = new System.Text.StringBuilder (); sbScript.Append ( "