读书人

asp.net2.0 - web自定义控件的智能标记

发布时间: 2012-03-21 13:33:15 作者: rapoo

asp.net2.0 - web自定义控件的智能标记问题
这两天写了个web自定义控件,控件由一个TextBox、一个Button、一个Calendar构成。但不知道怎么给加个智能标记,智能标记是用来选择Calendar样式的。希望得到指点,谢谢!
下面是控件代码:
[Designer(typeof(DateSelectDesignerActionList))]
[DefaultProperty( "EnableMultiSelect ")]
[ToolboxBitmap(typeof(DateTextBox), "ToolIcon.DateSelect.bmp ")]
[ToolboxData( " <{0}:DateSelect runat=server> </{0}:DateSelect> ")]
public class DateSelect : WebControl, INamingContainer, IComponent
{
private TextBox txtDateTime;
private Button btnSelect;
private Calendar calDateTime;

public DateSelect()
{
InitializeComponent();
}

public void InitializeComponent()
{
CreateComponent();
SetComponentProperty();
SetComponentEvent();
}

public void CreateComponent()
{
// Create Control
txtDateTime = new TextBox();
btnSelect = new Button();
calDateTime = new Calendar();

// Nest Control
Controls.Add(txtDateTime);
Controls.Add(btnSelect);
Controls.Add(calDateTime);
}

public void SetComponentProperty()
{
btnSelect.Text = "选择 ";
calDateTime.Visible = false;

calDateTime.Font.Name = "Verdana ";


calDateTime.Font.Size = 8;
calDateTime.Style.Add( "position ", "absolute ");
calDateTime.Style[ "top "] = txtDateTime.Style[ "top "];
calDateTime.Style[ "left "] = txtDateTime.Style[ "left "];
}

public void SetComponentEvent()
{
btnSelect.Click += new EventHandler(btnSelect_Click);
calDateTime.SelectionChanged += new EventHandler(calDateTime_SelectionChanged);
calDateTime.Load += new EventHandler(calDateTime_Load);
}

public void btnSelect_Click(object sender, EventArgs e)
{
if (EnableMultiSelect)
{
if (btnSelect.Text == "选择 ")
{
btnSelect.Text = "完成 ";
calDateTime.Visible = true;
}
else
{
btnSelect.Text = "选择 ";
calDateTime.Visible = false; ;
}
}
else
{


calDateTime.Visible = true; ;
}
}

public void calDateTime_SelectionChanged(object sender, EventArgs e)
{
string selectedDates = txtDateTime.Text;
if (EnableMultiSelect)
{
string s = string.Empty;
SelectedDatesCollection sdc = calDateTime.SelectedDates;
foreach (DateTime dt in sdc)
{
s = dt.ToString( "yyyy-MM-dd ");
if (selectedDates.IndexOf(s) == -1)
{
selectedDates += ", " + s;
calDateTime.SelectedDates.Add(Convert.ToDateTime(s));
}
//else
//{
//int i = selectedDates.IndexOf(s);
//selectedDates = selectedDates.Remove(i, i + s.Length);
//calDateTime.SelectedDates.Remove(Convert.ToDateTime(s));


//}
}
selectedDates = selectedDates.Trim( ', ');
}
else
{
selectedDates = calDateTime.SelectedDate.ToString( "yyyy-MM-dd ");
calDateTime.Visible = false;
}
Text = selectedDates;
txtDateTime.Text = selectedDates;
}

public void calDateTime_Load(object sender, EventArgs e)
{
if (Text != string.Empty)
{
string[] selectedDates = Text.Split( ', ');
foreach (string s in selectedDates)
{
calDateTime.SelectedDates.Add(Convert.ToDateTime(s));
}
}
}


#region Single Or Multiple

[Bindable(true)]
[DefaultValue( "False ")]
[DescriptionAttribute( "单个和多个日期选择。 ")]
[Category( "日历-杂项 ")]
[Localizable(true)]
public bool EnableMultiSelect


{
get { return ((ViewState[ "EnableMultiSelect "] == null) ? false : (bool)ViewState[ "EnableMultiSelect "]); }
set { ViewState[ "EnableMultiSelect "] = value; }
}

#endregion

#region Visable For Select Button

[Bindable(true)]
[DefaultValue( "Ture ")]
[DescriptionAttribute( "选择按钮是否可见并呈现出来。 ")]
[Category( "日历-杂项 ")]
[Localizable(true)]
public bool VisableSelectButton
{
get { return ((ViewState[ "VisableSelectButton "] == null) ? false : (bool)ViewState[ "VisableSelectButton "]); }
set { ViewState[ "VisableSelectButton "] = value; }
}

#endregion

#region TextBox Text

[Bindable(true)]
[Category( "Appearance ")]
[DefaultValue( " ")]
[DescriptionAttribute( "选择的日期。(多个以逗号隔开) ")]
[Localizable(true)]
public string Text
{
get { return ((ViewState[ "Text "] == null) ? String.Empty : (String)ViewState[ "Text "]); }
set { ViewState[ "Text "] = value; }
}

#endregion

#region TextBox Width

[Bindable(true)]
[Category( "Appearance ")]
[DescriptionAttribute( "控件的宽度。 ")]


[DefaultValue( " ")]
[Localizable(true)]
public override Unit Width
{
get { return txtDateTime.Width; }
set { txtDateTime.Width = value; }
}

#endregion

#region TextBox Height

[Bindable(true)]
[Category( "Appearance ")]
[DescriptionAttribute( "控件的高度。 ")]
[DefaultValue( " ")]
[Localizable(true)]
public override Unit Height
{
get { return txtDateTime.Height; }
set { txtDateTime.Height = value; }
}

#endregion

#region TextBox BackColor

public override Color BackColor
{
get { return txtDateTime.BackColor; }
set { txtDateTime.BackColor = value; }
}

#endregion





protected override void RenderContents(HtmlTextWriter output)
{
txtDateTime.RenderControl(output);
btnSelect.RenderControl(output);
calDateTime.RenderControl(output);
}

}
智能标记代码,存在问题,有点迷糊了:
using System;
using System.ComponentModel.Design;

public class DateSelectDesignerActionList : DesignerActionList


{
public DateSelectDesignerActionList(DateSelect ds)
: base(ds)
{
AutoShow = true;
}

public override DesignerActionItemCollection GetSortedActionItems()
{
DesignerActionItemCollection actionItems = new DesignerActionItemCollection();
actionItems.Add(
new DesignerActionMethodItem(this, "自动套用格式 ", "Calendar 任务 ", true)
);

return actionItems;
}
}


[解决办法]
http://www.cnblogs.com/mapserver/articles/360569.html

读书人网 >.NET

热点推荐