tags:

views:

134

answers:

2

Hi! I'm new to jquery, and sorry if this question is asked before (could find exactly what I was looking for)

I would like to implement the following convetion: If I create a error label with the name '_lblError' like this:

<div>
   <asp:label ID="_lblError" text="this is a error msg"/>
</div>

classA or classB would be applied to it depending on a empty string or not in the 'text' parameter of the label.

I also would like to place this code in a external js file that would be used throughout the app.

Thanks for any help!

A: 

If the idea is to provide say, have different font and/or background if there is an error and display nothing if there is no text in the error label then you could make the control a literal instead of a label. The literal control does not create a control with no text (MSDN doc)

Nathan Koop
Thanks, that will work in this scenario, but I was hoping to get a grip on how we can use naming convention to apply automagic styling. I'm waiting to see if I get a answer to that. I'll update the title to reflect that.
Larsi
+1  A: 

To start with, you probably need to give the label a css class that can be used for selection:

<asp:Label Id="_lblError" Text="This is the error message"
    CssClass="ThisIsWhatWeWillllWorkWith" />

This will probably output something like

<span id="ct100__lblError" class="ThisIsWhatWeWillWorkWith">
    This is the error message.
</span>

You can now select the label in jQuery using the class as selector, and add class A or B depending on whether the .text() property is empty or not.

$(function() {
    $('.ThisIsWhatWeWillWorkWith').each(function() {
        if($(this).text() == '') { $(this).addClass('ClassA'); }
        else { $(this).addClass('ClassB'); }
    });
});

All code is provided as is, with no guarantees of working without modification. But you get the general idea of how to solve the problem...

EDIT: In response to your comment, here's a way to do it without adding a css class to the label. Instead of using an <asp:Label> tag for the error message, wrap a literal in a tag you hard-code on your page:

<span class="ThisIsWhatWeWillWorkWith"><asp:Literal ID="__ltlError" Text="This is the error message.</asp:Literal></span>

Another, perhaps more elegant way, would be to create your own custom label, and use that instead.

public class ErrorLabel : System.Web.UI.WebControls.Label
{
    public ErrorLabel() {
        this.CssClass = "ThisIsWhatWeWillWorkWith";
    }
}

You then put the error message on your page with the following line:

<asp:ErrorLabel ID="__lblError" Text="This is the error message" />

Again, not sure if the above code will work as is. But again, you get the idea of what to do...

Tomas Lycken
yes, thanks for providing a sln, but I was aiming for a sln that works with a simple naming convention on the control.
Larsi
As you don't have full control over the client side ID, there is no way to get a bulletproof selector using just a naming convention on the ID. See edit to my post for a version that will be slightly more verbose on the server, but fully functional.
Tomas Lycken
Agree, and for providing a sln to the original question I'll accept your answer. Maybe I'll post another question on how to use conventions in jquery. Thanks
Larsi