ASP.NET常用控件之1:用于选择日期的TextBox控件

将日常使用的日期选择用户控件进行了封装,是我写的第一个服务器控件,希望大家给些意见。
using  System;
using  System.ComponentModel;
using  System.ComponentModel.Design;
using  System.Web.UI;
using  System.Web.UI.WebControls;

namespace  Sdtcn.WebControls
{
    [
    Designer(
typeof(Design.CompositeControlDesigner)) 
    ]
    
public abstract class CompositeControl : 
        WebControl, INamingContainer 
    
{
        
public override ControlCollection Controls 
        
{
            
get 
            
{
                EnsureChildControls();
                
return base.Controls;
            }

        }

    }

}

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace Sdtcn.WebControls
{
    
/**//// <summary>
    
/// CustomCalendar 的摘要说明。
    
/// </summary>

    public class CustomCalendar : CompositeControl
    
{
        
private TextBox            _calendarTextBox;
        
private HtmlInputButton _selectInputButton;
        
private Calendar        _calendar;

        
private static readonly object EventSelectChanged = new object();

        
private string display
        
{
            
get
            
{
                
if(ViewState["Display"== null)
                    
return "none";
                
else
                    
return ViewState["Display"].ToString();
            }

            
set
            
{
                ViewState[
"Display"= value;
            }

        }


        代理子控件的属性
代理子控件的属性

        事件
事件

        
        
private void _calendar_SelectionChanged(object sender, EventArgs e) 
        
{
            _calendarTextBox.Text 
= _calendar.SelectedDate.ToShortDateString();

            System.Web.UI.Control div 
= Page.FindControl("divCalendar");

            
if(div is HtmlGenericControl)
            
{
                ((HtmlGenericControl)div).Style.Add(
"display","none");
            }


            OnSelectChanged(sender,e);

            display 
= "none";
        }


        
private void _calendar_VisibleMonthChanged(Object sender, MonthChangedEventArgs e)
        
{
            display 
= "block";
        }


        
public CustomCalendar()
        
{}

        
protected override void CreateChildControls()
        
{
            Controls.Clear();

            _calendarTextBox 
= new TextBox();
            _calendarTextBox.ID 
= "calendarTextBox";
            _calendarTextBox.ReadOnly 
= true;

            _selectInputButton 
= new HtmlInputButton("button");
            _selectInputButton.Attributes.Add(
"Onclick","OnClick()");
            
            _calendar 
= new Calendar();
            _calendar.SelectionChanged 
+= new EventHandler(_calendar_SelectionChanged);
            _calendar.VisibleMonthChanged 
+= new MonthChangedEventHandler(_calendar_VisibleMonthChanged);

            
this.Controls.Add(_calendarTextBox);
            
this.Controls.Add(_selectInputButton);
            
this.Controls.Add(_calendar);
        }


        
protected override void OnPreRender(EventArgs e)
        
{
            
base.OnPreRender (e);

            
string script = "<script language=JavaScript> function OnClick(){  if( divCalendar.style.display == /"none/")    divCalendar.style.display = /"/";  else    divCalendar.style.display = /"none/";}</script>";
            Page.RegisterClientScriptBlock(
"script",script);
        }


        
protected override void Render(System.Web.UI.HtmlTextWriter writer)
        
{
            AddAttributesToRender(writer);

            _calendarTextBox.RenderControl(writer);
            _selectInputButton.RenderControl(writer);
            
            writer.Write(
"<br>");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.AddAttribute(HtmlTextWriterAttribute.Id,
"divCalendar");
            writer.AddAttribute(HtmlTextWriterAttribute.Style,
"DISPLAY:" + display + ";POSITION:absolute");
            _calendar.RenderControl(writer);
            writer.RenderEndTag();
        }


    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值