您的位置:首页 > 编程语言 > ASP

ASP.NET 控件不用Disabled实现ReadOnly的效果,即字体不变灰色。

2015-09-23 10:33 645 查看
最近,在一个asp.net项目中遇到RadioButtonList等控件设置Enabled=False时(只读状态),在IE浏览器中查看是字体颜色为灰,内容不清晰,尝试各种css效果失败,又在网上搜索近一天,终于发现以下方法有效。

参考引用:

HTML中Select不用Disabled实现ReadOnly的效果-javascript技巧

<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;")  ‘有些效果,但需要继续完善判定方法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: