Tuesday, August 25, 2009

ASP.NET MVC Numeric Textbox

Introduction

So I've been having a bit of a look at ASP.NET MVC as part of the public facing section of the Silverlight project I am working on. I've been able to get running the main part but wanted to neaten up the validation. I wanted to be able to add a TextBox on my form that would only accept a number. From what I could see jquery was the way to go. (If you have a look here you can see an example of how the numeric entry works.) But that raised a number of questions:

  • How to use jQuery?
  • How to add scripts?
  • How to add the TextBox?
  • How to specify the class of a TextBox?

Using Google it took quite a bit of time to track down all the pieces to make this work. As a result, I've written this one post that hopefully brings it together.

Making A Head Content Available

In order to use jQuery, you need to be able to add some scripts to the page generated by the view. This really needs to be in the head section, but on my view pages, there was no where to add it. I had an asp:content tag for TitleContent and one for MainContent but nothing where I could put the script.

In the Site.Master page I modified the head section from this:

<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>


To this:

<head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<asp:ContentPlaceHolder ID="head" runat="server" />
</head>


See the ContentPlaceHolder? That will allow me to define a head section in my view pages.

Adding The Scripts

Now that I have a ContentPlaceHolder, I can edit my view pages to add scripts. Just below the Page command I can add the code that will appear in the head section of the page.


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Construction.Service.Claim>" %>
<asp:Content ID="Scripts" ContentPlaceHolderID="head" runat="server" >
<script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> 
<script type="text/javascript" src="/Scripts/jquery.numeric.pack.js"></script> 
<script type="text/javascript">
$(document).ready(function() { 
$("input.numeric").numeric(); 
}); 
</script>
</asp:Content>




Few things to note here:
  • Everything in the asp:Content ID="Scripts" section will appear in the head section on the generated page. Just what I wanted.
  • In the scripts I've added reference to "/Scripts/jquery-1.3.2.min.js". This is the jQuery library that ships with ASP.NET MVC V1.0.
  • The next line is for "/Scripts/jquery.numeric.pack.js". This tells the page that we want to use the jquery numeric library which can be found here. I downloaded this file and then added it to the scripts section of my project. This line in my head section references that file. That link shows the jQuery numeric usage. So once you have this basic one going, you can refer back to that link to see how you might extend your options.
  • The next part is the 'ready' function for jQuery. I'm new to jQuery but from what I can work out, this is the function where you put all your stuff to prepare things for later use. In this case we are telling the system that things identified as 'input.numeric' will be numeric. The 'input' simply refers to the fact this will be associated to an input textbox. The '.numeric' indicates that this will be used by any input control that is of *class* 'numeric'. We'll see how this works next.

Making A TextBox Numeric

The last part is to create a TextBox in the view that needs to be numeric entry only. In my case I used code like this:


<%= Html.TextBox("ApprovedPercentage, ViewData.Model.ApprovedPercentage,
new { size = "5" ,@class="numeric" })%>


This is basically just creating a textbox on the form. The magic that pulls all this together is the clause:
@class="numeric"


This links it back to the input.numeric reference in the script and the textbox will be limited to a numeric. You can have as many textboxes on the form as you require and provided you include the '@class="numeric" ' part, they will all be numeric entry only.

You can set the textboxes up by id if you want. So in the ready function you would have this:

$(document).ready(function() { 
$("input#approvedpercentage").numeric(); 
$("input#approvedamount").numeric(); 
}); 


In this case, the # means treat the following word as the id of the textbox, not the class. You have to specify each one.

Then on the textbox you would have:

<%= Html.TextBox("ApprovedPercentage, ViewData.Model.ApprovedPercentage,new { size = "5" ,id="approvedpercentage" })%> 


Note, I didn't set the @class this time, I set the id.

Conclusion

Its actually pretty easy to get the numeric textbox working once you know all the pieces. Hopefully, someone finds this useful!

Friday, August 21, 2009

Lite Versions

Its the last month of winter and its 30 degrees. Celsuis - not farhenheit. Absolutely gorgeous weather. Bring on summer!

Its a Friday. I've had a pretty good couple of weeks doing some Delphi work. Now its time to get back to some iPhone lovin'. So today I've produced lite versions of Taekwondo ITF Patterns and Beach Rescue. In the case of ITF Patterns, only the first three patterns will be available. For Beach Rescue, a game will end when the player reaches 100 points.

The ITF Patterns sales have gone down a bit. Still selling but not as well as it was. I swapped war stories with the developer at Skunkscape who has done a similar program for WTF taekwondo. (Can you believe that the two best tkd apps in iTunes are develop within 100km of each other? Must be the sunny weather.) Seemed like a nice bloke. I won't discuss his sales but I was very impressed by the number of downloads that his lite version has had. So I figure its worth a try. I'm pretty confident in my app to let people try it before buying it.

Beach Rescue sales on the other hand have been pretty dismal. We'll see if a lite version can rescue Beach Rescue! With games though I do think I will have to spend some money on advertising. Still not willing to put my hand in my pocket for that just yet.

Been working after hours on another app which I hope will be ready soon. Just gathering the content for it. The programming work has all been done. Hopefully finished soon!


Wednesday, August 12, 2009

iPhone Musings

Been a little while since my last post. Finally got google ads working after getting a letter from Apple saying I was authorised to use iPhone in an ad. First couple of days the ad was getting heaps of hits from about 60 different countries. I think that was due to having quite a high bid for the keywords. I've had to reduce that though. Given the small profit margin per sale, you have to be careful the adword costs don't exceed profits. Since then the number of clicks has reduced to a few a day. Been playing around with CPC rates and ad text. Still not real sure how some of these quality scores are calculated though...

So was it worthwhile? Can't really say it has been. Sales haven't gone up at all. They remain consistent. I was hoping for a bit of a spike. I'll keep it going because its not costing me a lot and it gets me out there.

I was put on to an app called AppViz which is quite cool. The best feature is being able to see any reviews from any app store worldwide. I had thought I'd received no reviews but there as one from the UK for 5 stars. The person was also asking for some blackbelt patterns to be included as well as self defence. At this stage I'd need more requests before committing to adding more content.

But I have got a great idea for a new feature iI can apply to the existing content. I already think I have the best TKD app on the appstore, but this feature would cement it. Just a case of whether I think its worth spending the time on it or not.

Beach Rescue has not gone particularly well. Made some sales but no where near covering costs, so while games are fun to work on I can't see any more happening at the moment. I've sent out requests for reviews and promo codes but so far nothing much. Most traffic has been from the cocos2d forum where I had told them about the game I had developed with the cocos2d framework. For this app, I might have to spend some money on advertising and just get it out there a bit more.