Newbie IPostBackDataHandler Question

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Newbie IPostBackDataHandler Question

    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 Guest

  2. Similar Questions and Discussions

    1. Newbie Question: Biz Card Template Question
      Hi, I got the Pagemaker PlugIn - I am using one of the templates for Business Cards - the elements appear to be grouped (bound box all around when I...
    2. IPostBackDataHandler not working properly with DataGrid
      Hello All, I've posted this in microsoft.public.dotnet.framework.aspnet.datagridcontrol but got not reply so I'll try my luck here. Any...
    3. System.OverflowException during IPostBackDataHandler.LoadPostData
      During my postbacks, I try to assign the value from an Input tag to a property of a custom control. However, I keep recieving the following error: ...
    4. Pen Tool Use Question. (Embarrassingly Newbie Question)
      I'm currently using Flash MX and whenever I choose the Pen Tool instead of the pen nib with the small "x" beside it that supposed to show up on...
    5. Help w/ IPostBackDataHandler
      Your sample still leaves me confused. Can i email you offlist? "Natty Gur" <natty@dao2com.com> wrote in message...
  3. #2

    Default RE: Newbie IPostBackDataHandler Question

    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 Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139