Run ASP.NET Web Server From Any Folder

Rob McLaws has this cool extension that lets you click on a folder and run a webserver from it.

The article, with some comments on alternative ways to do it, or ways to improve it can be found here.

http://weblogs.asp.net/rmclaws/archive/2005/10/25/428422.aspx

From the page:

I’ve been doing some web development work again lately, and I haven’t wanted to screw with setting up IIS on my virtual machine. The .NET Framework 2.0 comes with a built-in webserver, based on the old Cassini web server. So I wanted to be able to easily set up any directory to have its contents served up on an as-needed basis. The result is an addition to the Windows Explorer right-click menu, as shown below:

This is enabled by a simple modification to the registry. You can take the text below and dump it into a file named “WebServer.reg”, and then run it, to immediately get the item in the context menu.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINESOFTWAREClassesFoldershellVS2005 WebServer]
@=”ASP.NET 2.0 Web Server Here”

[HKEY_LOCAL_MACHINESOFTWAREClassesFoldershellVS2005 WebServercommand]
@=”C:\Windows\Microsoft.NET\Framework\v2.0.50727\Webdev.WebServer.exe /port:8080 /path:”%1″”

There are a couple caveats to this tool, however:

  1. The web server does not randomly assign a port number, so it has to be hard-coded into the registry.
  2. The web server does not automatically assign a virtual directory, so it will always be “http://localhost:port”
  3. A command prompt window will be spawned, and cannot be closed without closing the web server process.

Moving foward, there are a couple of options to fix these issues:

  1. Someone who knows more about variables in the registry can help me fix issues #2 and #3
  2. I can build a wrapper executable that solves all three problems
  3. Microsoft can put in a DCR and fix it before it goes gold in a few days.

#3 is not likely, so I’d love to hear what you think. Hope this trick is useful.

 

Dropdown1 has a SelectedValue which is invalid because it does not exist in the list of items.

‘Dropdown1’ has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

This error has been a real pain to deal with.  I finally have a work around and a guess at the underlying problem, and because I see that people are having this same problem I thought I would do a little writeup about it.

 

Obviously, if you look at the actual error message, there are cases when you can get this message and the problem is easy to solve.  e.g. you have a dropdown populated with EmploID values and you try to databind with: 

Value=’<%# Bind(“EmployeeName”) %>

 
Well of course that won’t work if all your values are integers and you are trying to set the value to “Bill Smith”.
 
But my problem was a bit more complicated.
 
I was building a web user control (aka ASCX) that was a “Location” dropdown.  The control basically contains a dropdown, and all the necessary logic to populate the dropdown, and I was trying to use this control inside the EditTemplate of a GridView.
 
To make things more complicated, in this case, I didn’t JUST want all the Locations, I also wanted to add a “Please Select” item to the list, with a value of 0.
 
But I kept getting the error above when the value was supposed to be 0.
 
I stepped through my code to try to find out what was going on.  I believe the issue is this:
 
On load, on value set, and on databind I call my “Populate” method.  This method sees if there are items in the dropdown.  If not, it gets a list from the database and binds to the dropdown.  It also checks if there is a “Please Select” item, and adds it if there isn’t one.
 
But still when Dropdown’s “DataBound” event was fired the last time, my manually added item had been removed.
 
So what I think what is happening is that :
1) I set the dropdown DataSource to a collection of business objects
2) I add the “Please Select” item
3) The dropdown is told to databind itself by the Gridview.
4) The dropdown says… “Oh, I do have a datasource, so I’m going to ditch my current list and rebind to that old collection”
5) The dropdown says “Holy shnikies, I am supposed to bind to value “0”, but I don’t have one!!”
 
I think there are 2 possible solutions.
 
1) I created an adapter class that only has Id and Description properties that are going to be used by the control, and I wrote a function to take my collection of business objects and build a collection of Adapters (adding my “Please Select”) to the list of adapters.  To see what I am talking about I will post this code below.
 
2) Manually add items to the dropdown instead of databinding, so that when the control is told to databind, it doesn’t have a datasource to use.
 
 
Here is my adapter code:
            '**** In my populate method
Me.LocationsDropdown.DataSource = LocationAdapter.LocationsToAdapters(locations)
Me.LocationsDropdown.DataTextField = "LocationDescriptionWithActiveAttribute"
Me.LocationsDropdown.DataValueField = "LocationId"
Me.LocationsDropdown.DataBind()



'*************************************
'*************************************




Private Class LocationAdapter
Public ciLocationId As Integer
Public csLocationDescriptionWithActiveAttribute As String
Public Property LocationId() As Integer
Get
Return ciLocationId
End Get
Set(ByVal value As Integer)
Me.ciLocationId = Value
End Set
End Property

Public Property LocationDescriptionWithActiveAttribute() As String
Get
Return csLocationDescriptionWithActiveAttribute
End Get
Set(ByVal value As String)
Me.csLocationDescriptionWithActiveAttribute = Value
End Set
End Property

Public Sub New(ByVal loc As Location)
Me.LocationId = loc.LocationId
Me.LocationDescriptionWithActiveAttribute = loc.LocationDescriptionWithActiveAttribute
End Sub
Public Sub New(ByVal Id As Integer, ByVal Desc As String)
Me.LocationId = Id
Me.LocationDescriptionWithActiveAttribute = Desc
End Sub

Public shared Function LocationsToAdapters(ByVal locations As List(Of Location)) As List(Of LocationAdapter)
Dim adapters As New List(Of LocationAdapter)
adapters.Add(New LocationAdapter(0, "--Location"))
For Each loc As Location In locations
adapters.Add(New LocationAdapter(loc))
Next
Return adapters
End Function

End Class
 
 
 

Windows XP "Help and Support" Start Menu

I never ever ever want to click on the “Help and Support” button in the WinXP start menu, but I frequently do when trying to click on something else (usually “Run…”).

I found this info on http://www.winxptutor.com/hidehlp.htm

To remove the Help and Support link from XP Start Menu, try this:

  • Right-click Taskbar, choose Properties
  • In the Start Menu tab, click Customize
  • Select the Advanced tab
  • Uncheck Help and Support option.
  • Click OK, OK and close the dialog

For Windows Classic Start menu, you may need to modify the registry directly:

  • Click Start, Run and type Regedit.exe
  • Navigate to the following location:

HKEY_CURRENT_USER Software Microsoft Windows CurrentVersion Explorer Advanced

  • In the right-pane, create a new REG_DWORD value named NoStartMenuHelp
  • Double-click NoStartMenuHelp and set it’s value to 1
  • Close Regedit.

Adding a Single Project To Source Control?

In previous versions of VS.Net you could take a project and go File->Source Control->Add Selected Project To Source Control and you could simply add the project to your VSS provider.

Now in VS.Net 2005 (maybe caused by VSS 2005, or VSS 2005 over HTTP), if you follow this route you will end up with File->Source Control->Add Project to Source Control, and your project will be added with the following path:

$MyProject.rootMyProject.

Thanks VSS, thanks a lot.  I really needed that .root in there.

To get around this problem, just add another existing project to VS.Net (creating a solution).  Now highlight the actual project you want to add and do:

File->Source Control->Add Selected Project to Source Control and all will be wonderful.

GirlSpeak to English

This is really funny!  I used to joke that I was going to write a book about this stuff, but this page about sums it up.

GirlSpeak to English Translator

Example:

She says                               English
———                              ——-
We need                                I want
Do what you want                   You’ll pay for this later
We need to talk                      I need to complain

ASP.NET 2.0 Page Life-cycle

This table is from: http://www.csharper.net/blog/page_lifecycle_methods_in_asp_net_2_0.aspx

 

Method Postback Control
Constructor Always All
Construct Always Page
TestDeviceFilter Page
Used to determine which device filter is in place, and use this information to decide how to display the page.
AddParsedSubObject Always All
Notifies the server control that an element, either XML or HTML, was parsed, and adds the element to the server control’s ControlCollection object.
DeterminePostBackMode Always Page
Returns a NameValueCollection object that contains the data posted back to the page. The presence of the page hidden fields VIEWSTATE and EVENTTARGET is used to help determine whether a postback event has occurred. The IsPostBack property is set when the DeterminePostBackMode method is called.
OnPreInit Always Page
Called at the beginning of the page initialization stage. After the OnPreInit method is called, personalization information is loaded and the page theme, if any, is initialized. This is also the preferred stage to dynamically define a PageTheme or MasterPage for the Page.
OnInit Always All
Performs the initialization and setup steps required to create a Page instance. In this stage of the page’s life cycle, declared server controls on the page are initialized to their default state; however, the view state of each control is not yet populated. A control on the page cannot access other server controls on the page during the Page_Init phase, regardless of whether the other controls are child or parent controls. Other server controls are not guaranteed to be created and ready for access.
OnInitComplete Always Page
Called after page initialization is complete. In this stage of the page’s life cycle, all declared controls on the page are initialized, but the page’s view state is not yet populated. You can access server controls, but they will not yet contain information returned from the user.
LoadPageStateFromPersistenceMedium Postback Page
Uses the Load method of the System.Web.UI.PageStatePersister object referenced by the PageStatePersister property to load any saved view-state information for the Page object.
LoadControlState Postback All
Restores control-state information from a previous page request that was saved by the SaveControlState method.
LoadViewState Postback All
Restores view-state information from a previous page request that was saved by the SaveViewState method.
OnPreLoad Always Page
Called after all postback data returned from the user is loaded. At this stage in the page’s life cycle, view-state information and postback data for declared controls and controls created during the initialization stage are loaded into the page’s controls. Controls created in the OnPreLoad method will also be loaded with view-state and postback data.
OnLoad Always All
Notifies the server control that it should perform actions common to each HTTP request for the page it is associated with, such as setting up a database query. At this stage in the page lifecycle, server controls in the hierarchy are created and initialized, view state is restored, and form controls reflect client-side data.
RaisePostBackEvent Postback All
Notifies the server control that caused the postback that it should handle an incoming postback event.
OnLoadComplete Always Page
At this point in the page life cycle, all postback data and view-state data is loaded into controls on the page.
OnPreRender Always All
Notifies the server control to perform any necessary prerendering steps prior to saving view state and rendering content.
OnPreRenderComplete Always Page
At this stage of the page life cycle, all controls are created and the page is ready to render the output. This is the last event called before the page’s view state is saved.
SaveControlState Always All
Saves any server control state changes that have occurred since the time the page was posted back to the server. If there is no state associated with the control, this method returns a null reference. Custom controls using control state must call the RegisterRequiresControlState method on the Page before saving control state.
SaveViewState Always All
Saves any server control view-state changes that have occurred since the time the page was posted back to the server. If there is no view state associated with the control, this method returns a null reference.
SavePageStateToPersistenceMedium Always Page
Saves any view-state and control-state information for the page. The SavePageStateToPersistenceMedium method uses the Save method of the System.Web.UI.PageStatePersister object referenced by the PageStatePersister property to store view-state and control-state information for the page.
Render Always All
Initializes the HtmlTextWriter object and calls on the child controls of the Page to render. The Render method is responsible for creating the text and markup that is sent to the client browser. The default Render method calls RenderChildren to write the text and markup for the controls contained on the page.
OnUnload Always All
Used to do target-specific processing in the Unload stage of the control lifecycle. Typically, these are cleanup functions that precede disposition of the control.