Click here to Skip to main content
15,112,354 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
CSS properties can be set in a css file and be applied in the whole site. How about non-CSS properties like maxlength for input type of text. How can we set them in general?
Richard C Bishop 9-Jan-13 16:03pm
Usually that is an attribute directly within the control you are trying to set it for. It can either be done inline with the markup or in the code-behind. Are you trying to set the maxlength for all specific controls in the project or just one control?
cs101000 9-Jan-13 16:08pm
Of course for all specific controls. For example for special categories: FirstName & Lastname, Email, Description ...

I would suggest using a Theme then.

1. Add a folder called app_themes in the web root (if it's not already there).

2. Add a folder called MyTheme in that folder.

3. In the MyTheme folder add a skin file (right click folder and select add>>new item. from dialog select skin).

4. In the skin file put this in:

<asp:TextBox runat='sever' MaxLength='100' />

Note that no instance variables like ID are set here.

5. In web.config set under <system.web> add <page theme='MyTheme' />

This will make all textboxes have maxlength = 100. If you want it only for a single page, don't set that in the web config. In the @Page directive at the top of the page, set theme='MyTheme'

I found this here[^]
cs101000 10-Jan-13 4:26am
Thank you. But I dont see any skin file to add. Is there any in webmatrix when creating a new file? Does it have skin extension?
Richard C Bishop 10-Jan-13 9:52am
Yes, it does have a .skin extension. Did you try the method for applying it only to a single page?
cs101000 10-Jan-13 10:56am
I am trying to do it for the whole site not a single page. In step 3 when I right clicked on the MyTheme folder I didnt find a skin file to add between file types. And if ID cant be set then how can I categorize textboxes and give each category a specific MaxLength?
Richard C Bishop 10-Jan-13 11:02am
It sounds like my solution is not going to be what you need. If each text box is different than you are going to have to set each one by itself. You might try the solution below mine and see if that works.
cs101000 10-Jan-13 12:46pm
OK. Thanks anyway
Those are not "properties". They are "attributes", same as XML attributes.

Obviously, you look at HTML standard and write the accordingly; generalized syntax is like in XML: <someElement someAttributeName="some attribite value" …> …. This is for ASP.NET/HTML use.

You can add/modify any attribute in JavaScript, using element.setAttribute() or element.someAttributeName = "some attribite value":[^].

I would highly recommend to use jQuery:[^].

If you need to learn jQuery, please start here:[^],[^],[^].

Richard C Bishop 9-Jan-13 17:08pm
That is correct, attributes not properties, thank you.
You are welcome... :-)
You could use global variables, initializing them in the _AppStart.cshtml file an recalling them when you want.
Try to create in the root of your Webmatrix site a new _AppStart.cshtml file with the following content:
    AppState["mlen"] = 10;

Now create a Default.cshtml file with this content:
    var mlen = AppState["mlen"];

<html lang="en">
            <input type="text" maxlength="@mlen" />

Your input field has a maxlengh of 10 and so any other field in which you use the attribute maxlength="@mlen".
cs101000 11-Jan-13 3:11am
Is AppState["mlen"] = 10; a var definition? What is the differance between that and this: App.mlen = 10; The second question is how can I specify a class for some inputs and then define maxlength for that class of inputs? I would prefer to do this if possible rather than using vars
Gianmaria Gregori 11-Jan-13 8:43am
AppState["mlen"] = 10 and App.mlen = 10 are alternative syntaxes for the same goal.
Regarding the second question, maybe this Brind's article could be of help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900