1. 程式人生 > >ASP.NET 控制元件不用Disabled實現ReadOnly的效果,即字型不變灰色。

ASP.NET 控制元件不用Disabled實現ReadOnly的效果,即字型不變灰色。

最近,在一個asp.net專案中遇到RadioButtonList等控制元件設定Enabled=False時(只讀狀態),在IE瀏覽器中檢視是字型顏色為灰,內容不清晰,嘗試各種css效果失敗,又在網上搜索近一天,終於發現以下方法有效。

參考引用:

<select onbeforeactivate="return false" onfocus="this.blur()" onmouseover="this.setCapture()" onmouseout="this.releaseCapture()"> <option>1</option>

</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("onmouseout","this.releaseCapture()")  ’IE支援,FF不支援

rdoP_Type.Attributes.Add("onclick","return false")                               ‘讓FF支援

多個控制元件的情況下,可以通過遍歷設定屬性:

For Each t As Control In Controls
    If 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")

   End If

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;")  ‘有些效果,但需要繼續完善判定方法。