Reputation: 115
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>
`
Upvotes: 0
Views: 8945
Reputation: 377
As for wanting to know how to add HTML elements to a specific place like a certain DIV tag, well if you have the ID of the DIV tag then you can add the new HTML elements as children to that DIV tag.
Check out this post regarding the details for how to accomplish this.
Upvotes: 0
Reputation: 13600
MyPage.aspx
<asp:Label ID="myLabel" Text="Some text in span" runat="server" />
MyPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
myLabel.CssClass = "someNewClass";
}
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:
myLabel.CssClass += " someNewClass";
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:
Label myDynamicNewLabel = new Label();
myDynamicNewLabel.Text = "Let's say this is an error message";
myParentElement.Controls.Add(myDynamicNewLabel);
Upvotes: 2
Reputation: 103388
This can be done in your code-behind server events:
if (//password is wrong){
txtPassword.CssClass += " error";
}
Upvotes: 2