Css validation do not work in asp net - asp.net

i cant use css validation class inside asp.net controls but all other css design works correctly for example i just need to add class="validate[required]" to make a control validation in html but the same is not working for asp.net textbox controls i tried CssClass too didnt work.
please help, thanks in advance
<asp:TextBox ID="txt_uname" runat="server" Height="25" CssClass="validate[required]"></asp:TextBox>

validate[required]
This is not a valid css class name.
This declaration means validate class will apply to those elements, which have required attribute.
Your css class
.validate[required]
{
color:Red;
}
So try this
<asp:TextBox ID="txtUsername" runat="server" CssClass="validate" required="required"></asp:TextBox>

Related

ASP.NET closing tag

When I use autocompletion in VisualStudio 2010 within my .aspx application, there are different default completions at closing control tags:
<asp:CheckBox />
<asp:Label></asp:Label>
Is there a explaination for this behaviour?
<asp:CheckBox></asp:CheckBox>
<asp:Label />
Wouldn't be invalid.
This is because ASP.NET's Label control is decorated with the ParseChildrenAttribute, with ParseChildren(false) while CheckBox isn't.
You can support the same behavior whith your custom controls, for example, with the following code, Visual Studio will behave like Label if you use MyControl in the web form editor:
[ParseChildren(false)]
public class MyControl : WebControl
{
...
}
The label closing is like that
<asp:Label runat="server"></asp:Label>
because usually we type something between
<asp:Label runat="server" ID="lblOne">better start programming now</asp:Label>
that is not the case for checkbox, that we type inside of it
<asp:CheckBox runat="server" Text="enable" ID="cbOne" />
We have on both elements the Text field, why on the one we prefer to write out side... Look at this example, on Label, or On other similar controls the text that we may have to write may include characters that are not allowed inside the Text Property, maybe a complex css style or what ever... The check box from the other side is only include a small text (yes, not, something like that)
<asp:Label ID="lblLabel" runat="server">
This is a <b>"label"</b>
<br />And one more line
</asp:Label>
and more example that compiles
<asp:Label ID="lblLabel" runat="server">
This is a <b>"label"</b>
<br />And one more line
<asp:Literal runat="server" ID="ltrOneMore">One more Control Inside</asp:Literal>
</asp:Label>
---but this is not compile--
<asp:Label ID="lblLabel2" runat="server"
Text="This is a <b>"label"</b>
<br /> and one more line"
/>
At the final end is a decision that the makes make - maybe we need to ask them for the real real reason.
Now this is also not compile
<asp:CheckBox runat="server" ID="cbMyChbx">one<asp:CheckBox>
check box when is render on page is use two controls, one input and one label, so they maybe need to help user to understand that the text is not going on the input control.
<asp:CheckBox />
Because the element has no content, you can close the tag with /> instead of using a separate closing tag
<asp:Label></asp:Label> or <asp:Label />
Displays static text on a Web Forms page and allows you to manipulate it programmatically.
Learn more about it Web Server Control
All the answers above are valid, but something additional. All the asp controls are eventually rendered as HTML controls and that also defines how the asp controls behave. For e.g. it is not necessary that text in a label is always set as
<asp:Label runat="server" ID="lblOne">better start programming now</asp:Label>
it can be also done as follows
<asp:Label runat="server" ID="lblOne" Text="better start programming"></asp:Label>
now both are correct format, so it is not valid to say that any control which needs content will have a separate closing tag. It also depends on how it rendered in HTML. for e.g by default asp Label is rendered as a span and doesnt conform to XHTML standards. Hope this makes it clear, always think of how it will be rendered and ASP tries to adhere to what eventually will be rendered.
cheers

class name for asp input box

I'm a asp beginner but familiar in php.I used class for defining css in asp as two ways.I want to know which is the best way practice to use.
Way I
<asp:textbox runat="server" class="sample" id="text1"></asp:textbox>
Way II
<asp:textbox runat="server" CssClass="sample" id="text1"></asp:textbox>
Both is working for css style as
.sample{
/*some style*/
}
I would use the Way II incase asp controls and Way I incase of Html controls.
Incase of asp controls if you want to change the class based on some logic, you could in the code behind(webforms) if you are doing Way II.

How to set the name attribute of an element in ASP.NET which is not equal to its ID attribute

I have a code which I have to use HTML elements instead of ASP.NET elements. So because I want to have access to the elements from CodeBehind I've set all of their attributes to runat=server.
As you know ASP.NET changes the name attribute of elements to something like CT100$MainContent$IDNameOfThatElement. How can I prevent ASP.NET to change it? Cuz I have to have some other names because of JQuery stuff...
For example ASP.NET changes the name attribute from required to CT100$MainContent$PaymentCode in the following code:
<div class="field">
<input type="text" runat="server" name="required" id="PaymentCode" />
</div>
Thanx in advance.
You have to add ClientIDMode="Static"
Example:
<asp:Panel ID="myPanel" runat="server" ClientIDMode="Static">
</asp:Panel>
As far as I know you can't override the name attribute without jumping through some hoops. What you can do is create a controlAdapter that can control which attributes will be rendered and how.
Have a look at this answer for an idea about how to accomplish your goal.

Include CSS Content page in Masterpage ASP.NET

why i cannot adding css Content Page in Master Page Asp.Net.
i cannot change..
<asp:TextBox ID="name" runat="server"></asp:TextBox>
then i use css
#name {
width:300px;
height: 300px;
}
but he did not change.
anyone can help me resolve this?
By default server object ID's aren't rendered exactly the same. You should either use a class instead or set clientidmode to static for the textbox.
If you view the source on the rendered page and look for that textbox, you'll find the ID is probably a few names separated by an underscore.
Here is the same markup with a static client id.
<asp:TextBox ID="name" runat="server" ClientIDMode="static"></asp:TextBox>
Please note that you shouldn't do this for any control that you plan to repeat on a page.
In that case you should use a class.
You have to use this CSS using Class
because When Server control render in page then Their ID is Unique that generate by IIS foloowd by contant page Holder Name
means
<asp:TextBox ID="name" runat="server"></asp:TextBox>
this will genrate
<input id="ContantPlaceholderName_name" name="ContantPlaceholderName_name"/>
so use class name is better option

Add a class along with class specified in the skin

We are using themes to style our control.
For textbox we have added like this in the skin
<asp:TextBox runat="server" CssClass="txt edit_txt" />
Now in some cases, we want to add another class along with this 2 classes.
when we added a textbox like this in aspx
<asp:TextBox ID="txtnameE" runat="server" MaxLength="30" CssClass="RequiredCheckClass"></asp:TextBox>
On rendering it didn't take the "RequiredCheckClass" class, but only the other 2 classes specified in the skin.
So is there any way to add a class along with the classes specified in the skin from an aspx page.
PS : now i am going to use #Curt suggestion or will use EnableTheming=false and will add all the required classes to this control. Please update if anyone got any other idea other than these....
One option would be to create another TextBox control in the Skin file like:
<asp:TextBox SkinID="Required" runat="server" CssClass="txt edit_txt RequiredCheckClass" />
And then use the following in your markup file:
<asp:TextBox ID="txtnameE" runat="server" MaxLength="30" SkinID="Required"></asp:TextBox>
Other than that I'm not sure. I've always avoided ASP.NET Themes since I found out how restrictive they are.
You can use the "class" property - just like you use it in a regular HTML element.
thats the bypass that i am using.
<asp:TextBox SkinID="Required" runat="server" class="RequiredCheckClass" CssClass="txt edit_txt" />

Resources