ASP.NET 控制元件不用Disabled實現ReadOnly的效果,即字型不變灰色。
參考引用:
</select>
同理,在ASP.NET中的應用
在asp.net頁面中針對RadioButtonLis、RadioButton、DropDownList、HtmlSelect等控制元件標籤中新增onmouseover="this.setCapture()" onmouseout="this.releaseCapture()"即可(字型顏色也可以直接設定或通過css設定了)。
如:
<asp:RadioButtonList ID="rdoP_Type" runat="server" RepeatColumns="2" RepeatLayout="Flow" onmouseover="this.setCapture()" onmouseout="this.releaseCapture()" ForeColor="#FF9966">
<asp:ListItem Value="Y">是 </asp:ListItem>
<asp:ListItem Value="N">否 </asp:ListItem>
</asp:RadioButtonList>
在程式中,可以這樣新增:
以VB.NET為例:
rdoP_Type.Attributes.Add("onmouseover","this.setCapture()")
rdoP_Type.Attributes.Add("onmouseout","this.releaseCapture()")
分別在IE和FF瀏覽器下測試,發現IE測試沒問題,但是FF並不支援(setCapture和releaseCapture,FF有自己的),再加個屬性試試,變成這樣
rdoP_Type.Attributes.Add("onmouseover","this.setCapture()")
’IE支援,FF不支援
rdoP_Type.Attributes.Add("onclick","return false")
‘讓FF支援
多個控制元件的情況下,可以通過遍歷設定屬性:
For Each t As Control In ControlsIf TypeOf (t) Is RadioButtonList Then
Dim t1 As RadioButtonList = CType(t, RadioButtonList)
t1.Attributes.Add("onmouseover","this.setCapture()")
t1.Attributes.Add("onmouseout","this.releaseCapture()")
t1.Attributes.Add("onclick","return false")
Next
在此之前,試過其他方法,不太理想,在此記錄一下,以備需要時參考:
t1.Attributes.Add("style", "color:blue") 'FF支援,IE不相容
t1.Attributes.Add("onclick", "return false")
t1.Attributes.Add("onclick", "this.checked=!this.checked") ’
t1.Attributes.Add("onclick", " if(rdoP_Type_0.checked) rdoP_Type_1.checked=!rdoP_Type_1.checked;") ‘有些效果,但需要繼續完善判定方法。