Newbie IPostBackDataHandler Question

Discussion in 'ASP .Net Building Controls' started by Solel Software, Dec 22, 2004.

  1. Hi,

    I am having a problem with a custom server control I'm working on and put
    together a skeleton example to demonstrate the problem. Basically, I have a
    custom server control with two RadioButton controls added in
    CreateChildControls. They work great and all is well except for some reason
    their information is not available in IPostBackDataHandler.LoadPostData.
    I've gone through the ViewState and the postCollection argument to
    LoadPostData but haven't found the radio buttons' information there either.
    Is there something special that I have to do? For some reason, the
    information is not being stored in ViewState so it is not a LoadViewState
    issue (or maybe it is?). I _am_ able to access a TextBox and DropDownList's
    information in IPostBackDataHandler.LoadPostData so it seems to affect only
    RadioButton controls. The code for the bare-bones server control to
    illustrate the problem is below. Any help would be appreciated! Thanks so
    much!

    --
    Sincerely,

    Mark Fox

    public class TestRadio : System.Web.UI.Control, IPostBackDataHandler,
    INamingContainer
    {
    public TestRadio() : base() {}

    #region Implementation of IPostBackDataHandler
    void IPostBackDataHandler.RaisePostDataChangedEvent()
    {
    }

    bool IPostBackDataHandler.LoadPostData(string postDataKey,
    System.Collections.Specialized.NameValueCollection postCollection)
    {
    // HERE IS THE PROBLEM: both the RadioButtons have Checked == false
    // on postback even when one is selected
    RadioButton rb1 = (RadioButton) Controls[0];
    RadioButton rb2 = (RadioButton) Controls[1];

    return false;
    }
    #endregion

    #region Render Overrides
    protected override void CreateChildControls()
    {
    #region rb1
    _rb1 = new RadioButton();
    _rb1.GroupName = "DateSet";
    _rb1.CssClass = "Normal";
    _rb1.Text = "1";
    this.Controls.Add(_rb1);
    #endregion rb1

    #region rb2
    _rb2 = new RadioButton();
    _rb2.GroupName = "DateSet";
    _rb2.CssClass = "Normal";
    _rb2.Text = "2";
    this.Controls.Add(_rb2);
    #endregion rb2
    }

    protected override void Render( HtmlTextWriter writer )
    {
    this.EnsureChildControls();
    base.Render(writer);
    writer.AddAttribute(HtmlTextWriterAttribute.Type, "Hidden");
    writer.AddAttribute(HtmlTextWriterAttribute.Value, "");
    writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
    writer.RenderBeginTag(HtmlTextWriterTag.Input);
    writer.RenderEndTag();
    }
    #endregion

    protected internal RadioButton rb1
    {
    get { EnsureChildControls(); return _rb1; }
    }
    protected internal RadioButton rb2
    {
    get { EnsureChildControls(); return _rb2; }
    }
    private RadioButton _rb1;
    private RadioButton _rb2;
    }
     
    Solel Software, Dec 22, 2004
    #1
    1. Advertisements

  2. I figured this out. You don't have to reply, but thanks for looking!

    It turns out the info is posted back in the postCollection just not in a
    normal format (and for some reason it isn't in ViewState and is not loaded up
    by ASP.NET). It has to be parsed. The value in

    postCollection[this.UniqueID + ":" + this.GroupName]

    is the UniqueID of the radio button that is selected. So I added the
    following to IPostBackDataHandler.LoadPostData

    string postCollectionRadioButtonKey = this.UniqueID + ":" + this.GroupName;
    string postedValue = postCollection[postCollectionRadioButtonKey];
    if(postedValue != null)
    {
    foreach(Control thisControl in Controls)
    {
    if(thisControl.UniqueID == postedValue)
    {
    // this is the radio button that is checked,
    // the ID is null so you have to check another
    // property such as
    // ((RadioButton) thisControl).Text
    }
    }
    }




    "Solel Software" wrote:

    > Hi,
    >
    > I am having a problem with a custom server control I'm working on and put
    > together a skeleton example to demonstrate the problem. Basically, I have a
    > custom server control with two RadioButton controls added in
    > CreateChildControls. They work great and all is well except for some reason
    > their information is not available in IPostBackDataHandler.LoadPostData.
    > I've gone through the ViewState and the postCollection argument to
    > LoadPostData but haven't found the radio buttons' information there either.
    > Is there something special that I have to do? For some reason, the
    > information is not being stored in ViewState so it is not a LoadViewState
    > issue (or maybe it is?). I _am_ able to access a TextBox and DropDownList's
    > information in IPostBackDataHandler.LoadPostData so it seems to affect only
    > RadioButton controls. The code for the bare-bones server control to
    > illustrate the problem is below. Any help would be appreciated! Thanks so
    > much!
    >
    > --
    > Sincerely,
    >
    > Mark Fox
    >
    > public class TestRadio : System.Web.UI.Control, IPostBackDataHandler,
    > INamingContainer
    > {
    > public TestRadio() : base() {}
    >
    > #region Implementation of IPostBackDataHandler
    > void IPostBackDataHandler.RaisePostDataChangedEvent()
    > {
    > }
    >
    > bool IPostBackDataHandler.LoadPostData(string postDataKey,
    > System.Collections.Specialized.NameValueCollection postCollection)
    > {
    > // HERE IS THE PROBLEM: both the RadioButtons have Checked == false
    > // on postback even when one is selected
    > RadioButton rb1 = (RadioButton) Controls[0];
    > RadioButton rb2 = (RadioButton) Controls[1];
    >
    > return false;
    > }
    > #endregion
    >
    > #region Render Overrides
    > protected override void CreateChildControls()
    > {
    > #region rb1
    > _rb1 = new RadioButton();
    > _rb1.GroupName = "DateSet";
    > _rb1.CssClass = "Normal";
    > _rb1.Text = "1";
    > this.Controls.Add(_rb1);
    > #endregion rb1
    >
    > #region rb2
    > _rb2 = new RadioButton();
    > _rb2.GroupName = "DateSet";
    > _rb2.CssClass = "Normal";
    > _rb2.Text = "2";
    > this.Controls.Add(_rb2);
    > #endregion rb2
    > }
    >
    > protected override void Render( HtmlTextWriter writer )
    > {
    > this.EnsureChildControls();
    > base.Render(writer);
    > writer.AddAttribute(HtmlTextWriterAttribute.Type, "Hidden");
    > writer.AddAttribute(HtmlTextWriterAttribute.Value, "");
    > writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID);
    > writer.RenderBeginTag(HtmlTextWriterTag.Input);
    > writer.RenderEndTag();
    > }
    > #endregion
    >
    > protected internal RadioButton rb1
    > {
    > get { EnsureChildControls(); return _rb1; }
    > }
    > protected internal RadioButton rb2
    > {
    > get { EnsureChildControls(); return _rb2; }
    > }
    > private RadioButton _rb1;
    > private RadioButton _rb2;
    > }
     
    Solel Software, Dec 22, 2004
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. One Handed Man \( OHM - Terry Burns \)

    Custom Control & System.Web.UI.IPostBackDataHandler.LoadPostData

    One Handed Man \( OHM - Terry Burns \), Jun 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,127
    One Handed Man \( OHM - Terry Burns \)
    Jun 17, 2004
  2. Francois
    Replies:
    6
    Views:
    3,085
    Joe Fallon
    Dec 5, 2004
  3. =?Utf-8?B?YnJpYW4ubWlsbHM=?=

    IPostBackDataHandler.LoadPostData

    =?Utf-8?B?YnJpYW4ubWlsbHM=?=, Apr 20, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    3,584
    William F. Robertson, Jr.
    Apr 25, 2005
  4. z f
    Replies:
    1
    Views:
    8,617
    =?Utf-8?B?RFdT?=
    Mar 1, 2006
  5. Martin Gustavsson

    IPostBackDataHandler with multiple rendered inputs.

    Martin Gustavsson, Mar 7, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    543
    Martin Gustavsson
    Mar 7, 2006
  6. Nathan Sokalski
    Replies:
    4
    Views:
    573
    Nathan Sokalski
    Jul 21, 2006
  7. Piotr Nowak

    IPostBackDataHandler problem

    Piotr Nowak, Sep 5, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    1,024
    Piotr Nowak
    Sep 6, 2007
  8. Andrew Dawson

    Implementing IPostBackDataHandler in a custom web control with VB.NET

    Andrew Dawson, Aug 9, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    534
    Teemu Keiski
    Aug 12, 2004
Loading...