I just got asked a question that comes up quite often when someone starts working with ASP.NET...

 The question was:

"I have an asp.net 2.0 page which all I’m trying to do is populate a label control with the selected text of a dropdownlist control, whenever a button control is clicked.  It always puts the name of the first item in the dropdownlist in the label control, no matter what item I have selected.  Any ideas?"

The Code was:

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load With Me.DropDownList1.Items .Clear() .Add("Item 1") .Add("Item 2") .Add("Item 3") .Add("Item 4") End With End Sub Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = DropDownList1.SelectedItem.Text End Sub End Class

The issue is that the Page_Load event will fire each time the page gets rendered, this includes on the post back from the button click so the list will be cleared then repopulated and the first item will always be selected once the Button1_Click event fires.

There is a good MSDN article for this ASP.NET Page Life Cycle Overview that covers this and cool diagram by Leon Andrianarivony ASP.NET Page Lifecycle diagram.

To solve this issue all that you need to do is use the Page.IsPostBack property in the Page_Load event like:

    Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then With Me.DropDownList1.Items .Clear() .Add("Item 1") .Add("Item 2") .Add("Item 3") .Add("Item 4") End With End If End Sub

This assumes ViewState is on...

Technorati tags: , ,

Posted by: woodyp
Posted on: 9/21/2006 at 2:37 AM
Categories: .NET | ASP.NET | VB.NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed
 
 

Tonight I will get to talk to a class at San Diego State University about how to use Visual Studio to complete a class assignment.

I will do a in class demonstration but I will also show a lot of the free tanning resources Microsoft has for learning the toolset. I hope others can find this information useful.

First Free software!

There are a set of products Microsoft offers for free that will allow students, and hobbyists program on the windows platform. They can be found at the Visual Studio Express home page. There you can download two different types of Visual Studio, one for doing web development and four different ones for doing Windows development. You can also download SQL Server Express.

For the work in this class students will need one of the Windows Development Express products and SQL Server Express. I also suggest that they get the SQL Server Management Studio Express and the Northwind and Pubs sample database.

Now we have an environment to learn with... How about some lessons?

MSDN Virtual Labs

Some of my favorite things that are available are the MSDN Virtual Labs the Virtual Labs are way cool in that you don't need to install any of the above software locally. You will need to have JavaScript enabled, IE6.0 or higher and the Virtual Server VRMC Advanced Control but that is a small price to pay for free hands on training. I was shocked to see that we have not updated all of the labs to Visual Studio 2005 but you and still learn the basics in 2003 and there are labs what is new for Visual Studio 2005.

Learn Visual Studio

The Learn Visual Studio site on MSDN lists other resources available, these include Learning Plans, E-Learning, Classroom Training, Available Books and Exams.

The Learning Plans are quite cool, you identify what type of development you want to learn and Microsoft will show you resources and let you build a custom learning plan.

More Resources

MSDN
MSDN Flash
Future Versions of Visual Studio
CodeZone
San Diego .NET Developer Group
San Diego .NET User Group

Technorati tags: , , ,

Posted by: woodyp
Posted on: 9/15/2006 at 3:34 AM
Categories: .NET | C# | VB.NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed
 
 

Scott Guthrie has posted details of the changes coming to Atlas!

"Atlas" 1.0 Naming and Roadmap


Posted by: woodyp
Posted on: 9/12/2006 at 7:33 AM
Categories: .NET | ASP.NET
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Post RSSRSS comment feed