How to group RadioButtons in Datalist/Gridview control

Posted: October 29, 2013 in Asp.net, c#
Tags: , ,

To select one radio button at a time in datalist, please follow below steps

Step 1: Write this Past below JavaScript in the aspx source code 

<script type=”text/javascript” language=”javascript”>
function CheckOnes(spanChk)
{
var oItem = spanChk.children;
var theBox= (spanChk.type==”radio”) ? spanChk : spanChk.children.item[0];

xState=theBox.unchecked;
elm=theBox.form.elements;

for(i=0;i<elm.length;i++)
if(elm[i].type==”radio” && elm[i].id!=theBox.id)
{
elm[i].checked=xState;
}
}
</script>

Step 2: Now data list as

<asp:DataList ID=”dlExample” runat=”server” RepeatDirection=”Vertical” RepeatColumns=”4″ OnItemDataBound=”dlExample_ItemDataBound” >
<ItemTemplate>
<table> <tr> <td> <asp:RadioButton ID=”rdb” runat=”server” /> </td> </tr> </table>
</ItemTemplate>
</asp:DataList>

Step 3: In code behind

protected void dlExample_ItemDataBound(object sender, DataListItemEventArgs e)
{
RadioButton rdb;
rdb = (RadioButton)e.Item.FindControl(“rdb”);
if(rdb != null)
rdb.Attributes.Add(“onclick”, “CheckOnes(this);”);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s