2010-08-25 74 views
0

我試圖在某個調查中顯示答案,與被調查人員查看原始調查完全相同。但是,查看時,我不希望瀏覽器允許更改RadioButtonList中的選定選項。有沒有辦法讓一個RadioButtonList只讀,而不使用radioButtonList1.Enabled = falseASP.NET 2005中的只讀RadioButtonList

我已經嘗試了繼承RadioButtonList並添加一個Fixed屬性,它只允許更改所選值,只要Fixed = false,但它不會阻止用戶更改值;如果RadioButtonList有AutoPostBack = true,它只會將其恢復爲原始值,我想避免使用回傳。

我已經考慮使用圖形選擇/未選定的單選按鈕,並將其編碼爲純HTML,字符串和捆綁線,但我希望有更好的方法。有任何想法嗎?

回答

1

如果您禁用單選按鈕元素,它們將變灰並且表單看起來與原始表單不一樣。

作爲'禁用'的替代品,如果用戶嘗試更改它,則可以讓javascript恢復該值。 jQuery的點擊處理程序處理鍵盤和鼠標選擇:

<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> 
+0

這解決了與特定ID單一RBL的情況。這可以概括爲對頁面上的所有RBL執行相同的操作嗎? – taserian 2010-08-25 16:16:15

+0

當然。你可能想要添加一個類到你的RBL,這樣你可以創建一個更安全的選擇器。我已經更新了答案。我保存所有初始選定的ID,然後在單擊任何單選按鈕時重新選擇它們。 (我很懶) – foson 2010-08-25 21:17:54

0

只需使用的代碼如下

<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> 
+0

禁用單選按鈕會使其灰顯。我不希望單選按鈕變灰,我希望它們保持原樣,但只讀。 – taserian 2013-03-23 23:07:52