Monday, 24 August 2009

Using aspnet_regsql via the command line to setup membership roles and profiles only

When you setup the asp.net membership, profile, etc using the aspnet_regsql wizard its pretty much an all or nothing affair - it adds all of the possible features in one go with no option to select which features are added.

When setting up the login systems you only need to add the following feature support: Membership, Role Manager and Profiles. The two remaining features (Personalization and SQL Web event provider) are not needed.

So to only install the features you require you should use the command line version of the tool.

Note: The version listed below (v2.0.50727) is valid for both .net 2.0 and .net 3.5. Without going into the technical details .net 3.5 is a layer on top of 2.0 and the same tool is used for both versions.

If you are working with a version that is newer than 3.5 then check that versions folder for an updated version of this tool as it may be updated in future versions.

Getting the command window open (Windows Vista)

  1. Click the start button then copy and paste this into the search box C:\Windows\Microsoft.NET\Framework\
  2. While holding the shift key down, right click on the folder called "v2.0.50727".
  3. You will see the normal context menu but with an extra option called "Command Window Here"

Getting the command window open (Older versions of Windows)

  1. Click the start button, select Run.
  2. Type "cmd" without the speech marks and press enter
  3. Copy this line of text: cd C:\Windows\Microsoft.NET\Framework\v2.0.50727\
  4. Right click in the command prompt window and choose paste.
  5. Press enter

Setting up the database

Now that you are in the correct location you should type the text below into the command line.

aspnet_regsql.exe -E -S .\SQLEXPRESS -A mrp -d databasename

The -E, -S, -A and -d are all case-sensitive so make sure you type the line exactly!

Replace SQLEXPRESS with the instance name of your database server - if you're using the free version of Sql Server then you will almost certainly be called .\SQLEXPRESS. The .\ bit at the start means "this computer" so if your sql server is on a different server then put the computer name there such as COMPUTER\SQLEXPRESS.

Replace databasename with the name of the database you want to add these features to.

What do the command line parameters mean?

You can get a full explanation of all the options available by typing:

aspnet_regsql -?

Here is a run down of the parameters we used:

-E Auth with windows credentials -S server instance -A services to add (membership, roles, profiles) -d database name

REMEMBER - Parameters are case sensitive

Did it work?

Press enter and you should see this:

Start adding the following features:
Membership
Profile
RoleManager

.......

Finished.

C:\Windows\Microsoft.NET\Framework\v2.0.50727>

A final tip

One thing that I did not realise the first time I used these features is that you can use an existing database when you set these tables up. In my first site that I used these features I ended up with two databases like ClientDatabase and ClientDatabaseAspNetDB because I thought that it would overwrite my data! This is NOT the case! You can specify the same database you are using for all your other development.

Further Reading

kick it Shout it vote it on WebDevVote.com

Thursday, 20 August 2009

Setting a default button so your users can press enter to submit your form

You might have noticed that when you press enter to submit a form in your asp.net page it just refreshes the page instead of submitting as you would expect.

The reason behind this is that asp.net web forms uses the <form> html tag to power the whole postback mechanism. This means that the default behaviour of submitting the form is still occurring but as far as asp.net is concerned you have just submitted its postback form with no commands so it just refreshes.

DefaultButton property

Anyway enough of the back story. The way around this is to set the DefaultButton property. The DefaultButton property was added to the HtmlForm and Panel classes in asp.net 2.0.

You can set this programmatically:

Panel1.DefaultButton = Button1.UniqueID;

or via the code markup:

<asp:Panel ID="Panel1" runat="server" DefaultButton="Button1">
</asp:Panel>

Per panel

The best use of the DefaultButton property is at the Panel level. The asp.net webforms postback model defines a single html form that all elements of the page are contained in.

If you have for example a login button and a search box in your masterpage, a feedback form in the content area then you have several logical forms contained behind the scenes in a single html form tag. Your users will expect to press enter in the search box and trigger a search. They dont want to see your feedback form validators kicking up a fuss.

By putting each logical form inside an you can set the default button to improve the user experience.

At the form level

As I have already hinted you can also do this at the page level by setting the DefaultButton on the behind-the-scenes page level form tag. As you will already see this is not a good idea for serious projects but you may want to set it in certain circumstances.

If you are in a content page of a masterpage then you will need to do it in the code behind:

this.Form.DefaultButton = this.Button1.UniqueID;

Accessibility

As an added bonus you are also improving the experience of visually impaired or otherwise disabled visitors. By defining the default button you are helping the screen reader software to have a fighting chance at understanding your asp.net page.

Gotcha - Buttons and ImageButtons only (not LinkButtons)

You can't set a <asp:LinkButton> as your DefaultButton. Technically you are supposed to be able use any control which implements IButtonControl but you cannot use the LinkButton. I think this is probably something to do with the fact that LinkButton is a javascript postback while the other buttons render out to proper form elements.

Further Reading

Thursday, 13 August 2009

ModalPopupExtender Sys.ArgumentNullException Error

Are you are seeing this javascript error when you try to run a page with a ModalPopupExtender in it?

Error: Sys.ArgumentNullException: Value cannot be null.
Parameter name: element

Then the fix is probably pretty simple - you have put incorrect ID's in either the OkControlID or CancelControlID attributes.

You don't have to specify the OkControlID or CancelControlID's to use the ModalPopupExtender but if you do and you later rename the control or you simply mistype when setting it up the compiler will not catch this.

The error message could be more helpful but hopefully you will have found this article in your favourite search engine without much trouble!

Here is the official documentation and demo page for this control:


-->