Wednesday, January 19, 2011

How to create the ASP.Net membership tables

Sometimes web hosts (such as godaddy) will set up the aspnet membership tables for you in your database as part of the setup.

When you need to set them up locally, use the following .Net framework command
//For servername you can use "(local)" if the database is a local SQL Server install
System.Web.Management.SqlServices.Install(txtServer.Text, txtUserName.Text, txtPassword.Text, txtDatabase.Text, SqlFeatures.All);

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>