Workaround for RadioButton in Repeater type controls

A

Alan Samet

If you need to get in touch with me, be sure to remove the
"newsgroups" from my email address.

I've developed a quick-and-dirty workaround for putting
System.Web.UI.WebControls.RadioButton controls in repeater-style
controls. I've only tested it a little, but it seems like it should
work fine for getting around that naming issue that arises when you
have a set of radio buttons that should be grouped together; but
because they exist on different nodes of the control tree, they get
different "Name" attributes. This particular example is for radio
buttons that are nested directly within a repeater control. If you
want to change which "NamingContainer" acts as its parent, just
reimplement the "get" accessor of the ParentUniqueId property.

Also, if you haven't used external assemblies that contain Web Custom
Controls in your projects, you register them by using the @ Register
directive. e.g.:

<%@ Register Assembly="TheAssemblyThatContainsRepeatableRadioButtonWithoutTheDllSuffix"
Namespace="AlanSamet.Web.UI.WebControls" TagPrefix="WhateverYouWant"
%>

Then, add your control as follows:

<WhateverYouWant:RepeatableRadioButton runat=server />

-Alan
http://www.htmlwindows.net/
http://www.usfbullshit.com/
http://www.alansamet.com/
I need work

Here's the source:

using System;
using System.Web.UI;

namespace AlanSamet.Web.UI.WebControls
{
public class RepeatableRadioButton :
System.Web.UI.WebControls.RadioButton, IPostBackDataHandler
{
protected string ParentUniqueId
{
get
{
return this.Parent.Parent.UniqueID;
}
}

protected override void Render(HtmlTextWriter writer)
{
base.Render (new RepeatableRadioButtonWriter(writer,
ParentUniqueId, GroupName));
}

bool IPostBackDataHandler.LoadPostData(string postDataKey,
System.Collections.Specialized.NameValueCollection postCollection)
{
bool bWasChecked = this.Checked;
this.Checked = postCollection[String.Format("{0}:{1}",
ParentUniqueId, GroupName)].Equals(this.UniqueID);
return bWasChecked ^ this.Checked;
}

void IPostBackDataHandler.RaisePostDataChangedEvent()
{
base.OnCheckedChanged(EventArgs.Empty);
}
}

public class RepeatableRadioButtonWriter : HtmlTextWriter
{
string _parentUniqueId = null;
string _groupName = null;

public RepeatableRadioButtonWriter(HtmlTextWriter writer, string
parentUniqueId, string groupName) : base(writer)
{
_parentUniqueId = parentUniqueId;
_groupName = groupName;
}

public override void AddAttribute(HtmlTextWriterAttribute key,
string value)
{
if (key == HtmlTextWriterAttribute.Name)
value = String.Format("{0}:{1}", _parentUniqueId, _groupName);
base.AddAttribute (key, value);
}
}
}
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top