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

来源:互联网 发布:centos latex 编辑:程序博客网 时间:2024/06/06 03:21
最近,在一个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;")  ‘有些效果,但需要继续完善判定方法。






0 0
原创粉丝点击