Example 1: This control renders an input control with a name equal to custom controls UniqueID property.
public class CustomTextBox : WebControl, IPostBackDataHandler { public bool LoadPostData(string postDataKey, NameValueCollection postCollection) { /// /// automatically invoked as we have a /// html input control with a name equal to /// web controls UniqueID /// string text = postCollection[postDataKey]; return true; } public void RaisePostDataChangedEvent() { } protected override void RenderContents(HtmlTextWriter writer) { writer.Write(string.Format("<input type=\"input\" name=\"{0}\" id=\"{1}\" />", this.UniqueID, this.ClientID)); } }Example 2: This control does not render an input control with a name equal to custom controls UniqueID property. Thus we need RegisterRequiresPostBack call in the load method with the parameter of self.
public class ComplexControl : WebControl, IPostBackDataHandler { public bool LoadPostData(string postDataKey, NameValueCollection postCollection) { /// /// will NOT get automatically invoked /// because we dont have input control with name /// equal to web controls UniqueID /// THUS we need to RegisterRequiresPostBack call /// string name = postCollection[this.UniqueID + "$Name"]; string age = postCollection[this.UniqueID + "$Age"]; return true; } public void RaisePostDataChangedEvent() { } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.Page.RegisterRequiresPostBack(this); } protected override void RenderContents(HtmlTextWriter writer) { writer.Write("<div>"); writer.Write(string.Format("Name: <input type=\"input\" name=\"{0}\" id=\"{1}\" />", this.UniqueID + "$Name", this.ClientID + "_Name")); writer.Write(string.Format("Age: <input type=\"input\" name=\"{0}\" id=\"{1}\" />", this.UniqueID + "$Age", this.ClientID + "_Age")); writer.Write("</div>"); } }
No comments:
Post a Comment