Read-Only RadioButtonList in ASP.NET 2005
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开发者_Go百科 pure HTML, string and bale wire, but I'm hoping there's a better way. Any ideas?
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'></script>
<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>
Just use the code as below
<script type="text/javascript">
$(function(){
$("#rbl input:radio").attr("disabled", "true");
});
</script>
<asp:RadioButtonList runat="server" ID="rbl" >
<asp:ListItem Text="Item1" />
<asp:ListItem Text="Item2" Selected="True" />
<asp:ListItem Text="Item3" />
</asp:RadioButtonList>
精彩评论