Loading ...

Radio Button single selection in Repeater/Datalist

 /5
0 (0votes)


I have spent lot of time searching on google and fixing the single selection of radion buttion problem in datalist/reapter. Here is the code to use radio buttion inside repeater and datalist . Problem we face while using radio buttion in datalist that we are not able to check single radio buttion,if we use it simply it will act like check box. So, over come this problem of mutiple selection.We can use the following javascript for single selection.

Here is the example code snippets :
<asp:Repeater ID="rptTest" runat="server" OnItemDataBound="rptTest _ItemDataBound">
         <HeaderTemplate>
            Select Radio buttion
         </HeaderTemplate>
 <ItemTemplate>
            <asp:RadioButton ID="radTest" runat="server" />                            
</ItemTemplate>
</
asp:Repeater>

On Repeater ItemDataBound event :-
protected void rptTest_ItemDataBound(object sender, RepeaterItemEventArgs e)     
  
{
    if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            RadioButton rdo = (RadioButton)e.Item.FindControl("radTest");        
           
string script = "SetSingleRadioButton('rptTes  t.*Test',this)";
            rdo.Attributes.Add("onclick", script);
    }

Java Script for Unique Radio Button :-
<script type="text/javascript">
    function SetSingleRadioButton(nameregex, current) {
        re = new RegExp(nameregex);
        for (i = 0; i < document.forms[0].elements.length; i++) {
            elm = document.forms[0].elements[i]
            if (elm.type == 'radio') {
                if (re.test(elm.name)) {
                    elm.checked = false;
                }
            }
        }
        current.checked = true;
    }
Hope it Helps …Thanks

Comments (5)

   
Carlos
ha, ha, ha perfect!!!, thank you from Italia
3/23/2009
 · 
by
   
yofi
I avoided the regexp through this and it works fine for me. hope it helps...

protected void rptWithout_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            return;
        RadioButton rdo = (RadioButton)e.Item.FindControl("rdoSelect");
        string script = "SetSingleRadioButton('" + rdo.ClientID + "',this)";
        rdo.Attributes.Add("onclick", script);
    }

function SetSingleRadioButton(nameregex, current) {
      

        for (i = 0; i
6/16/2009
 · 
by
   
Shaitender
Okay, i will try that too .. btw thanks
6/16/2009
 · 
by
   
maurvi
maurvi
its not workk

errors r fired..
2/24/2010
 · 
by
   
shaitender
@@maurvi
Whats the Error ...Mail me at(shaitender@gmail.com)
2/24/2010
 · 
by

Top Posts