现在的位置: 首页 > 综合 > 正文

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

2013年04月08日 ⁄ 综合 ⁄ 共 4351字 ⁄ 字号 评论关闭
将日常使用的日期选择用户控件进行了封装,是我写的第一个服务器控件,希望大家给些意见。
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)

抱歉!评论已关闭.