Click here to Skip to main content
15,886,689 members
Articles / Web Development / ASP.NET

ASP.NET date picker control - Part 2

Rate me:
Please Sign up or sign in to vote.
4.92/5 (10 votes)
24 Aug 2009CPOL1 min read 57.7K   33   16
In this post, I will explain you how I have embedded JavaScript, images and stylesheet to my previous article.

image

Introduction

In my previous post How to: Create a Date Picker Composite Control in ASP.NET (C#), I explained how to work with ASP.NET composite control to create a date picker control for ASP.NET.

Problem

However to use that control you still require JavaScript, style and images to be included.

Solution

In this post, I will explain you how I have embedded JavaScript, images and stylesheet to my previous article, How to: Create a Date Picker Composite Control in ASP.NET (C#). I have also added another property to the control which is how to change date format for the control. You can download the code, drag and drop it and start using it inside data navigation controls or anywhere in your ASP.NET web application.

Property

  • DateFormat - By default, it's been set to "%d/%m/%Y" which is DMY.

How to Embed ?

I added to the project images, JavaScripts, and styles that I need for my date picker control. Click on each file properties and change Build Action from Content to Embedded Resource, then modify the AssemblyInfo.cs as below:

C#
[assembly: System.Web.UI.WebResource("DatePicker.Resources.calendarview.css", "text/css")]
[assembly: System.Web.UI.WebResource("DatePicker.Resources.calendarview.js", "text/js")]
[assembly: System.Web.UI.WebResource("DatePicker.Resources.CalendarIcon.gif", "img/gif")]
[assembly: System.Web.UI.WebResource("DatePicker.Resources.prototype.js", "text/js")]

Note: The format of embedded resources is very important which is:

C#
[Assembly Name].[Folder].[File Name]

To Embed JavaScript

To embed JavaScript, I have created a function called AddJavaScript(string javaScriptFile) which is:

C#
private void AddJavaScript(string javaScriptFile)
    {
        string scriptLocation = Page.ClientScript.GetWebResourceUrl
		(this.GetType(),javaScriptFile );
        Page.ClientScript.RegisterClientScriptInclude(javaScriptFile, scriptLocation);

    }

To Embed Stylesheet

C#
private void AddStyleSheet()
     {
         string includeTemplate = "<link rel='stylesheet' text='text/css' href='{0}' />";
         string includeLocation =
               Page.ClientScript.GetWebResourceUrl
		(this.GetType(), "DatePicker.Resources.calendarview.css");
         LiteralControl include = new LiteralControl
				(String.Format(includeTemplate, includeLocation));
         Page.Header.Controls.Add(include);
     }

To Embed Image

Just add the following line inside the OnInit function:

JavaScript
_ImgDate.ImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(), 
			"DatePicker.Resources.CalendarIcon.gif");

Add these functions inside the OnInit function:

C#
AddStyleSheet();
AddJavaScript("DatePicker.Resources.prototype.js");
AddJavaScript("DatePicker.Resources.calendarview.js");

And that is our date picker control ready to use. You can drag and drop on any page and start using it.

View this article on my blog.

Your feedback is welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) BMJ
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralError: Microsoft JScript runtime error: Object doesn't support this property or method Pin
Daniel Kamisnki4-Dec-09 15:50
Daniel Kamisnki4-Dec-09 15:50 
Generalnice on.. Pin
Rajesh Pillai24-Aug-09 13:27
Rajesh Pillai24-Aug-09 13:27 
GeneralRe: nice on.. Pin
Salmanzz25-Aug-09 0:47
Salmanzz25-Aug-09 0:47 
thanks for your feedback
GeneralAnother issue I'd like to solve Pin
Phyllis Smith15-Aug-09 16:18
Phyllis Smith15-Aug-09 16:18 
GeneralRe: Another issue I'd like to solve Pin
Salmanzz16-Aug-09 8:21
Salmanzz16-Aug-09 8:21 
GeneralProblem with adding validators Pin
Phyllis Smith14-Aug-09 15:55
Phyllis Smith14-Aug-09 15:55 
GeneralRe: Problem with adding validators Pin
Salmanzz15-Aug-09 0:32
Salmanzz15-Aug-09 0:32 
GeneralRe: Problem with adding validators Pin
Phyllis Smith15-Aug-09 2:19
Phyllis Smith15-Aug-09 2:19 
QuestionVery good - but the calender is placed abowe the page? Pin
palsbo21-Jul-09 23:46
palsbo21-Jul-09 23:46 
AnswerRe: Very good - but the calender is placed abowe the page? Pin
Salmanzz21-Jul-09 23:59
Salmanzz21-Jul-09 23:59 
GeneralThanks great control Pin
wtf_imanut21-Jul-09 8:35
wtf_imanut21-Jul-09 8:35 
GeneralRe: Thanks great control Pin
Salmanzz21-Jul-09 23:28
Salmanzz21-Jul-09 23:28 
GeneralRe: Thanks great control Pin
wtf_imanut27-Jul-09 8:34
wtf_imanut27-Jul-09 8:34 
GeneralRe: Thanks great control Pin
Salmanzz27-Jul-09 23:42
Salmanzz27-Jul-09 23:42 
GeneralGood Work! Pin
prasad022-Jul-09 3:19
prasad022-Jul-09 3:19 
GeneralRe: Good Work! Pin
Salmanzz2-Jul-09 4:56
Salmanzz2-Jul-09 4:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.