changing css class on linkbutton

J

John

Hi

I have been trying to change the css class of a linkbutton without succes
( I dont want to change the cssclass property). Maybe somebody knowshow to
do this?? th.John

<%@ Page Language="VB" %>

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

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Me.LinkButton1.Attributes.Add("onclick", "javascript:change('LinkButton1',
'first');")



End Sub

</script>

<style type="text/css">

..first

{

color: black;

}

..second

{

color: red;

}

</style>

<script language="JavaScript">

function change(id, newClass) {

identity=document.getElementById(id);

identity.className=newClass;

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:LinkButton ID="LinkButton1" class="second"
runat="server">LinkButton</asp:LinkButton></div>

</form>

</body>

</html>
 
K

Ken Cox [Microsoft MVP]

Hi John,

I think your problem may have been due to using

class="second"

instead of

cssclass="second"

Anyway, the code below should do what you want.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


<%@ page language="VB" %>

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

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Me.LinkButton1.Attributes.Add _
("onclick", "javascript:change " & _
"('LinkButton1', 'first');return false;")
End Sub

</script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Change style class onclick</title>
<style type="text/css">
..first
{
color: black;
}
..second
{
color: red;
}
</style>

<script language="JavaScript">
function change(id, newClass) {
identity=document.getElementById(id);
identity.className=newClass;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:linkbutton id="LinkButton1" runat="server"
cssclass="second">LinkButton</asp:linkbutton></div>
</form>
</body>
</html>
 
R

Robbe Morris [C# MVP]

Do this instead:

Me.LinkButton1.Attributes.Add("onclick", "javascript:change(this.id,
'first');")

you could also try this.name

I think you'll find that LinkButton1 isn't really named LinkButton1
if you do a page / view source.
 
J

John

works thanks!
Ken Cox said:
Hi John,

I think your problem may have been due to using

class="second"

instead of

cssclass="second"

Anyway, the code below should do what you want.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]


<%@ page language="VB" %>

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

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Me.LinkButton1.Attributes.Add _
("onclick", "javascript:change " & _
"('LinkButton1', 'first');return false;")
End Sub

</script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Change style class onclick</title>
<style type="text/css">
.first
{
color: black;
}
.second
{
color: red;
}
</style>

<script language="JavaScript">
function change(id, newClass) {
identity=document.getElementById(id);
identity.className=newClass;
}
</script>

</head>
<body>
<form id="form1" runat="server">
<div>
<asp:linkbutton id="LinkButton1" runat="server"
cssclass="second">LinkButton</asp:linkbutton></div>
</form>
</body>
</html>

John said:
Hi

I have been trying to change the css class of a linkbutton without succes
( I dont want to change the cssclass property). Maybe somebody knowshow
to do this?? th.John

<%@ Page Language="VB" %>

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

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)

Me.LinkButton1.Attributes.Add("onclick",
"javascript:change('LinkButton1', 'first');")



End Sub

</script>

<style type="text/css">

.first

{

color: black;

}

.second

{

color: red;

}

</style>

<script language="JavaScript">

function change(id, newClass) {

identity=document.getElementById(id);

identity.className=newClass;

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:LinkButton ID="LinkButton1" class="second"
runat="server">LinkButton</asp:LinkButton></div>

</form>

</body>

</html>
 

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

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,815
Latest member
treekmostly22

Latest Threads

Top