关闭

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

标签: textboxasp.netcalendarobjectstringsystem
2085人阅读 评论(0) 收藏 举报
分类:
将日常使用的日期选择用户控件进行了封装,是我写的第一个服务器控件,希望大家给些意见。
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();
        }


    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2259858次
    • 积分:25835
    • 等级:
    • 排名:第228名
    • 原创:1篇
    • 转载:1662篇
    • 译文:0篇
    • 评论:98条
    最新评论