locked
How to Override LoadPostData RRS feed

  • Question

  • User764522383 posted

    Im overriding a RadioButton , but here is the problem I can not have base Properties in my codeBehind & also can not have the checked property its alwasys true,

    problem is from LoadPostData how should I override this.

     

    namespace CRS.Web.UI.WebControls
    {
    	
        [ToolboxData("&lt;{0}:GroupRadioButton runat=server></{0}:GroupRadioButton>")]
        public class GroupRadioButton : RadioButton, IPostBackDataHandler
        {
            private string tmpID;
    
            public GroupRadioButton()
                : base()
            {
    
            }
            public GroupRadioButton(string RadioButtonValue, string RadioButtonText, System.Drawing.Color TextForeColor)
            {
                this.Text = RadioButtonText;
                this.ForeColor = TextForeColor;
                this.RadioButtonValue = RadioButtonValue;
                this.Checked = Checked;
            }
            public GroupRadioButton(string RadioButtonValue, string RadioButtonText)
            {
                this.Text = RadioButtonText;
                this.RadioButtonValue = RadioButtonValue;
                this.Checked = Checked;
    
            }
            #region Properties
    
            private string RadioButtonValue_;
            public string RadioButtonValue
            {
                get { return RadioButtonValue_; }
                set { RadioButtonValue_ = value; }
            }
            private string RadioID
            {
                get
                {
                    string id = Attributes["id"];
                    if (id == null)
                        id = UniqueID;
                    else
                        id = UniqueID + "_" + id;
                    return id;
                }
            }
            #endregion
    
            #region Rendering
    
            protected override void Render(HtmlTextWriter output)
            {
                RenderInputTag(output);
            }
    
            private void RenderInputTag(HtmlTextWriter htw)
            {
                /*************************************************************************/
                this.tmpID = this.UniqueID;
                this.tmpID = this.tmpID.Replace("$", "_");
                this.ID = this.tmpID;
                /*************************************************************************/
    
                htw.Write(string.Format("&lt;span style='color:{0};'>", this.ForeColor.Name));
                htw.AddAttribute(HtmlTextWriterAttribute.Id, this.ID);
                htw.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
                htw.AddAttribute(HtmlTextWriterAttribute.Name, GroupName);
                htw.AddAttribute(HtmlTextWriterAttribute.Value, this.RadioButtonValue);
    
                if (Checked)
                    htw.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
                if (!Enabled)
                    htw.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
    
                string onClick = Attributes["onclick"];
                if (AutoPostBack)
                {
                    if (onClick != null)
                        onClick = String.Empty;
                    onClick += Page.GetPostBackClientEvent(this, String.Empty);
                    htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
                    htw.AddAttribute("language", "javascript");
                }
                else
                {
                    if (onClick != null)
                        htw.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
                }
    
                if (AccessKey.Length > 0)
                    htw.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);
                if (TabIndex != 0)
                    htw.AddAttribute(HtmlTextWriterAttribute.Tabindex,
                        TabIndex.ToString(NumberFormatInfo.InvariantInfo));
                htw.RenderBeginTag(HtmlTextWriterTag.Input);
                htw.RenderEndTag();
                //Add Text Property For RadioButton
                htw.Write(string.Format("&lt;label for='{0}'>{1}</label>", this.ID, this.Text));
                //End Span tag for ForeColor
                htw.Write("&lt;/span>");
    
            }
    
            #endregion
    
            #region IPostBackDataHandler Members
    
            void IPostBackDataHandler.RaisePostDataChangedEvent()
            {
                OnCheckedChanged(EventArgs.Empty);
            }
    
            bool IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
            {
                bool result = false;
                string ControlID = postCollection[GroupName];
                if ((ControlID != null) && (ControlID == RadioID))
                {
                    if (!this.Checked)
                    {
                        this.Checked = true;
                        result = true;
                    }
                }
                else
                {
                    if (this.Checked)
                        this.Checked = false;
                }
                return result;
            }
    
            #endregion
        }
    }
     
     
     
    Sunday, June 15, 2008 11:15 PM

Answers

  • User-1136466523 posted

    Hi,

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p> </o:p>

    From your description, it seems that the LoadPostData event doesn’t fired during the postback, so you cannot get the page item’s data.

    <o:p> </o:p>

    In order to make the loadPostData event fired in your custom server control, you have to involve a control whose ID attribute equals with this.UniqueID.

    <o:p> </o:p>

    Generally, we can add a hidden filed in our custom server control, see:

    <o:p> </o:p>writer.Write("<input type='hidden' name='" + this.UniqueID + "'>")<o:p></o:p><o:p> </o:p>

    Or what you can do is to set the name attribute of Sumbit to this.UniqueID .

    <o:p> </o:p>

    Only througn those ways can the LoadPostData event be fired in our custom server control.

    <o:p> </o:p>

    Thanks.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 18, 2008 2:41 AM