Sunday, October 17, 2010

Page RegisterRequiresPostBack method.

When you implement a custom control, only if you are implementing IPostBackDataHandler, the LoadPostData method get automatically get called only if you add an input control with the same name as the custom control’s UniqueID. If you don’t want to add a input control with a name equal to custom controls UniqueID property, then you can explicitly register custom control to invoke LoadPostData method by registering with RegisterRequiresPostBack(...) method.
Example 1: This control renders an input control with a name equal to custom controls UniqueID property.
public class CustomTextBox : WebControlIPostBackDataHandler
{
    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 : WebControlIPostBackDataHandler
{
    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:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...