Archive for the ‘asp.net’ Category
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 easy so far. The situation gets a little bit more complicated when you start nesting this inside a repeater.
e.g.
<asp:Repeater runat="server" ID="rpt1"> <itemTemplate> <label for="<%= emailaddress.clientid()%>">Email</label> <asp:TextBox runat="server" ID="emailaddress" /> </itemTemplate> </asp:Repeater>
this won’t work.
Here’s what you need to do instead.
<asp:Repeater runat="server" ID="rpt1">
<itemTemplate>
<label for="<%# container.findcontrol("emailaddress").clientid()%>">Email</label>
<asp:TextBox runat="server" ID="emailaddress" />
</itemTemplate>
</asp:Repeater>
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
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 whole page or for individual usercontrols if they don’t require viewstate. The deciding factor here is: does the page or usercontrol perform postbacks.
Even with careful pruning of viewstate you’ll probably find your viewstate is still large…
What you can do
Now there are a couple of strategies I’ve seen for working round this, both involve overriding the page
1 is to intercept the page rendering and move the viewstate hidden variable to the bottom of the page.
Here’s an example http://www.dotnetdiary.com/labels/Moving%20ViewState%20Field.html
2 is a very clever solution that keeps the viewstate information on the server and justs passes a key back and forth to the client.
I read about this approach here http://www.eggheadcafe.com/articles/20040613.asp
Here’s my approach
Now since I don’t think that viewstate bloat has too much of an impact on page performance for real users I’m only going to worry about search engine spiders.
asp.net has a way of detecting spiders – now I’m not saying it’s going to be 100% accurate but in my tests it’s been accurate enough for this technique.
In the page load of your page (or master page)
If Request.Browser.Crawler Then Page.EnableViewState = False
er that’s it, pretty simply eh?
You can verify if this is working by viewing the source of the cached version of the page that google holds.
Hope that helps!
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 how I fixed it. RegisterStartupScript does the job of running at startup by emiting the js right at the foot of the page. I wrapped my JS in jQuery’s own onload code.
Page.ClientScript. RegisterStartupScript(GetType(String),
"loginunsuccessful", "<script type=""text/javascript"">
$(document).ready(function(){alert('Login unsuccessful – please try again');});
</script>")
Still the same error, I then changed to using Page.ClientScript.RegisterClientScriptBlock, which emits the JS in the midst of the html and hey presto no errors. This is without using the RegisterForEventValidation I’d like to add.
Final code:
Page.ClientScript.RegisterClientScriptBlock(GetType(String),
"loginunsuccessful", "<script type=""text/javascript"">
$(document).ready(function(){BlockAlert('Login unsuccessful – please try again');});
</script>")
Simple to fix when you know how.
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
Required field validator for an fckeditor
Posted on October 7th, 2008 • Filed under asp.net • 2 Comments
Unfortunately a requiredfieldvalidator doesn’t work with the fckeditor.
The way to do this is to use a custom validator.
<asp:customvalidator runat="server" validateemptytext="true" id="cvIntro" setfocusonerror="true" display="none" errormessage="Please enter an introduction" clientvalidationfunction="ValidateContentText" controltovalidate="fckIntro"></asp:customvalidator>
Important note: you need to set validateemptytext=”true” otherwise it doesn’t fire
Then add the following javascript (note that I’m using jQuery here to reference the validator control)
<script type="text/javascript">
function ValidateContentText(source,args)
{
var controltovalidate = jQuery(source).attr("controltovalidate");
var fckControl = FCKeditorAPI.GetInstance(controltovalidate);
args.IsValid = fckControl.GetXHTML(true) != "";
}
</script>
You might also want to handle the server-side validation too, even though if client-side isn’t working then the fckeditor won’t be working…
Protected Sub cvIntro_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvIntro.ServerValidate
Dim myContentPlaceHolder As ContentPlaceHolder
myContentPlaceHolder = CType(Master.FindControl("cphMain"), ContentPlaceHolder)
Dim fckEditorToCheck As FredCK.FCKeditorV2.FCKeditor
fckEditorToCheck = myContentPlaceHolder.FindControl(CType(source, CustomValidator).ControlToValidate)
args.IsValid = Not String.IsNullOrEmpty(fckEditorToCheck.Value)
End Sub
Find a control within a area
Posted on October 7th, 2008 • Filed under asp.net • No Comments
If you are using master pages, you may expect that you can find a control within an tag with the following
me.findcontrol("fckEditor")
But unfortunately that doesn’t work.
You need to make a reference to the place holder the control is in, then find the control within the place holder.
Another gotcha here is that you need to find this control based on the id it has on the master page not the id it has in the page in question.
e.g.
If the tags on the page are
<asp:Content ID="Content2" ContentPlaceHolderID="cphMain" Runat="Server">
</asp:Content>
You need to create a reference to the place holder with
Dim myContentPlaceHolder As ContentPlaceHolder
myContentPlaceHolder = CType(Master.FindControl("cphMain"), ContentPlaceHolder)
and not
Dim myContentPlaceHolder As ContentPlaceHolder
myContentPlaceHolder = CType(Master.FindControl("Content2"), ContentPlaceHolder)
Hope that saves someone some time!
Lo-Fi unzipping functionality for .net
Posted on October 2nd, 2008 • Filed under asp.net • No Comments
I’ve looked for unzipping functionality for .net a couple of times over the years and never found a totally satifactory solution.
In a recent project I had to grab an attachment from a POP box and then unzip the attachment.
I found an unzipping product called 7zip, it’s open source and there’s a command line version. See their downloads page for the commandline version - http://www.7-zip.org/download.html
Once you have this exe on your server you can easily unzip a file using the following code:
dim pathToZip as string = "c:\some\folder\file.zip"
Dim p As Process = Process.Start("C:\Program Files\7Zip\7za.exe", "e " & pathToZip & " -oc:\")
p.WaitForExit()
Obviously you are not limited to just unzipping, you can do anything the command line version does: zip, password protect a zip file, list the contents, etc
asp.net outputcache based on url
Posted on October 1st, 2008 • Filed under asp.net • No Comments
The outputcache page directive is very handy – and I tend to use this where appropriate on my usercontrols (.ascx)
You can optionally vary by querystring parameters.
Something that’s missing from the default options is to vary based on the actual url of the page. I was building a site where a random testimonial was pulled from the database and displayed in a small panel. I wanted this to stay constant on each particular url.
Here’s how to do it…
Add this to your global.asax
Public Overrides Function GetVaryByCustomString(ByVal context As HttpContext, ByVal custom As String) As String
Select Case custom.ToLower
Case "pageurl"
Return Request.FilePath.ToLower
Case Else
' Return String.Empty
Return MyBase.GetVaryByCustomString(context, custom)
End Select
End Function
Then on your ascx file add the following to the top
<%@ OutputCache Duration="60" Shared="true" VaryByParam="None" VaryByCustom="pageurl" %>