转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
ASP.NET工程中日历控件的典型应用之一         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:680 更新时间:2009/4/23 10:50:28

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

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

源代码如下: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;
using System.Web.UI.HtmlControls;

namespace WebApplication_rd
{
 /// <summary>
 /// Summary description for WebForm1.
 /// </summary>
 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.ExecuteReader();
    DataGrid1.DataBind();
    con.Close();
   }
   catch (Exception ex)
   {
    Trace.Warn(ex.Message);
   }
  }


  #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.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  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[iLastCellIndex].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 = "<a href=\"javascript:pickDate(''''" +
     sTextBoxName +
     "'''');\"><IMG SRC=\"i/iCalendar.gif\" border=\"0\" align=\"absmiddle\"></a>"; //The HTML to add to the Edit cell
    e.Item.Cells[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>
 /// Summary description for CalendarPopUp.
 /// </summary>
 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 Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  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("<script language=''''javascript''''>"); //Opening script tag
   sbScript.Append(Environment.NewLine); //Newline
   sbScript.Append("window.opener.document.all[''''"); //Reference to original window
   sbScript.Append(Request.QueryString["src"]); //Control to set value on
   sbScript.Append("''''].value = ''''"); //Set the value
   sbScript.Append(Calendar1.SelectedDate.ToShortDateString()); //The date
   sbScript.Append("'''';"); //End of line
   sbScript.Append(Environment.NewLine); //Newline
   sbScript.Append("window.close();"); //Close this window
   sbScript.Append(Environment.NewLine); //Newline
   sbScript.Append("</script>"); //Closing script tag

   //Add the script to the page
   this.Page.Controls.Add(new LiteralControl(sbScript.ToString()));
  }
 }
}

 

 


[C语言系列]NET 中C#的switch语句的语法  [系统软件]托拽Explore中的文件到VB.net的窗口
[系统软件]Boost库在XP+Visual C++.net中的安装  [常用软件]新配色面板:Paint.Net3.0RC1官方下载
[常用软件]用内建的“Net Meeting”聊天  [VB.NET程序]Henry的VB.NET之旅(三)—共享成员
[VB.NET程序]Henry的VB.NET之旅(二)—构造与析构  [VB.NET程序]Henry的VB.NET之旅(一)—失踪的窗体
[VB.NET程序]在托盘上显示Balloon Tooltip(VB.NET)  [VB.NET程序]Henry手记-VB.NET中动态加载Treeview节点(二)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台