简单自定义控件
简单自定义控件
decalre SelectedIndexChanged event when create children.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace com.csr.rd.jms.webcontrol
{
/**//// <summary>
/// Summary description for UC2.
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:UC2 runat=server></{0}:UC2>")]
public class UC2 : System.Web.UI.WebControls.WebControl,INamingContainer,System.Web.UI.IPostBackDataHandler
{
private DropDownList F1 = new DropDownList();
private DropDownList S1 = new DropDownList();
private Label lblTime = new Label();
[Bindable(true),Category("Appearance"), DefaultValue("")]
private void InitDropdownList()
{
F1.Width = 70;
S1.Width = 70;
F1.BackColor = System.Drawing.Color.Azure;
S1.BackColor = System.Drawing.Color.Beige;
}
public int Index
{
get
{
return (ViewState["index"] == null)? 0 : int.Parse(ViewState["index"].ToString()) ;
}
set
{
ViewState["index"] = value;
}
}
private void addItems()
{
F1.Items.Add("1");
F1.Items.Add("2");
S1.Items.Add("fIRST");
S1.Items.Add("Second");
}
protected override void CreateChildControls()
{
F1.AutoPostBack = true;
S1.AutoPostBack = true;
F1.SelectedIndexChanged += new EventHandler(this.F1_SelectedIndexChanged);
F1.SelectedIndex = Index;
addItems();
this.Controls.Add(S1);
this.Controls.Add(F1);
}
protected override void OnInit(EventArgs e)
{
//addItems();
base.OnInit (e);
}
protected override void Render(HtmlTextWriter output)
{
// if(!this.Page.IsPostBack)
// {
//addItems();
// }
InitDropdownList();
this.Attributes.AddAttributes(output);
output.AddAttribute(HtmlTextWriterAttribute.Width,"200px");
output.RenderBeginTag(HtmlTextWriterTag.Table);
output.RenderBeginTag(HtmlTextWriterTag.Tr);
output.RenderBeginTag(HtmlTextWriterTag.Td);
F1.RenderControl(output);
S1.RenderControl(output);
output.RenderEndTag();
output.RenderEndTag();
output.RenderEndTag();
//output.Write(Text);
}
private void F1_SelectedIndexChanged(object sender, EventArgs e)
{
S1.SelectedIndex = F1.SelectedIndex;
ViewState["index"] = F1.SelectedIndex.ToString();
}
public void RaisePostDataChangedEvent()
{
// TODO: Add CommonCalendar.RaisePostDataChangedEvent implementation
}
public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
return true;
}
}
}