Archive for the ‘asp.net’ Category
Observations on moving code from VB.NET to C#
Posted on March 25th, 2011 • Filed under asp.net, csharp • No Comments
I’ve been programming in VB.NET since the dotnet framework was launched, but never really felt the need to try C#. I’ve just decided to try it…
Since both VB.NET and C# use the same framework I was expecting that switching between the 2 should be easy. We’ll it’s not quite as plain sailing as I expected.
I’m moving an existing project over piece-by-piece. As I come across stumbling blocks I’m adding them to this post.
Stop validation controls scrolling to top of the page
Posted on December 16th, 2010 • Filed under asp.net • No Comments
This has been bugging me for ages. When a validation control fires the page scrolls to the top. Very annoying when your form is further down the page.
A very easy way to stop this happening is to override the window.scroll function.
Add the following to the page with the form on.
<script type="text/javascript">
window.scrollTo = function( x,y ) {
return true;
}
</script>
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 • 1 Comment
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 asp.net, Javascript • 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 asp.net, Javascript • 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.
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">// <![CDATA[
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
Update 2011
var controltovalidate = jQuery(source).attr("controltovalidate");
No longer works... just use
var controltovalidate = source.controltovalidate;
Instead - easy peasy
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!