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 {///
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);} / //
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 {///
} #Endregion
private void LinkButton1_Click (object sender, System.EventArgs e) {System.Text.StringBuilder sbScript = new System.Text.StringBuilder (); sbScript.Append ( "