HHHHEEEELLLP!!! radio button and repeater <read before you judge>

Last post 06-05-2008 7:44 PM by hooplafoo. 7 replies.

Sort Posts:

  • HHHHEEEELLLP!!! radio button and repeater <read before you judge>

    04-14-2008, 10:37 PM
    • Loading...
    • hooplafoo
    • Joined on 01-24-2007, 5:41 PM
    • Posts 13

    ok, so yeah, repeater with a radio nested, nice move MS.. i got the javascript to keep the radios from being selected.. each radio button has a list of checkboxes that get show / hide on click of the radio button.. Item -> item details kind of thing

     repeater -> radiobutton -> repeater -> checkboxes

    on itemdatabound find the repeater and add checkboxes as necessary

    so it all renders fine, and works nicely...

    BUT!!! no matter which radio button is checked I always get the value of checked=false on each radio button.  I find the control fine, but it is always false, even when I'm looking at a checked radio... HOW DO I DO THIS!!>> so upset, this has taken me near 8 hours just to get to this point and now its another battle to figure out how to get this... ARRGH!!

    RadioButton rb = (RadioButton)subCatListing.Items[0].FindControl("subCatItem");

    //always false...
    if (rb.Checked)

    {
    ..always false

    .Net<3.5> HardCore<commando>
  • Re: HHHHEEEELLLP!!! radio button and repeater <read before you judge>

    04-15-2008, 1:05 AM
    • Loading...
    • hooplafoo
    • Joined on 01-24-2007, 5:41 PM
    • Posts 13

    I've tried a datalist also to no avail.. Oh yeah, and its in a wizard as a wizard step also... :-\

    .Net<3.5> HardCore<commando>
  • Re: HHHHEEEELLLP!!! radio button and repeater <read before you judge>

    04-15-2008, 1:23 AM

    HI, r u assigning different id's  to all radio button's and make sure that u check all the radio Button Id's and not a single one in a loop..

    It would be better if u post ur code... 

  • Re: HHHHEEEELLLP!!! radio button and repeater <read before you judge>

    04-15-2008, 2:21 AM
    Answer
    • Loading...
    • hooplafoo
    • Joined on 01-24-2007, 5:41 PM
    • Posts 13

    wow, well first of all thanks for such a quick response.. I never could figure it out.. here's more or less what I was doing maybe it will help someone out there

    .net they were all server controls-->
    <asp:wizard>
    <wizardStep>

    <repeater  id=mainCategories>

    <asp:radioButton id="subCatItem" />

          <div>
            <repeater id=subCategoryDetails>
              
                <asp:checkBoxList id="chbxSubCatListing">

            </repeater>
          </div>

    </repeater>
    </wizardStep>
    </asp:wizard>

    -->C#


    //so obviously I left out some code, like casting the dataitem, etc..
    //but in the end the idea was to on item bound, get the current radio button assign some attributes to it, onclick to select only one, show/hide the div
    //append an id to it, etc...
    //bear with me.. but it rendered out exactly what I thought I wanted, a list of radio buttons that show / hid a div of checkbox items


    onitemDataBound(sender, itemEventargs e)
    {

    RadioButton rb = (RadioButt)item.FindControl("subCatItem");

    if (rb != null)
    {
    //lj is an object type that is cast to the dataitem in code not shown
    rb.Attributes.Add(
    "onclick", "javascript:showDiv('chbxGroup_" + lj.uniqueId+ "')");
    rb.ID = "rb_"+lj.uniqueId;
    rb.Text = lj.typeName;
    strFunction = "SetUniqueRadioButton('"+dl.ClientID+"',this);";
    rb.Attributes.Add(
    "onclick", strFunction+"javascript:showDiv('chbxGroup_" + lj.uniqueId+ "')");
    rb.GroupName = "subCatGroupRadios";
    }

    foreach (var o in jobTypes)
    {
    if(i == currentItem && currentItem <= totalResults)

    var currentSubCategory = from p in ...

    CheckBoxList cb = (CheckBoxList)item.FindControl("chbxSubCatListing");

    //incase refresh or something
    cb.Items.Clear();

    foreach ({type} ljt in currentSubCategory)
    {
    ListItem li = new ListItem();
    li.Text = ljt.jobTypeSubCatyName;
    li.Value = ljt.uniqueid.ToString();
    r.Items.Add(li);
    }
    currentItem = i;
    }

    i++;


    }

     

    Now The pain---->

     //called on switch that fired on activatewizard step, if step == 1... kind of thing
    //and in the end the radio button was always false..

    protected int getSubCatValue()

    {
    var returnSubCatId = 0;

    jRequest = {cast to object}

    var i = 0;

    if (subCatItem.HasControls())
    {

    foreach (repeater Item ri in subCatItem.Items)

    {

    RadioButton rb = (RadioButton)mainCategories.Items[0].FindControl("subCatItem");

    CheckBoxList cbl = (CheckBoxList)mainCategories.Items[0].FindControl("chbxSubCatListing");

    //THIS WAS ALWAYS FALSE, THE TEXT VALUE WOULD COME THROUGH FINE, BUT NOT THE CHECKED STATUS..
    if
    (rb.Checked)

    {

    }

    foreach (ListItem li in cbl.Items)

    {
    ...
    So basically that's the gist of what was goig on,  I know this code won't build, but since the post I've changed the code, but this was the idea... I abanoonded, and I guess that's what MS wanted me to do... it makes sense why radio buttons aren't idea in repeaters, but VS didnt' tell me not too!! hehe..

    BTW, I don' t know how to turn this color off, so I apologize...

    But in short this drove me nuts.. honestly It was all rendering fine, it was all server controls, but I coulndnt discern which was checked via server side, I saw it.., but when I'd run through the code that radio button was always false.. each item, I saw the items come through with the correct text value so I know I was close, but I the checked flag was always, always false.. ;..(

    So in the end I just went to the gym and worked it out, when I returned, I decided that I'd use checkboxes in a datalist, with a nested checkboxlist that I bound to onitembound, I assigned the same javascript function to the main checkbox to keep it exclusive, and also kept the onclick function on tit to show / hide the div, and bound to the checkboxlist for my sub category details listing...so I didnt lose any functionality.. just a couple years off the backend...

    The problem was that I didn't discover the issue with the radiobuttons until I had already built the page out, so I was fighting a losing battle from the start but didnt get ther till I was 6 hours deep..
    If you'd like to see the working code I'd be happy to post it, but I've resolved this issue, and am very surprised by the quick response, I will definately return and post back if I run into any  more issues.. so I'll probably be posting tomorrow!!!

    Thanks again!!

    .Net<3.5> HardCore<commando>
  • Re: HHHHEEEELLLP!!! radio button and repeater <read before you judge>

    04-15-2008, 3:02 AM

     foreach (repeater Item ri in subCatItem.Items)

    {

    RadioButton rb = (RadioButton)mainCategories.Items[0].FindControl("subCatItem");

    CheckBoxList cbl = (CheckBoxList)mainCategories.Items[0].FindControl("chbxSubCatListing");

    //THIS WAS ALWAYS FALSE, THE TEXT VALUE WOULD COME THROUGH FINE, BUT NOT THE CHECKED STATUS..
    if
    (rb.Checked)

    {

    }

     

    I assume u have multiple radio button but ur checking for only one radio button i.e  "subCatItem". So in this case there are multiple radio button list so it will conflict and it willl always give you erroraneous result. So for each radio button list u need to have different Id's.

    Try to append id of the field or an incremented numeric value bt make sure that u have different Id generated each time for radio button....

     Hope this helps U..

  • Re: HHHHEEEELLLP!!! radio button and repeater <read before you judge>

    04-15-2008, 1:38 PM
    Answer
    • Loading...
    • hooplafoo
    • Joined on 01-24-2007, 5:41 PM
    • Posts 13

    well not to beat a dead horse since I was able to do this with checkboxes and I'm fine with that, but yes, I'm sure that they had different ID's, and from my understanding an "item" being bound in a repeater is not a literal control, ie radiobutton, but a repeaterItem, so when its being bound its treated as an entirely different object and is assigned mutually exclusive ID's due to the nature of the repeater item.. so when doing a findControl, I'm not actually finding the radiobutton, but the placeholder for that radio button, which has its own unique ID and exists in each repeater item. 

    So as you can see in the code, I'm looping through the controls of the repeater, which contains 'n' number of controls, all spawning from the initial control, but rendered out as repeater items...and if you mean the actual rendering out of the control, the actual string would be something like ctl00ContentPlaceHolder001Ctl00SubCatItemrb_1, ctl00ContentPlaceHolder001Ctl00SubCatItemrb_2,... and as you can see in the posted code, I was assigning a uniqueId to the id of the control onItemBound...

    and just to clarify, rb would come through with their different corresponding values... so I would get rb.text = "text1", then the next iteration through the control list of the repeater would be rb.text = "text2" so I know that I was finding the "individual" radiobuttons in the repeater control list, and would have their values, beyond the fact that I was assigning a different unique Id as the value, I did think it conficting Id's might be a problem... so I don't think that was the issue..

    my suspicions are that the wizard control does its own kind of control naming and control management, and when I put a repeater in the mix and a radio button in that repeater, the repeater makes unique RadioButtons, and the wizard makes a unique repeater to do some kind of state management for it to keep track of its children controls, and even though I check the control, the wizard or repeater is not handling or bubbling up the stateEvent of that radio... it knows what it placed on the page, but it doesn't know how to handle the radio button checked event since radio buttons are usually a part of a group, and this usage does seem counter intuitive to the usage or a radio button after all..

     again thank you for taking the time, but really, I'm fine with using checkboxes, it appears that radiobuttons inside of repeaters are not the correct usage of radiobuttons, at least not in my implementation of it... and I'm totally fine with using checkboxes...

    Thanks Again!!!

    .Net<3.5> HardCore<commando>
  • Re: HHHHEEEELLLP!!! radio button and repeater <read before you judge>

    05-16-2008, 11:19 PM
    • Loading...
    • johnbrennan
    • Joined on 05-17-2008, 3:07 AM
    • Posts 1

     Hi Folks, I hit this very same issue yesterday. I assumed that I could drag a radio button on to my repeater, set the groupname property and that was it. Of course I quickly saw that it wasn't that easy. How frustrating to have something like this take up hours of my time.

     I searched on google and saw that some people had a solution involving the use of javascript. This just looked plain messy. What if javascript was disabled on my client browser etc. etc. The method I went with was one by Scott Mitchell at 4guysfromrolla.com. Scott's article can be found right here on www.asp.net. The article itself covers the use of radiobuttons in the Gridview but you can apply exactly the same approach to get around the problems of using radiobuttons in the repeater control. Scotts approach is instead of using the asp.net radiobutton server control we add a asp.net literal control to our repeater. Then when we bind our data to the repeater we'll dynamically insert a standard html radio button <input type="radio". In the case of the repeater we'll do this in the ItemCreated event. Bottom line this approach works for me and it doesn't require any javascript:

     
    Here is a link to the article: http://www.asp.net/learn/data-access/tutorial-51-vb.aspx
     

    Cheers,

    John 

  • Re: HHHHEEEELLLP!!! radio button and repeater <read before you judge>

    06-05-2008, 7:44 PM
    • Loading...
    • hooplafoo
    • Joined on 01-24-2007, 5:41 PM
    • Posts 13

    That solution doesn't require javascript, you're right, but I'm not sure it would work if the grid was nested inside a wizard control... you'd have to add more code to handle the various wizard step state..

    also a note to people reading this, most, of if not all of the javascript solutions will not work when a repeater is nested inside a wizard, you can't get the checked value, since when the wizard is loaded, the value is unchecked.. I

    However, I'm not sure if the above example will work, definately the best solution worth a try so far.. but again, the way the wizard maintains its state is similiar to the way the gridview does, SOOOO try getting the value of a selected radio button from within a repeater, within a wizard... like I said in my previous post, I was able to get this working using checkboxes, if I'm wrong, I'd love to hear about it!!!

     

    .Net<3.5> HardCore<commando>
Page 1 of 1 (8 items)