views:

12

answers:

1

I'm attempting to display the answers in a certain survey, exactly as the surveyed person viewed the original survey. However, when viewing, I don't want the browser to allow changing of the selected options in a RadioButtonList. Is there a way to make a RadioButtonList read-only, without using radioButtonList1.Enabled = false?

I've tried subclassing RadioButtonList and adding a Fixed attribute to it that only allows changing of the selected value whenever Fixed = false, but it doesn't prevent the user from changing the values; it only reverts it to the original value if the RadioButtonList has AutoPostBack = true, and I want to avoid using postbacks at all.

I've considered using the graphics for a selected/unselected radio button and coding it up as pure HTML, string and bale wire, but I'm hoping there's a better way. Any ideas?

+1  A: 

If you disable radio button elements, they will be greyed out and the form will not look the same as the original.

As a substitute for 'disable', you can have javascript restore the value if the user tries to change it. The jQuery click handler handles both keyboard and mouse selection:

    <script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'&gt;&lt;/script&gt;
    <script type="text/javascript">
        $(function(){ 
            //save list of checked radio buttons
            var checked = $('table input:radio:checked'); 

            $("table input:radio").click(function() { 
                //go through list and re-check initial checks
                $.each(checked, function() { 
                    this.checked=true; 
                }); 
            });

        }); 

    </script>
    <asp:RadioButtonList runat="server" ID="rbl" >
        <asp:ListItem Text="Item1" />
        <asp:ListItem Text="Item2" Selected="True"  />
        <asp:ListItem Text="Item3"  />            
    </asp:RadioButtonList>
foson
This solves the case for a single RBL with a specific ID. Can this be generalized to do the same for all RBLs on the page?
taserian
Sure. You may want to add a class to your RBL's so you can make a more secure selector. I've updated the answer. I save all of the initial selected ids, then reselect them all when any radio button is clicked. (I was lazy)
foson