Newbie IPostBackDataHandler Question

S

Solel Software

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;
}
 
S

Solel Software

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 said:
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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top