ASP.Net Validator Default Style - asp.net

I'm using several variants of the Validator controls (RequiredFieldValidator, CompareValidator, etc) and am using the CssClass property of the validator. I can see (via Firebug) that the class is being applied, but the validator control itself is adding a style element to it, namely color: red. But I don't want that. I want the control to use the cssclass only.
I know I can override the Forecolor attribute, but I'll have to do that on every validator in the project. And I'd really like to be able to just change my CSS Class in my stylesheet in case we have to change all the error message appearances in the future.
Anyone have any clues how to tell the Validator controls to NOT use their default styles?

You can do this in your css file:
.validator
{
color: blue !important;
}
This will override the inline red style.

You can change the default style of validators using Themes.
Right click on the website in Visual Studio
Choose "Add ASP.NET Folder"
Choose "Themes", name the new folder "DefaultTheme"
Create a file called "Controls.skin" in the DefaultTheme folder
Add the following to the Controls.skin file:
<asp:RequiredFieldValidator runat="server" CssClass="validation-error" />
<asp:RangeValidator runat="server" CssClass="validation-error" />
<asp:CompareValidator runat="server" CssClass="validation-error" />
<asp:RegularExpressionValidator runat="server" CssClass="validation-error" />
<asp:CustomValidator runat="server" CssClass="validation-error" />
<asp:ValidationSummary runat="server" CssClass="validation-error" />
Merge the following into your web.config:
<configuration>
<system.web>
<pages theme="DefaultTheme" />
</system.web>
</configuration>
Then you can set whatever colour you want for .validation-error in your CSS files.
(Note that versions of ASP.Net before 4.0 used to apply style="Color:red" to all validators by default, making it hard to override their colours in CSS. If you find that is affecting you, then you can override it by setting the ForeColor property on each of the theme elements above, or add !important to your CSS rule.)
See:
How to: Define ASP.NET Page Themes
How to: Apply ASP.NET Themes
Validation Controls Lost Their Red Color in ASP.Net 4.0

If you use themes, you can set up your skin file to control the appearance of your validator. The problem with Forecolor inline is that the way .Net renders the default controls, it inserts a color="#..." attribute that overrides CSS at the element level. If Keltex's solution above doesn't get it for you with the !important directive, your next step is probably to use/adapt/help work on the CSS-Friendly Control Adapters project at http://www.asp.net/CSSAdapters/.
Shameless plug: Brian DeMarzo is working on extending this project at Google Code.

Have you looked at themes?

Set Forecolor=""
And
CssClass="your-css-class"

Related

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" />

Why does AjaxControlToolkit TabContainer render all custom control markup properties?

This is a strange one to explain but hope I make sense.
Our organisation has a library of custom controls that we use in our solutions. One example of these controls is a textbox combined with a set of validators which can be configured appropriately by its properties set in the markup.
I now have a problem when using this control in (which I beleive to have narrowed it down) a TabContainer.
If I wanted to use the following markup in the container:
<scc:TextBox ID="txtEmailAddr" runat="server" CssClass="input EmailAddress" EnforceEntry="EmailAddress"
ErrorMessage_RequiredFieldNotCompleted="" ErrorMessage_ShowExclamation="true"
MaxLength="150" ShowErrorMessageBelow="false" Label="Email Address " />
When I save or reload the .aspx markup it then renders the following markup for the same control:
<scc:TextBox ID="txtEmailAddr" runat="server" CssClass="input EmailAddress" EnforceEntry="EmailAddress"
ErrorMessage_RequiredFieldNotCompleted="" ErrorMessage_ShowExclamation="True"
MaxLength="150" ShowErrorMessageBelow="False" Label="Email Address "
ClientSidePreventInvalidChars="True" EnableClientScript="True"
EnfoceOnPaste="False" EnforceMaxLengthWithRXOnMultiline="True"
EnforceOnPaste="False" EnforceSpaceInPostcode="True"
ErrorMessage_InvalidFormat="Email Address : Please enter a valid email address"
ErrorMessage_NumericValueInvalidOrOutOfRange="Email Address requires a number to be entered in the range to ."
GuidanceText="" GuidanceText_RenderInMouseoverPanel="False"
JavascriptURL="~/Include/TextBoxMaximumLength.js" LabelBold="False"
LabelCSSClass="" MaxValue="9999999" MinValue="-9999999" Read_Only="False"
RememberAnswer="False" RenderInParagraphs="True"
RenderRequiredTextForRequiredFields="True" Required="True"
RequiredField_InitialValue="" Rows="0" ShowMaxLength="False" Text=""
TextBox_TabIndex="0" TextboxSkinID="" TextMode="SingleLine"
TooltipPopup_BodyText="" TooltipPopup_TooltipText="(guidance)"
ValidationGroup="" ValidationExpression="" />
This would not be a problem other than the properties that are now being rendered in the markup are overriding default functionality of the actual control. In this case the default Email Address regular expression is being ignored because the property 'ValidationExpression' is being set to an empty string!
Again I could place the default regex in that property, but I would just like to understand why the markup is behaving in this manner?
Thanks.
Get the code for the AjaxContolToolkit and step through it to see why all properties are rendered. You can adjust that code as you need and compile the dll and use that. From personal experience, that is the only way I have found use for the Toolkit because of behaviors like you describe.

.skin vs .css from asp.net

What is the main difference between .skin and .css in asp.net?
.skin is new enhancement of IDE. I have been working with .css. What is available in .skin that is not to .css
thanks,
saj
In the skin file you can set properties of asp.net controls.
For example,
<asp:TextBox runat="server" Width="200"/>
All the TextBox controls in your application will have width 200.
You can give it a name and only the controls you like you can set them to apply a skin for example,
<asp:TextBox SkinID="MultiLineTextBox" runat="server" TextMode="MultiLine" Height="240"/>
now in a web page when adds TextBox control you can set its SkinID to be "MultiLineTextBox"
as the following,
<asp:TextBox runat="server" SkinID="MultiLineTextBox"/>
and thus it will inherit the TextMode as MultiLine and the Height as 240.
To use the skin you have to add a theme to your application under the App_Themes folder and there you add the skin file, now to use this theme in your pages you have to set the EnableTheming property of the page to true, StylesheetTheme or Theme to the name of your theme. You can also set this properties in the config file.
Setting the theme in the page aspx,
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" EnableTheming="true" StylesheetTheme="Your Theme Name" %>
Setting the theme in the web.config,
<configuration>
<system.web>
<pages styleSheetTheme="Your Theme Name"></pages>
</system.web>
</configuration>
Note that in terms of what these two things actually do there is a considerable difference. Any properties set in the .skin file are copied out to all of the page controls. An advantage to using Cascading Style Sheets is that the information is loaded and cached once. (and can be applied to multiple web pages.) Skin files can cause page bloat because all the properties set in the skin file must be merged with every affected control every time the page is rendered.
Additionally, the default behavior of the ASP.NET Theme .skin files is to override the properties of the controls being affected (this can be an unexpected behavior). For example, if you set the Width property for all ASP:Labels in your .skin file, all the ASP:Labels that use the skin file will have their Width properties set to that of the .skin file's regardless of the control's individual Width setting. To avoid this behavior, the ASP.NET StyleSheetTheme can be used to allow control-level properties to override the global .skin properties.
You can set some properties like Width even in CSS. Apart from being able to set properties that CSS cannot, there are some things you need .skin file for.
Consider an example where you need all the asp:Label controls on your page to be in blue color. A asp:Label is actually text inside a span, thats inside a hidden div. This is why we are able to set some properties like BackColor to this asp:Label and why the standard label control does not have a 'BackColor' property.
So, if you try to set font color to all ASP Labels through CSS,
then something like
Label {
color: Blue;
}
Will not work. On the other hand, using a skin file you can write
<asp:Label runat="server" ForeColor="Blue"></asp:Label>
and this will set all Labels to blue color.

How to set multiple cssclass name to a control with ASP.NET theming enabled?

CSS supports multiple class names:
<hwc:DropDownList ID="ddlRoomType" runat="server" class="invisible blue" EnableTheming="true" />
Skin file:
<%-- Default dropdown appearance --%>
<asp:DropDownList runat="server" CssClass="dropDownList" AutoPostBack="true"/>
And asp.net theming provides extra skinning and attribute abstraction layer for us. So i'm talking about classic css styling and asp.net theming combined scenario.
But when it comes to giving a CssClass (or class) attribute manually with asp.net theming enabled, you have to make a choice which one is to override another.
How can we combine manually entered class names with dynamic class name created by asp.net theming to generate the html output like below;
<select id="ctl00_Content_ddlRoomType" class="invisible blue dropDownList">
<option value="0">- Select Room -</option>
<option value="9">Single Room</option>
</select>
I can't find any .net theming class to override to implement another theming logic.
Any ideas? Thanks.
Combining CSS classes isn't supported by the standard runtime.
However, it should be pretty easy to add, using a control adapter. You might use a syntax something like CssClass="+dropDownList" to add the class to any existing defaults. The control adapter would look for the plus sign, and set the CssClass property accordingly.
Alternatively (and probably easier), you could override the existing classes you're interested in, and modify the CssClass property to work like you want.

Creating ASP.NET composite control. How to apply properties associated with SkinID?

I am creating a custom composite control based off of an asp:Label control. I'd like to be able to have the label's default properties be skinable as well as additional properties that I add to my control.
But when I add a skin definition to the Default.skin file in my themes directory, and add the control to my page with the SkinId specified, I cannot figure out how to get the control to render with the Skinned properties.
Additional points:
My custom control is defined in a separate library/dll.
I added one test property, and added the [Themeable(false)] attribute. Then I set that property in the .skin file. I didn't get any errors when I tried to view the page, so it appears to me that the .skin file is not getting applied or that the control def in the skin file doesn't get matched up with the control def in the aspx page.
From the skin file:
<ctrl:ExtendedLabel SkinId="test" runat="server"
Expandable="true" Lookup="true" Required="true"
RequiredCssClass="required" Text="Hello" />
From the aspx page:
<ctrl:ExtendedLabel SkinID="test" runat="server"/>
From web.config:
<pages>
<controls>
<add tagPrefix="ctrl" namespace="MyCompany.WebControls"
assembly="MyCompany.Web" />
</controls>
</pages>
I added a test property to the control, marked it as [Themeable(false)] in order to test if I'd get the runtime error when setting that property in the skin file. No error.
Notice that the pages tag doesn't have a styleSheetTheme attribute. I do however, have a Base page class that overrides StyleSheetTheme property, which seems to work for everything else.
If I add the styleSheetTheme attribute to the pages tag in web.config, the skin stuff works, including getting the error if I try to set the non-Themeable property.
What's the difference? How can I get it to work with the Base page class property code?
What are you getting when you try this? I just created a simple test project and was able to skin a custom property on a web custom control. My steps were:
Create the custom control.
Add [Themeable(true)] attribute to the class definition in the code-behind file.
Add a Label control to my custom control.
Add a property named "LabelText" to my custom control, which gets/sets the label controls Text value.
In web.config, add 'theme="TestTheme"' to the system.web/pages section.
In web.config, add '<add tagPrefix="mine" tagName="Test" src="~/UI/Test.ascx" />' to the system.web/pages/controls section.
In my Default.skin file, added '<mine:Test runat="server" LabelText="Test Text" />
In Default.aspx, added '<mine:Test id="test1" runat="server" />'
Load the page up and see the text "Test Text", which was only present in the Skin file.
Hopefully one of my steps above will be something you forgot, but if not, please update your question with details on what you are trying and what you are seeing.

Resources