Tuesday, 26 January 2010

Formatting for currency

This will turn 20000 into £20,000.00:
String.Format("{0:C}", myValue)
'myvalue' must be numeric. If it's a string, use Convert.ToDouble first.

Friday, 22 January 2010

"Object doesn't support this property or method" with theform.submit

In a recent post I described how easy it was to maintain scroll position for a user during a postback, by adding this to the page directive:
MaintainScrollPositionOnPostback="true"
This did cause me problem with one page where I implemented it. Only in Internet Explorer (other browsers seemed ok) did I get this javascript error:
Microsoft JScript runtime error: Object doesn't support this property or method
And it was referring "theform.submit"

It took me a while to resolve. I found that I had a user control on the page with a button that had the property name="submit". Removing this resolved the problem.

Monday, 18 January 2010

URL rewriting and multiple parameters

I'm using the Intelligencia URL rewriter and I needed to handle more than one parameter. This is my rewrite instruction in the web.config file:

SIFR flash replacement with AJAX update panel

If you have an ajax update panel only partially updating your page you'll find that the standard SIFR implementation doesn't work on any items in the update panel. To overcome this, you need to wrap the whole contents of sifr-config.js in a function. I called it callSIFR.

Then you need to write the call to this function to the client at the PreRender event from your master page.
protected void Page_PreRender(object sender, EventArgs e)
    {
    ScriptManager.RegisterStartupScript(this, typeof(Page), "SIFRcall", "callSIFR();", true);
    }

Sunday, 17 January 2010

Maintaining user scroll position on postback

When your user is scrolled down on a 'tall' page and does something that evokes a postback, like submitting some form data, we often want to return to their scroll position after postback. This is a particularly common requirement when reporting validation failures to the user on a form submitted when we're using server-side validation rather than javascript.

ASP.net makes this incredibly easy for us. It's as simple as adding this to your page directive:
MaintainScrollPositionOnPostback="true"
Don't you just love it?

Wednesday, 13 January 2010

Passing a parameter/value from a host page to a user control

This goes in your control:
private String _ValueID;
    public String ValueID
    {
        get { return _ValueID; }
        set { _ValueID = value; }
    }
And you can set the value in your host page either like this:
<uc2:SubNav ID="SubNav1" runat="server" ValueID="whatever" />
or in your host code-behind like this:
SubNav1.ValueID = "whatever";

Thursday, 7 January 2010

Flushing the DNS cache

I've had one or two run-ins with unforeseen DNS caching. Today it reared its ugly head again when I moved a site I'm building to a new URL.

To clear the DNS cache on a windows machine, run this from the command prompt:
ipconfig /flushdns
On Vista, you'll have to launch the command prompt by right-clicking and selecting 'Run as Administrator'.

Tuesday, 5 January 2010

Configuring SMTP in web.config

 <system.net>
    <mailSettings>
      <smtp from="fromuser@fromaddress.net">
        <network host="smtp.myhost.com" password="abracadabra" userName="myusername"/>
      </smtp>
    </mailSettings>
  </system.net>

Directory.Delete results in Session_End

I spent ages trying to work out why I was being logged out the web app I was building. Well it turns out that it was happening at the point where I was deleting a directory no longer needed for user uploads.

At this point it seems that a recompile is triggered, and therefore the session is lost. Now there's one to look out for!

Sunday, 3 January 2010

Pagination - rounding up my number of pages

I had to manually code a bit of pagination in C# today. To work out how many pages there are in total I started to look into testing for an integer when dividing the results by the results-per-page. Instead I used this simple bit of maths:
numberOfPages = (totalResults + resultsPerPage - 1)/resultsPerPage;
And, if you're interested, here's the complicated way of doing it:
pageCount = int.Parse(Math.Ceiling(Decimal.Divide(TotalCount, PageSize)));
if (pageCount == 0) pageCount = 1;