Monday, November 29, 2010

How to strip non-alphanumeric chars from a string

The example below also leaves the space character and commas intact.

      private string StripSpecialChars(string inputString)
{
return (Regex.Replace(StripHTML(inputString), "[^A-Za-z0-9 ,]", ""));
}

How to strip HTML tags out of a string

      static string StripHTML(string inputString)
{
return Regex.Replace
(inputString, "<.*?>", string.Empty);
}

Saturday, November 13, 2010

How to reseed an identity field

When I create e-commerce sites, the order numbers always start at 1. My customers like to pretend that they've had lots of orders when they launch, so I need to update their order number to something higher. The problem is this is always the identity field of the table. This command does the trick. It doesn't update the old order numbers, but I don't need those updated so it's no big deal.

DBCC CHECKIDENT('order', RESEED, 1000070 )

Tuesday, October 19, 2010

How to set the selected item in an MVC Dropdownlist

The first parameter to the Html.DropDownList needs to be a name that does not match the name in your model. Below I used "OptionId" instead of "Options".

View

<%= Html.DropDownList("OptionId",Model.Options) %>

Controller

SelectListItem item1 = new SelectListItem();

item1.Text = "Red";

item1.Value = "Red";

item1.Selected = false;

viewModel.Options.Add(item1);

SelectListItem item2 = new SelectListItem();

item2.Text = "Green";

item2.Value = "Green";

item2.Selected = true;

viewModel.Options.Add(item2);

SelectListItem item3 = new SelectListItem();

item3.Text = "Blue";

item3.Value = "Blue";

item3.Selected = false;

viewModel.Options.Add(item3);

Tuesday, October 5, 2010

How to create a modal dialog in jQuery and prevent scrolling in background


$(document).ready(function() {

$("#dialog").dialog({ autoOpen: false

, modal: true

, buttons:

{

"Cancel": function() {

$(this).dialog("close");

$("body").css("overflow", "auto"); }

, "Save": function() {

$(this).dialog("close");

$("body").css("overflow", "auto"); }

}

});

});


function RenameFile(filename, id) {

$("#OldFilename").html("From: " + filename);

$("#NewFilename").val(filename);

$("#dialog").dialog("open");

$("body").css("overflow", "hidden");

}


<div id="dialog" title="Change Filename">

<p id="OldFilename"><b>From: </b>oldfilename.doc</p>

<p><b>To: </b>

<input id="NewFilename" type="text" style="width:200px" /></p>

</div>


<a style="cursor:hand;text-decoration:underline;color:Blue" onclick="javascript:RenameFile('<%= Html.Encode(doc.FileName) %>',<%= doc.PathID %>)">rename</a>



Wednesday, September 29, 2010

How to resolve unresolved type System.Web.Security.MembershipCreateStatus which appears after converting from vs2008 to vs2010

I upgraded a project I was working on from vs2008 to vs2010 and started getting an error for an unresolved type: System.Web.Security.MembershipCreateStatus.

My project was targeting the .NET Framework 3.5 and during the conversion, I selected not to change the target to the .NET Framework 4.0.

This issue was occurring in my Test project which was generated automatically when I created an MVC 2 project in vs2008. In vs2010, test projects cannot target the 3.5 Framework (by design). The quick fix was to delete AccountController.cs in the test project.

A better solution would be create another MVC2 project in vs2010 and use the generated Test project.

Thursday, September 23, 2010

How to add a configuration file to your class library

I have a web project with some configuration settings in the web.config. I added a class library to the project and realized I could not access those values from the class library. The solution is to add an app.config file to the class library project and copy the configuration settings from the web.config to the app.config.

To access ConfigurationManager from the class library, I need to add a reference to the System.Configuration .Net assembly.

If you don't like having the settings in both places, you could retrieve the configuration values for the web application by using the class library (app.config), and remove the configuration values from the web.config.


Wednesday, September 22, 2010

How to insert a tab in Microsoft Word when your cursor is in a table cell

When you're working in a table in Word, the tab key will bring you to the next cell (not insert a tab).

When you want to insert a tab, use [Ctrl][Tab] instead

Tuesday, September 21, 2010

How to create FREE flowcharts online

This is a pretty cool product. http://www.gliffy.com It's easy to use like Visio, which no longer comes with my MSDN subscription :( . You can create .pdf, png or jpgs.


Monday, September 20, 2010

How to display hidden files and folders in Windows 7

1. Start | Control Panel | View By: Category
2. Click Appearance and Personalization
3. Below "Folder Options" there is a link for "Show hidden files and folders"
4. Then select the radio button "Show hidden files, folders and drives"
5. Click [ok]

How to use a hidden input in MVC

While working on an e-commerce site, I needed to update my order total and submit it with the other form variables. My order total displayed to the user was just text, which doesn't get passed to the controller when submitted, so I needed a hidden input that I could update.

My html looks like this:

<p class="display_currency"><span id="totalprice">$0.00</span></p><%= Html.HiddenFor(m=>m.TotalOrder) %>

and my javascript. (snippet is from an UpdateTotal() function)

var total = 500.25
$("#totalprice").html("$" + total.toFixed(2));
$("#TotalOrder").val(total.toFixed(2)); //hidden control for model

when submitting the form, the model contains the correct value for TotalOrder

Saturday, September 18, 2010

How to copy color syntax from VS2010 to Blogger

Update: I just noticed that this is working in Chrome (which is what I usually use), but on in Internet Explorer 8

Step 1: Install Productivity Power Tools (which include HTML copy)
Step 2: Highlight some code and copy to the clipboard with [Ctrl][c]
Step 3: In Blogger, paste with [Ctrl][v]

If pasting HTML, you'll need to encode your less than signs. (Change < to &lt;)

               switch (pricedisplaytype)
{
case (int)ProductPriceDisplayType.DontShowPrice:
item.ItemTitle = description;
break;
case (int)ProductPriceDisplayType.ShowPriceEach:
item.ItemTitle = String.Format("{0} (${1:n3} each)", description, qty.Price / qty.Quantity);
break;
case (int)ProductPriceDisplayType.ShowTotalPrice:
item.ItemTitle = String.Format("{0} (${1:n2})", description, qty.Price);
break;
}

How to select a radio button in JQuery

To set a radio button value using JQuery, you need to set the the checked attribute. The name attribute will let you group the radio buttons and the id attribute will help you find the radio button using jQuery

function SetRadioButtonValueToThree()
{
$( "#radiobtn_3" ).attr( "checked" , "checked" );
}

<input type= "radio" name= "myradioGroup" id= "radiobtn_1" value= "1" checked= "checked" />One<br/>
<input type= "radio" name= "myradioGroup" id= "radiobtn_2" value= "2" />Two<br/>
<input type= "radio" name= "myradioGroup" id= "radiobtn_3" value= "3" />Three<br/>

How to create HTTP POST and GET methods with the same signature

When using Ajax Uploader (a very cool component) and MVC, I ran into a situation where I needed the same signature for the HTTP GET and HTTP POST controller actions. The compiler wouldn't let me use the same name. The ActionName attribute solved that problem.


[ActionName( "UploadArtwork" )]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult UploadArtworkGet(string dataMyComponentUses)
{
...
return View( "UploadArtwork" );
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadArtwork(string dataMyComponentUses)
{
...
return View( "UploadArtwork" );
}

How to return a pdf file in MVC (instead of preview by browser)


public ActionResult GetPDF(string filename)
{
return File(filename, "application/pdf" , Server.HtmlEncode(filename));
}