Check Out Dave's New game for iPhone and iPod Touch: Smiled Out!

Binding a Dropdown List in ASP.Net Inside a ListView

I like to blog things that I have to look up. Here’s a quickie which solves a problem I was having.

In my table, I have a field “PayType” which is a char(1) and has valid values “S” and “H”. These mean “Salary” and “Hourly”, respectively.

I am showing this in a ListView in my screen I’m developing. So, when editing the record, I naturally wanted a dropdown box where you can choose “Hourly” or “Salary”. Note this will only work if you know what values you want in your dropdown box. If the selections are dynamic, then you will probably have to have a databound event and build the box manually.

I wanted it to default to the selected type for the record I’m editing when I click on edit, but could not find anything in the intellisence that looked correct for my Bind() call.

1
2
3
4
5
6
7
<asp:DropDownList 
      ID="ddPayType" 
      runat="server" 
      SelectedValue=<%# Bind("PayType") %>  >;
          <asp:ListItem Text="Salary" Value = "S" />
          <asp:ListItem Text="Hourly" Value = "H" />
</asp:DropDownList>

The answer was to Bind on the “SelectedValue” parameter. I was thrown off because SelectedValue does not seem to pull up in Visual Studio 2008 as an option on the list when writing code in the markup editor. But it correctly updates the value when the Update command is executed.


Follow Dave on Twitter
  1. Dan says:

    Hey, I was wondering if there is any way i can get the Recovery (D:) back on to my computer something happened and its gone when i press f11 nothing happens. Can u please help me out?

  2. Dave Andrews says:

    Not sure what it is you mean, Dan. If you are wanting to run a recovery process on your computer, usually the manufacturer can send you a CD that will restore it back to its original state.

  3. LL says:

    Dave, I watched your video on How to Consolidate Worksheets in Excel. I have VISTA and am having quite the difficulty in performing this task, albeit is it more complex data than what you were working with. I imported data from QuickBooks and am attempting to do TRENDS for my company for the past 2.5 years. So for example, when I started out we had some expenses, but now they are expanded and there are more categories. Is there anyway you have time to walk me through consolidating this expansive data?

    Thanks!

    Lesa

  1. There are no trackbacks for this post yet.

Leave a Reply