I need to know how to change the css class on a tag on page load.. in PHP this was much easier all i had to do is to write <tagNAme class="<?php echo "className"; ?>"> …
I was able to write php code blockes with in tags.. but in ASP .NET this doesn’t seem to work
I tried <% tagElementId.attribute.add("class","cssName"); %> after the tag i wanted to change the css in ASPX file….but didn’t work..
My scanario is i need to check password textbox and on login button click..if the password is wrong password textbox should have a class called “error” otherwise a css class named “userinput”
How do I do it?
EDIT
<form id="loginPanel" runat="server">
<ul>
<li>
<h3>
Log on</h3>
</li>
<li>
<label for="userId">
User ID :</label>
<asp:TextBox ID="userId" name="userId" class="loginTextInput" runat="server"></asp:TextBox>
</li>
<li>
<label for="password">
Password :</label>
<asp:TextBox ID="password" name="password" class="loginTextInput"
runat="server" ontextchanged="password_TextChanged"></asp:TextBox>
</li>
<li>
<asp:Button ID="logInButton" runat="server" Text="Log on" OnClick="logInButton_Click" />
<li class="forgotUserText">Forgot <a href="#" class="forgotUser">User ID </a>or <a
href="#" class="forgotUser">Password?</a> </li>
</ul>
</form>
`
MyPage.aspx
MyPage.aspx.cs
I want to point out one more thing – try not to mix aspx (html) pages with the logic like in your example. It’s a bad habit. In php you don’t really have any alternatives, but you try to work in asp.net now, so you need to get rid of your old habits and think completely different.
EDIT based on your comment about Curt’s answer:
If you want to ADD some class to the existing one, you can do it something like that:
EDIT based on the last comment (and the last one, because this begins to be a completely different question! Create new question and I suggest to do some research first, so we don’t have to explain all the basics)
You just need to set attribute runat=”server” for the element you want to modify by your code-behind and be sure you set its ID as well. Then, you can do something like this: