Personal notes on software development.
For Java technologies check my dedicated site

Pages

Notes:

Adding a "-- Choose a Category --" List Item [1]

When first displaying the DropDownList we may want to have a DropDownList item selected that says something like, "-- Choose a Category --".
To add a new list item to the DropDownList, go to its smart tag and select "Edit Items...". Add a new list item with the Text "-- Choose a Category --" and the Value -1.

Alternatively, you can add the list item by adding the following markup to the DropDownList:
<asp:DropDownList ID="DropDownList1" runat="server" 
    DataSourceID="ObjectDataSource1" DataTextField="CategoryName" 
    DataValueField="CategoryID" AutoPostBack="true"
    AppendDataBoundItems="True">
    <asp:ListItem Selected="True" Value="-1">-- Choose a category --</asp:ListItem>
</asp:DropDownList>

Additionally, we need to set the DropDownList control's AppendDataBoundItems to True because when the categories are bound to the DropDownList from the ObjectDataSource they'll overwrite any manually-added list items if AppendDataBoundItems isn't True.

Handling NULL Values:

Scenario [2]: We have a DropDownList bound to some DB data. The DropDownList value is assigned to "CategoryID" and its text to "CategoryName". The DB CategoryID can be assigned to a NULL value, yet the DropDownLists in the EditItemTemplates don't include a list item to represent a NULL value. This has two consequences:
  • User cannot use the DropDownList to change a product's category or supplier from a non-NULL value to a NULL one;
  • If a product has a NULL CategoryID, in the DB, this will result in an exception on the DropDownList. This is because the NULL value returned by CategoryID in the Bind() statement does not map to a value in the DropDownList (the DropDownList throws an exception when its SelectedValuenot in its collection of list items property is set to a value ).
In order to support NULL CategoryID values, we need to add a ListItem to the DropDownList to represent the NULL value. So, we set the AppendDataBoundItems property to true and manually add the additional ListItem. The databinding logic in ASP.NET will automatically convert a blank string to a NULL value and vice-a-versa. Therefore, we want the ListItem's Value to be an empty string:
<asp:DropDownList ID="Categories" runat="server"
    AppendDataBoundItems="true"
    DataSourceID="CategoriesDataSource" 
    DataTextField="CategoryName" 
    DataValueField="CategoryID" 
    SelectedValue='<%# Bind("CategoryID") %>'>
    <asp:ListItem Text="None" Value="" />
</asp:DropDownList>

Populating a DropDownList in code behind example [3]:

DropDownList1.Items.Clear();
ListItem item;
for (int i = 0; i < ProductsGridView.PageCount; i++)
{
    item = new ListItem("Page " + (i+1), i.ToString());
    DropDownList1.Items.Add(item);
}
DropDownList1.SelectedIndex = ProductsGridView.PageIndex; 

Related articles:
[1] - Master/Detail Filtering With a DropDownList
[2] - Customizing the Data Modification Interface (see Handling NULL Values section)
[3] - Paging and Sorting Report Data (Step4: Customizing the Paging Experience)

No comments:

Post a Comment