maxlength for a multiline textbox
Posted on March 17th, 2010 • Filed under asp.net • No Comments
In asp.net you can set the attribute maxlength which works fine if the textmode is SingleLine, but if you set the textmode to MultiLine then maxlength no longer works.
This is because the html that is rendered is a textarea instead on a regular input field.
Customvalidator to the rescue!
For example:
<asp:TextBox runat="server" ID="txtIntro" TextMode="MultiLine" Rows="3" /> <asp:CustomValidator runat="server" ID="cvIntro" maxlength="255" ErrorMessage="Please enter 255 characters or less" ControlToValidate="txtIntro" ClientValidationFunction="checkTextAreaLength" />
This “fires” this clientside function:
<script type="text/javascript">
function checkTextAreaLength(source, arguments) {
var maxlength = $(source).attr("maxlength");
if (maxlength == undefined) maxlength = 255;
if (arguments.Value.length > maxlength)
arguments.IsValid = false;
else
arguments.IsValid = true;
}
</script>
One thing to note is that I’ve had to add the maxlength attribute to the customvalidator not to the textbox. I would prefer to have maxlength set on the textbox but so far I’ve been unable to work this out. If anyone has any ideas please let me know.
Setting a labels "for" attribute inside a repeater
Posted on November 15th, 2009 • Filed under asp.net • No Comments
It’s nice to add <labels> to your form fields and set the labels “for” attribute so that it references the field it is a label for. e.g. <label for="emailaddress">Email</label> <input type="text" id="emailaddress"/> Now if you’re referencing asp.net controls you can use the clientid property. e.g. <label for="<%= emailaddress.clientid()%>">Email</label> <asp:TextBox runat="server" ID="emailaddress" /> All nice an [...]
MySql and the Art of Sql
Posted on July 2nd, 2009 • Filed under MySql • No Comments
I’ve been reading the The Art of SQL and it makes some very sensible suggestions regarding query performance. Here’s somewhere I’ve implemented 1 of the techniques. The task: return records for this month. Usually I’d write something like select * from results where month(dateadded) = month(now()) and year(dateadded) = year(now()) The problem with this is that [...]
Import csv data into MySql
Posted on May 8th, 2009 • Filed under MySql • No Comments
Unusually for an asp.net developer, MySql is my database platform of choice – very quick and less server intensive that certain other database platforms I could mention… Anyway here’s how I go about loading csv data into MySql Prepare your data in a program such as excel or open office Save as comma delimied or [...]
jQuery and asp.net validation controls
Posted on April 11th, 2009 • Filed under Javascript, asp.net • 1 Comment
Just found a nice tip to check with jQuery if all built-in asp.net validation controls on a form are valid. As an asp.net developer and jQuery advocate I know I’m going to find this tip very useful http://devoma.com/post/JQuery-and-ASPNET-validators.aspx
Case insensitive xpath queries
Posted on April 6th, 2009 • Filed under xml • No Comments
I had an xpath query to test whether a node already exists in an xml document Dim KeyphraseNode As XmlNode = xmlDoc.SelectSingleNode( _ String.Format("/websites/website[@url='{0}']/keyphrases/keyphrase[@value='{1}' and @type='{2}']" , _ WebsiteUrl, keyphrase.ToLower, keyphraseType)) Unfortunately users were testing for values with different capitalisations so the program was reporting that the nodes didn’t exist when actually they did. I [...]
asp.net viewstate and seo
Posted on March 18th, 2009 • Filed under asp.net • No Comments
asp.net viewstate is a wonderful invention, but can bloat your html. For most users this is probably going to have minimal impact on the page load time. But for search engines spiders you really want to minimise needless information, especially near the top of the page. It’s best practice to turn off viewstate for the [...]
ASP.NET Event Validation and “Invalid Callback Or Postback Argument”
Posted on February 5th, 2009 • Filed under Javascript, asp.net • No Comments
I was getting this and pulling my hair out. My button (server-side) was pumping out a bit of javascript Page.ClientScript.RegisterStartupScript(GetType(String), "loginunsuccessful", "<script type=""text/javascript""> alert(‘Login unsuccessful – please try again’);</script>") And this kept causing the error. I tried using the prescribed fix: ClientScript.RegisterForEventValidation(mycontrol.uniqueid) in the prerender. Still got the error. Here’s [...]
Use jQuery to replace <body onunload="GUnload()">
Posted on January 26th, 2009 • Filed under Javascript • 7 Comments
When using Google maps it’s important to use GUnload on the page’s unload event. Fine when you can access the body tag directly…. But if you have the Google map functionality wrapped inside an ascx you need a different approach. After a big of digging I found how to use jQuery to “catch” the unload [...]
Need to access session variables in an ashx?
Posted on January 16th, 2009 • Filed under asp.net • 4 Comments
Just a quick note if you need to access session variables from an ashx, you need to implement SessionState.IRequiresSessionState or IReadOnlySessionState if you only need to read the vars In your ashx change Public Class yourclass : Implements IHttpHandler Implements SessionState.IRequiresSessionStat to Public Class yourclass Implements IHttpHandler Implements SessionState.IRequiresSessionStat