Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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?
Posted 9-Jan-13 10:58am
Comments
richcb at 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 at 9-Jan-13 16:08pm
   
Of course for all specific controls. For example for special categories: FirstName & Lastname, Email, Description ...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 herehttp://www.dotnetspider.com/forum/204101-How-set-maxlength-property-textbox-using-css.aspx[^]
  Permalink  
Comments
cs101000 at 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?
richcb at 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 at 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?
richcb at 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 at 10-Jan-13 12:46pm
   
OK. Thanks anyway
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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":
http://www.w3schools.com/dom/met_element_setattribute.asp[^].
 
I would highly recommend to use jQuery: http://api.jquery.com/category/attributes/[^].
 
If you need to learn jQuery, please start here:
http://en.wikipedia.org/wiki/Jquery[^],
http://docs.jquery.com/Tutorials[^],
http://docs.jquery.com/How_jQuery_Works[^].
 
—SA
  Permalink  
Comments
richcb at 9-Jan-13 17:08pm
   
That is correct, attributes not properties, thank you.
Sergey Alexandrovich Kryukov at 9-Jan-13 17:11pm
   
You are welcome... :-)
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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">
    <body>
        <form>
            <input type="text" maxlength="@mlen" />
        </form>
    </body>
</html>
Your input field has a maxlengh of 10 and so any other field in which you use the attribute maxlength="@mlen".
  Permalink  
Comments
cs101000 at 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 at 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 http://www.mikesdotnetting.com/Article/199/HTML5-Form-Helpers-For-WebMatrix could be of help.

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



Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 10 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100