Let calendar appear when the page is called

Source: Internet
Author: User
Calendar| Show | The page has recently been asked by friends about how to do this in the asp.net when you need to call the calendar, let it show, select the time to automatically hide, and return a date string to the text entry box.
Once I saw a popular online with vb.net write UserControl realized this part of the function, where I change it to C # version, for the needs of friends to download learning.
In general, this feature is in two steps, and the first step is to create a UserControl control:
Its code is as follows (Popupcalendar.ascx):
<%@ control language= "C #" autoeventwireup= "false" codebehind= "PopUpCalendar.ascx.cs" inherits= " Calendarexample.popupcalendar "targetschema=" http://schemas.microsoft.com/intellisense/ie5 "%>
<asp:panel id= "Pnlcalendar" style= "Z-INDEX:101; left:0px; Position:absolute; top:0px "width=" 301px "height=" 163px "runat=" Server ">
<asp:calendar id= "Calendar1" width= "307px" height= "164px" runat= "server" backcolor= "white" bordercolor= "BLACK" Borderstyle= "Solid" nextmonthtext= "
<todaydaystyle backcolor= "#FFFFC0" ></TodayDayStyle>
<daystyle font-size= "8pt" font-names= "Arial" ></DayStyle>
<dayheaderstyle font-size= "10pt" font-underline= "True" font-names= "Arial" borderstyle= "None" backcolor= "#E0E0E0" ></DayHeaderStyle>
<selecteddaystyle font-size= "8pt" font-names= "Arial" font-bold= "True" forecolor= "white" backcolor= "Navy" >< /selecteddaystyle>
<titlestyle font-size= "10pt" font-names= "Arial" font-bold= "True" forecolor= "white" backcolor= "Navy" ></ Titlestyle>
<othermonthdaystyle forecolor= "Gray" ></OtherMonthDayStyle>
</asp:Calendar>
</asp:panel>
The codebehind file is as follows (PopUpCalendar.ascx.cs):
Namespace Calendarexample
{
Using System;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;



<summary>
Summary description of the Popupcalendar.
</summary>
Public abstract class PopUpCalendar:System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Calendar Calendar1;
protected System.Web.UI.WebControls.Panel Pnlcalendar;



private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
}



#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

Designer supports the desired method-do not use
The Code Editor modifies the contents of this method.
</summary>
private void InitializeComponent ()
{
This. Calendar1.selectionchanged + = new System.EventHandler (this. calendar1_selectionchanged);
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
public void Displaycalendar (string scaltooltext, DateTime dselecteddate, string sdatefieldname, int itop, int ileft)
{
if (pnlcalendar.visible = = False)
{
pnlcalendar.style["top"] = Itop.tostring ();
Pnlcalendar.style["Left" = ileft.tostring ();
Calendar1.selecteddate = dselecteddate;
Calendar1.visibledate = dselecteddate;
Calendar1.tooltip = Scaltooltext;
calendar1.attributes["Selectedfield"] = Sdatefieldname;
Pnlcalendar.visible = true;
}
Else
Hidecalendar ();
}



public void Hidecalendar ()
{
Pnlcalendar.visible = false;
}



private void Calendar1_SelectionChanged (object sender, System.EventArgs e)
{
TextBox txtdate;
Txtdate = (TextBox) page.findcontrol (calendar1.attributes["Selectedfield"));
Txtdate.text = Calendar1.SelectedDate.ToShortDateString ();
Hidecalendar ();
}
}
}



The second step is to call this UserControl in the ASP.net file;
Its code is as follows (dispcalendar.aspx):
<%@ Register tagprefix= "SK" Tagname= "Popupcalendar" src= "Popupcalendar.ascx"%>
<%@ Page language= "C #" codebehind= "DispCalendar.aspx.cs" autoeventwireup= "false" inherits= " Calendarexample.webform1 "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta content= "Microsoft Visual Studio 7.0" name= "Generator" >
<meta content= "C #" Name= "Code_language" >
<meta content= "JavaScript" name= "vs_defaultClientScript" >
<meta content= "http://schemas.microsoft.com/intellisense/ie5" name= "Vs_targetschema" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<sk:popupcalendar id= "MyCalendar" runat= "Server" ></sk:popupcalendar>
<table>
<tr>
&LT;TD width= ">"
Select Date:
</td>
<td>
<asp:textbox id= "txtstartdate" runat= "Server" width= "86px" ></asp:TextBox>
</td>
<td>
<asp:imagebutton id= "Button1" runat= "Server" imageurl= "Calendar.gif" ></asp:ImageButton>
</td>
</tr>
</table>
</form>
</body>
</HTML>
The corresponding codebehind files are as follows (DispCalendar.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 Calendarexample
{
<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox txtstartdate;
protected System.Web.UI.WebControls.ImageButton Button1;
protected Popupcalendar MyCalendar;

private void Page_Load (object sender, System.EventArgs e)
{
if (! IsPostBack)
Mycalendar.hidecalendar ().//Place user code here to initialize the page
}



#region Web Form Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Button1.Click + = new System.Web.UI.ImageClickEventHandler (this. Button1_Click);
This. Load + = new System.EventHandler (this. Page_Load);



}
#endregion

private void Button1_Click (object sender, System.Web.UI.ImageClickEventArgs e)
{
DateTime dseldate;
Try
{
Dseldate=datetime.parse (Txtstartdate.text);
}
Catch
{
Dseldate=datetime.now;
}
Mycalendar.displaycalendar ("Select a start Date", Dseldate, "Txtstartdate", 22, 215);
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.