編寫C#日期時(shí)間控件的幾種方法
參照用JavaScript編寫的日期選擇,將其中的JavaScript部分寫入控件中就可以了。C#日期時(shí)間控件代碼不復(fù)雜,拿出來大家共享以下。大家?guī)兔φ艺褺ug,提提改進(jìn)意見。
實(shí)現(xiàn)效果如下:
Bug:控件放到frame 的頁面里面使用得時(shí)候就會(huì)出錯(cuò):“parent.event 為空或不是對(duì)象”
將InputCalendar.cs中output.WriteLine("parent.event.cancelBubble=true;");一句去掉即可。
這一句的意義是:設(shè)置或獲取當(dāng)前事件是否要在事件句柄中向上冒泡。當(dāng)前應(yīng)用中好像沒有用,所以去掉也不要緊。
如何增加Autopostpack功能:
- if (autopostback==true )
- {
- output.WriteLine ("javascript:"+Page.GetPostBackEventReference(this)+";");
- }
- //添加了一個(gè)AUTOPOSTBACK功能。
在頭部加上如下代碼,即可實(shí)現(xiàn)選擇Autopostpack功能了。
- private bool autopostback=true;
- [Bindable(true),
- Category("Appearance"),
- DefaultValue(true)]
- public bool AutoPostBack
- {
- get
- {
- return autopostback;
- }
- set
- {
- autopostback=value;
- }
- }
解決在用戶C#日期時(shí)間控件控件中取值問題:
將原來的C#日期時(shí)間控件:
- output.AddAttribute(HtmlTextWriterAttribute.Id,this.ID);
- output.AddAttribute(HtmlTextWriterAttribute.Name,this.ID);
改為:
- output.AddAttribute(HtmlTextWriterAttribute.Id,this.UniqueID);
- output.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
將原來的C#日期時(shí)間控件:
- output.AddAttribute(HtmlTextWriterAttribute.Id,this.ID); output.AddAttribute(HtmlTextWriterAttribute.Name,this.ID); 改為: output.AddAttribute(HtmlTextWriterAttribute.Id,this.UniqueID); output.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
【編輯推薦】