How do you set the forecolor of a disabled linkbutton?

A

Allan Ebdrup

When I set the forecolor of a linkbutton to red after setting enabled to
false, the linkbutton stays grey?
How do I change the appearance of a disabled linkbutton?

Kind Regards,
Allan Ebdrup
 
B

bruce barker

for those W3C compliant browsers (IE is not one) use the W3C disabled
pseudo class

<style>
a:disabled {color:red}
</style>

-- bruce (sqlwork.com)
 
W

Walter Wang [MSFT]

Hi Allan,

If the reason that you're disabling the LinkButton is to prevent user from
clicking it but you don't want to change the appearance, you actually can
override the OnClick client-side event to cancel the action:

LinkButton1.Attributes["OnClick"] = "return false;";

Here's a complete test Web Form:


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
Link1.Enabled = !Link1.Enabled;
}

protected void Button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(Link1.Attributes["OnClick"]))
{
Link1.Attributes["OnClick"] = "return false;";
Link1.BackColor = System.Drawing.Color.Gray;
}
else
{
Link1.Attributes.Remove("OnClick");
Link1.BackColor = System.Drawing.Color.Empty;
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="Link1" runat="Server" Text="A Link"
ForeColor="Red"></asp:LinkButton>
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" Text="Button2"
OnClick="Button2_Click" />
</div>
</form>
</body>
</html>


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
A

Allan Ebdrup

Walter Wang said:
Hi Allan,

If the reason that you're disabling the LinkButton is to prevent user from
clicking it but you don't want to change the appearance, you actually can
override the OnClick client-side event to cancel the action:

But I do want to disable it.

Kind Regards,
Allan Ebdrup
 
A

Allan Ebdrup

bruce barker said:
for those W3C compliant browsers (IE is not one) use the W3C disabled
pseudo class

<style>
a:disabled {color:red}
</style>

Thanks, but the solution has to work in IE.

Kind Regards,
Allan Ebdrup
 
W

Walter Wang [MSFT]

Hi Allan,

I'm currently consulting your question in our internal discussion list.
I'll keep you updated as soon as I get updated information. Thank you for
your patience and understanding.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi Allan,

I've consulted your question, it seems IE doesn't support the CSS attribute
selector when you use strict mode by using <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">, but
only some certain styles are supported to change at this time, the color of
the disabled elements are unfortunately not changed by the styles:

<%@ Page Language="C#" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">

<script runat="server">

protected void Button1_Click(object sender, EventArgs e)
{
Link1.Enabled = !Link1.Enabled;
}

protected void Button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(Link1.Attributes["OnClick"]))
{
Link1.Attributes["OnClick"] = "return false;";
Link1.BackColor = System.Drawing.Color.Gray;
}
else
{
Link1.Attributes.Remove("OnClick");
Link1.BackColor = System.Drawing.Color.Empty;
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
a[disabled] {
color: red;
text-decoration: none;
font-family: Arial, Helvetica;
font-size: 26px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="Link1" runat="Server" Text="A Link"
ForeColor="Red"></asp:LinkButton>
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" Text="Button2"
OnClick="Button2_Click" />
</div>
</form>
</body>
</html>



Using above page, you can see the link's size changed when it's disabled,
which means the css selector is working, but the color is not changed due
to the by design behavior.

I'm sorry that I cannot find a more appropriate workaround for you.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top