一个日期选择控件,(半原创)

微软自带的日期控件感觉不太好用,到处找,最后客户写了个JS(客户也是搞开发的),我把JS封装了一下
比较简单,不写注释了
CS代码如下
  1 None.gif using  System;
  2 None.gif using  System.Collections.Generic;
  3 None.gif using  System.ComponentModel;
  4 None.gif using  System.Text;
  5 None.gif using  System.Web;
  6 None.gif using  System.Web.UI;
  7 None.gif using  System.Web.UI.WebControls;
  8 None.gif using  System.Security.Permissions;
  9 None.gif
 10 None.gif[assembly: WebResource( " WYN.WebControls.script_PopupCalender.js " " application/x-javascript " , PerformSubstitution  =   true )]
 11 None.gif[assembly:TagPrefix( " WYN " , " TextBoxCalendar " )]
 12 None.gif namespace  WYN.WebControls
 13 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 14InBlock.gif    [ToolboxData("<{0}:TextBoxCalendar runat=server></{0}:TextBoxCalendar>")]
 15InBlock.gif    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
 16InBlock.gif    public class TextBoxCalendar :TextBox
 17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 18InBlock.gif
 19InBlock.gif        [Bindable(true)]
 20InBlock.gif        [Category("Appearance")]
 21InBlock.gif        [DefaultValue("")]
 22InBlock.gif        [Localizable(true)]
 23InBlock.gif        [Description("控件选定的短日期字符串格式")]
 24InBlock.gif        public string ShortDate
 25ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 26InBlock.gif            get
 27ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 28InBlock.gif                if (String.IsNullOrEmpty(Text))
 29InBlock.gif                    base.Text = System.DateTime.Now.Date.ToShortDateString();
 30InBlock.gif
 31InBlock.gif                return base.Text;
 32ExpandedSubBlockEnd.gif            }

 33InBlock.gif
 34InBlock.gif            set
 35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 36InBlock.gif                base.Text = value;
 37ExpandedSubBlockEnd.gif            }

 38ExpandedSubBlockEnd.gif        }

 39InBlock.gif
 40InBlock.gif        [Bindable(true)]
 41InBlock.gif        [Category("Appearance")]
 42InBlock.gif        [DefaultValue("")]
 43InBlock.gif        [Localizable(true)]
 44InBlock.gif        [Description("控件选定的日期")]
 45InBlock.gif        public DateTime SelectedDate
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            get
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 49InBlock.gif                if (String.IsNullOrEmpty(Text))
 50InBlock.gif                    base.Text = System.DateTime.Now.Date.ToShortDateString();
 51InBlock.gif
 52InBlock.gif                return Convert.ToDateTime(base.Text);
 53ExpandedSubBlockEnd.gif            }

 54InBlock.gif            set
 55ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 56InBlock.gif                base.Text = value.ToShortDateString();
 57ExpandedSubBlockEnd.gif            }

 58ExpandedSubBlockEnd.gif        }

 59InBlock.gif
 60InBlock.gif        [Browsable(false)]
 61InBlock.gif        public override string Text
 62ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 63InBlock.gif            get
 64ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 65InBlock.gif                return base.Text;
 66ExpandedSubBlockEnd.gif            }

 67InBlock.gif            set
 68ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 69InBlock.gif                ;
 70ExpandedSubBlockEnd.gif            }

 71ExpandedSubBlockEnd.gif        }

 72InBlock.gif
 73InBlock.gif        [Browsable(false)]
 74InBlock.gif        public override TextBoxMode TextMode
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 76InBlock.gif            get
 77ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 78InBlock.gif                return base.TextMode;
 79ExpandedSubBlockEnd.gif            }

 80InBlock.gif            set
 81ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 82InBlock.gif                ;
 83ExpandedSubBlockEnd.gif            }

 84ExpandedSubBlockEnd.gif        }

 85InBlock.gif
 86InBlock.gif        [Browsable(false)]
 87InBlock.gif        public override bool ReadOnly
 88ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 89InBlock.gif            get
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 91InBlock.gif                return true;
 92ExpandedSubBlockEnd.gif            }

 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif
 95InBlock.gif        protected override void OnLoad(EventArgs e)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 97InBlock.gif            // Define the resource name and type.
 98InBlock.gif            String rsname = "WYN.WebControls.script_PopupCalender.js";
 99InBlock.gif            Type rstype = this.GetType();
100InBlock.gif
101InBlock.gif            // Get a ClientScriptManager reference from the Page class.
102InBlock.gif            ClientScriptManager cs = Page.ClientScript;
103InBlock.gif       
104InBlock.gif            // Register the client resource with the page.
105InBlock.gif            cs.RegisterClientScriptResource(rstype, rsname);
106InBlock.gif
107InBlock.gif
108InBlock.gif            base.OnLoad(e);
109ExpandedSubBlockEnd.gif        }

110InBlock.gif
111InBlock.gif        protected override void Render(HtmlTextWriter writer)
112ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
113InBlock.gif            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "PopupCalender();");
114InBlock.gif            base.Render(writer);
115ExpandedSubBlockEnd.gif        }

116ExpandedSubBlockEnd.gif    }

117ExpandedBlockEnd.gif}

118 None.gif

JS文件如下
None.gif var  oPopup  =  window.createPopup();
None.gif
var  oSrc;
None.gif
None.gif
var  theDate;  
None.gif
var  theYear;
None.gif
var  theMonth;
None.gif
None.giftheDate 
=   new  Date();
None.gif
None.gif
function  PopupCalender()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
InBlock.gif    
var s="<div style='background-color:#F5F5F5;width:310;padding:5px;'>" +
InBlock.gif    
"<table style='font-size:9pt' cellspacing='0' cellpadding='0' bgcolor='#ffffff' style='margin-top:0px' width='300'>" +
InBlock.gif    
"<tr><td><input id='b1' type='button' value='上一年' style='font-size:9pt'></td>" +
InBlock.gif    
"<td><input id='b2' type='button' value='下一年' style='font-size:9pt'></td>" +
InBlock.gif    
"<td width='60'><input name='theDate' type='text' value='' size='12' readonly style='font-size:9pt'></td>" +
InBlock.gif    
"<td><input id='b3' type='button' value='上一月' style='font-size:9pt'></td>"+
InBlock.gif    
"<td><input id='b4' type='button' value='下一月' style='font-size:9pt'></td></tr></table>" +
InBlock.gif    
"<table style='font-size:9pt' border='1' cellspacing='1' cellpadding='1' bordercolor='#cccccc' id='C' bgcolor='#ffffff' style='margin-top:0px' width='300'>" +
InBlock.gif    
"<tr style='background-color:#f0eada'>" +
InBlock.gif    
"<td height='26'><font color='red'>星期日</font></td>" +
InBlock.gif    
"<td>星期一</td><td>星期二</td><td>星期三</td><td>星期四</td><td>星期五</td><td><font color='blue'>星期六</font></td>" +
InBlock.gif    
"</tr></table></div>"
InBlock.gif
InBlock.gif    oPopup.document.body.innerHTML 
= s;
InBlock.gif
InBlock.gif    oPopup.document.all.b1.onclick 
= pyear;
InBlock.gif    oPopup.document.all.b2.onclick 
= nyear;
InBlock.gif    oPopup.document.all.b3.onclick 
= pmonth;
InBlock.gif    oPopup.document.all.b4.onclick 
= nmonth;
InBlock.gif    
//oPopup.document.all.C.οnclick=setTheDate;
InBlock.gif

InBlock.gif    oSrc 
= window.event.srcElement; 
InBlock.gif    theYear 
= theDate.getFullYear();
InBlock.gif    theMonth 
= theDate.getMonth();
InBlock.gif
InBlock.gif    displayC(
0);
InBlock.gif    oPopup.show(
0024310176, oSrc);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
None.gif
function  pyear()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    theYear
--;    displayC(0);
ExpandedBlockEnd.gif}

None.gif
None.gif
function  nyear()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    theYear
++;    displayC(0);
ExpandedBlockEnd.gif}

None.gif
None.gif
function  pmonth()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    theMonth
--;    displayC(0);
ExpandedBlockEnd.gif}

None.gif
None.gif
function  nmonth()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    theMonth
++;    displayC(0);
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
function  getLastDay()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var lyear = theDate.getFullYear();
InBlock.gif    
var lmonth = theDate.getMonth() + 1;
InBlock.gif
InBlock.gif    
switch(lmonth)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
case 1:
InBlock.gif        
case 3:
InBlock.gif        
case 5:
InBlock.gif        
case 7:
InBlock.gif        
case 8:
InBlock.gif        
case 10:
InBlock.gif        
case 12:  return 31break;
InBlock.gif        
case 4:
InBlock.gif        
case 6:
InBlock.gif        
case 9:
InBlock.gif        
case 11:  return 30break;
InBlock.gif        
case 2:   
InBlock.gif            
if((lyear % 4)!=0)  return 28;
InBlock.gif            
if((lyear % 100)!=0)  return 29;
InBlock.gif            
return 28;        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
function  getWeekDay()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var lday = theDate.getDate();
InBlock.gif    theDate.setDate(
1);
InBlock.gif    
var lweekday = theDate.getDay();
InBlock.gif    theDate.setDate(lday);
InBlock.gif    
return lweekday;
ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif
function  displayC(ldate)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(theMonth<0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        theMonth 
= 11;    theYear--;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if(theMonth>11)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        theMonth 
= 0;    theYear++;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
if(theYear<1000) theYear=1000;
InBlock.gif    
if(ldate==0) ldate = theDate.getDate();
InBlock.gif
InBlock.gif    theDate.setDate(
1);
InBlock.gif    theDate.setYear(theYear);
InBlock.gif    theDate.setMonth(theMonth);
InBlock.gif
InBlock.gif    
var lastday = getLastDay();
InBlock.gif    
var weekday = getWeekDay();
InBlock.gif
InBlock.gif    
if(ldate>lastday)
InBlock.gif        theDate.setDate(lastday);
InBlock.gif    
else
InBlock.gif        theDate.setDate(ldate);
InBlock.gif    
InBlock.gif    
var objc = oPopup.document.all.C;
InBlock.gif    
for(var i=objc.rows.length-1;i>0;i--)
InBlock.gif        objc.deleteRow(i);
InBlock.gif
InBlock.gif    
for(var i=0; i<42; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var col = i % 7;
InBlock.gif        
var row = (i - col) / 7;
InBlock.gif        
var d = i- weekday + 1;
InBlock.gif        
//if(col == 0 && d> lastday) break;
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif        
if(col ==0 ) dot.gifvar r = objc.insertRow(); r.style.cursor= "hand"; r.style.textAlign="center"; }
InBlock.gif        c 
= r.insertCell();
InBlock.gif        
if(d>0 && d<=lastday)
InBlock.gif            c.innerText 
= d.toString();
InBlock.gif        
else
InBlock.gif            c.innerText 
= " ";
InBlock.gif
InBlock.gif        c.attachEvent(
"onclick",setTheDate);
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    showDay(objc, weekday);
ExpandedBlockEnd.gif}

None.gif
None.gif
function  showDay(objc, weekday)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var lday = theDate.getDate() + weekday - 1;
InBlock.gif    
var col = lday % 7;
InBlock.gif    
var row = (lday - col) / 7 + 1;
InBlock.gif    objc.rows[row].cells[col].style.backgroundColor 
= "#999999";
InBlock.gif    objc.rows[row].cells[col].style.color 
= "#ffffff";
InBlock.gif            
InBlock.gif    oPopup.document.all.theDate.value 
= theDate.getFullYear().toString() + "-" 
InBlock.gif        
+ (theDate.getMonth() + 1).toString() + "-" 
InBlock.gif        
+ theDate.getDate().toString();
InBlock.gif    
ExpandedBlockEnd.gif}

None.gif
None.gif
function  setTheDate(ev)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var obj = ev.srcElement;
InBlock.gif    
var lday = parseInt(obj.innerText);
InBlock.gif
InBlock.gif    
if(isNaN(lday)) return;
InBlock.gif    displayC(lday);
InBlock.gif
InBlock.gif    oSrc.value 
= oPopup.document.all.theDate.value;
InBlock.gif    oPopup.hide();
ExpandedBlockEnd.gif}

有一个地方要特别说明一下:
   由于不想让使用者见到JavaScript文件,不想让他们手工设JS路径,所以代码用到了2.0里的一个新东西
在Namespace上打上如下Attribt
[assembly: WebResource("WYN.WebControls.script_PopupCalender.js", "application/x-javascript", PerformSubstitution = true)]

注册JS的方法如下:
None.gif   protected   override   void  OnLoad(EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
// Define the resource name and type.
InBlock.gif
            String rsname = "WYN.WebControls.script_PopupCalender.js";
InBlock.gif            Type rstype 
= this.GetType();
InBlock.gif
InBlock.gif            
// Get a ClientScriptManager reference from the Page class.
InBlock.gif
            ClientScriptManager cs = Page.ClientScript;
InBlock.gif       
InBlock.gif            
// Register the client resource with the page.
InBlock.gif
            cs.RegisterClientScriptResource(rstype, rsname);
InBlock.gif
InBlock.gif
InBlock.gif            
base.OnLoad(e);
ExpandedBlockEnd.gif        }

然后在JS文件右击,选属性,生成操作(Build Action) 设为嵌入的资源

收工!可以用了

转载于:https://www.cnblogs.com/listhome/archive/2006/11/03/549100.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值