Archive

Archive for September, 2010

StringUtil class – Space Before a Capital Letter

17 September, 2010 Leave a comment

Hi,

I often use enumerations in code that I want to display as text on the screen. For example the state of an object recorded in an enum like

public enum StageType
{
PreRelease = 0,
Proposed = 1,
Open = 2,
Closed = 3
}

So if I get the matching value out of the database and I want to render a textual representation of the enum to the UI I migth do:

lblStatus.Text = ((StageType) iStage).ToString(); // where iStage is an int value representing the enum.

So this is great appart from the first item PreRelease will be shown without a space.

Step in my amazing class which will parse the string and add a space before a capital so “PreRelease” becomes “Pre Release”
This way I can use any Camel case enum string and have it render to the UI nicely.


public static class StringUtil
{

///
/// Returns a string with a space after a capital
///
///
///
public static string SpaceOnCapital(string word)
{
StringBuilder sb = new StringBuilder();

foreach (char c in word)
{
if (sb.Length == 0)
{
sb.Append(c);
}
else
{
// not the first character
// ch
if (IsCapital(c))
{
sb.AppendFormat(” {0}”, c);
}
else
{
sb.Append(c);
}
}
}

return sb.ToString();

}

public static bool IsCapital(char c)
{
int ascii = (int)c;
return (ascii >= 65 && ascii <= 90);
}
}

Cheers

Categories: c#

Create Visual Studio Project with the correct directory structure

10 September, 2010 Leave a comment

This ones just for me.

I have been using TFS and when I read the best practice documentation for how to setup your projects I have ended up having a preference for how i like my directory structure.

Assume you have a project  call BudgetForecast, I would  end up with a folder structure like

c:\Dev\BudgetingProject\Main\Source\BudgetForecast\Source\

Under the BudgetForecast directory would be the BudgetForecast.sln and in the Source subdirectory would be the Project file.

So how to get Visual studio to do that.

  1. Create a new solution
  2. Set the location to c:\Dev\BudgetingProject\Main\Source\
  3. Set the Name to “Source” (this also sets the solution name)
  4. Set the SolutionName to “BudgetForecast”
  5. make sure the “Create Directory for Solution”  is picked
  6. click OK

Your soltuion is now created but your project is called source, so just rename to project to BudgetForecas, save that and your done.

I wrote this as I can never remember whether Name or solution Name needs to be changed to source and invariably I get it wrong

Cheers

Categories: Visual Studio

jQuery – enable / disable (readonly) and how to reference .Net Server side Controls

8 September, 2010 Leave a comment

Ok, I am just starting to play around with jQuery and these are a couple of tips that I have found useful.

1. How to access .net server side controls
The code to get the value from a html input control <input id=”name”> might be as follow:
var name = $(‘#name’).val();

For a server side control like <asp:TextBox ID=”txtName” runAt=”sever” /> you can reference it as follows:
var name = $(‘#<%=txtName.ClientID%>’).val();

I have a user control for date selection called the DateSelector, and it has a control inside it called txtDate which stores the selected date.
If I want to get the value from within a user control like this I can do:

var dte = $(‘#<%=dteDateSelector.ClientID%>_txtDate’).val();

2. Disable a control
In this case I want to disable a text box from the txtName example above:
$(‘#<%=txtName.ClientID%>’).attr(“disabled”, true);
to clear it
$(‘#<%=txtName.ClientID%>’).removeAttr(“disabled”)

A problem with disabling controls in this way is that their values cannot be read in javascript, so another way is to use the “ReadOnly” attribute
$(‘#<%=txtName.ClientID%>’).attr(“readonly”, true);
to clear it
$(‘#<%=txtName.ClientID%>’).removeAttr(“readonly”)

Cheers

Categories: ASP .Net, jQuery