Solved

Radio Buttons in a DataList

Posted on 2004-08-26
7
1,474 Views
Last Modified: 2010-08-05
I have a DataList control that has an image, a label, and a radio button in the ItemTemplate. My problem is that I can not group the radio buttons for some reason. I am setting the same GroupName property to every radio button in the ItemDataBoud event, but I can still select multiple radio buttons at the same time. Can anyone help please!
0
Comment
Question by:djaldogz
7 Comments
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11908024
when you view the source, what name does it give the radio?

0
 

Author Comment

by:djaldogz
ID: 11908096
<input id="ImageList0_ImageList__ctl1_radioSelect" type="radio" name="ImageList0:ImageList:_ctl1:SwatchGroup" value="radioSelect" />
<input id="ImageList0_ImageList__ctl2_radioSelect" type="radio" name="ImageList0:ImageList:_ctl2:SwatchGroup" value="radioSelect" />
<input id="ImageList0_ImageList__ctl3_radioSelect" type="radio" name="ImageList0:ImageList:_ctl3:SwatchGroup" value="radioSelect" />

SwatchGroup is the GroupName that I assigned to each radio button
0
 
LVL 8

Expert Comment

by:trevorhartman
ID: 11908113
You will probably have to set the groupname programmatically after the DataList has been created.  itterate through all the items in the datalist and set each item's radiobutton's groupname.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:djaldogz
ID: 11908259
Just tried it.
Did this after the DataBind event:

for(int j=0; j < myColorSelector.ImageList.Items.Count; j++)
{
RadioButton myRadio = (RadioButton)myColorSelector.ImageList.Items[j].FindControl("radioSelect");
myRadio.GroupName = "SwatchGroup";
}

Didn't work, nor did it change the source at all.





0
 
LVL 10

Expert Comment

by:jnhorst
ID: 11910041
Confirm for me if I understand the situation:  You have one radio button in the ItemTemplate, not multiple buttons.  And when the data list shows you want to be able to select only one "item" by selecting that radio button, with any other selected radio buttons being de-selected.  This is what your html output looks like.  If I understand correctly, I have tried this myself just now a couple of different ways and it looks like you cannot override the name attribute with the GroupName property in a DataList like that.  I suspect the same would apply to the DataGrid.

You may need to use a link button that calls the Edit command, and then specify and EditItemTemplate to show something that marks the color as selected.

John
0
 
LVL 29

Accepted Solution

by:
David H.H.Lee earned 250 total points
ID: 11910695
djaldogz ,

Use this :
client script
----------------
<script language="javascript">
function checkOne(selObj)
{
      var items=document.Form1 ; //your form name
      for(var i=0;i<items.length;i++)
      {
            if(items.elements[i].type=="radio")//detect radio button
            {
                  if(items.elements[i].id.indexOf("DataList1")>-1)//datalist name
                        items.elements[i].checked=false;
            }
      }
      document.getElementById(selObj).checked=true;
}
</script>

<form id="Form1" method="post" runat="server">
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
      <asp:RadioButton ID="rdbEE" Runat="server" Text="Option" GroupName="EE"></asp:RadioButton>
</ItemTemplate>
</asp:DataList>
</form>

code-behind
-----------------
 Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList1.ItemDataBound
        If e.Item.ItemType = ListItemType.AlternatingItem OrElse e.Item.ItemType = ListItemType.Item Then
            Dim rdbEE As RadioButton
            rdbEE = CType(e.Item.FindControl("rdbEE"), RadioButton)
            rdbEE.Attributes.Add("onClick", "javascript:checkOne('" & e.Item.ClientID & "_rdbEE')")
        End If
    End Sub

Regards
x_com
0
 

Author Comment

by:djaldogz
ID: 11915080
John,
You understood the problem correctly. While a link button with an edit command would do the job, I really needed a radio button to work in this case. Thank you, though.

X_Com,

I tried your script, and it worked perfectly!

Thank you!
Allen-
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hovering effect 9 46
Getting multiple values from a dataset ASP.NET(VB) 3 37
Vb.net dynamic formulas in runtime 11 61
ASP.net Javascript use multiple sliders 2 16
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now