ASP.NET Tidbit: I really like the Asp.NET dropdown server control. While everyone is marveling about the grid , for today I am simply impressed by the humble drop-down list control. Here is a sample of binding the list to a data reader (based on a software defect tracker that I'm working on).
Bugs.ProjectAreasDB dbprojareas = new Bugs.ProjectAreasDB(); cboAreas.DataSource=dbprojareas.GetAreasPerProject(projSelectedID); cboAreas.DataTextField="ProjectArea"; cboAreas.DataValueField="ixProjectArea"; cboAreas.DataBind();
So provided that the call to the business object return a datareader, this snippet binds the drop down to the contents of two fields within the reader. Notice the DataTextField and DataValueField properties. The DataTextField is what the user sees when the dropdown displays in an HTML page. you can think of the DataValueField as the equivalent of an HTML value attribute. It usually contains information that is written back to the DB. Nothing new with that concept right? But the execution in ASP.NET is nicely done. In my example the DataValueFields ends up holding the PK of the ProjectArea table.
Now then - what if you already have a record in the DB and you need to display the value thats alraedy in the DB as the selection of the dropdown? Lets say the DB has an ixProjectArea value of "1" . At some point when thepage loads, the code snippet above executes and fills the dropdown with all the possible choices that a user can select from BUT how about the alraedy selected value that was already written to the DB? Here its is:
cboAreas.Items.FindByValue(result["ixProjectArea"].ToString()).Selected=true
The Items property actually obtains the collection of items that are contained in the dropdown. As you can see, this is a bit different than setting the selected value via the "SelectedItem.Value" property, which is the one most people would initially reach for. By the way, the "Items" property also has a "FindByText" method.
So for today I'm impressed with the ASP.NET dropdown control.
12:44:11 PM
|