2011年8月9日星期二

Bind data to DropDownList in edit template of GridView or DetailsView

This is straight forward. Set the “SelectedValue” attribute for DropDownList control.

Note. The “SelectedValue” is not in the intelligent context menu. It won’t automatic pop-up when you typing, but it does exist.

For example, we display a product in a DetailsView. In edit mode, we want to use a dropdown list to select the type of that product. Assume the type id is used in the product table, the edit template would be like:

<EditItemTemplate>
<asp:DropDownList  **** DataTextField="Name" DataValueField="typeid" SelectedValue='<%# Bind("typeid") %>'></asp:DropDownList>
</EditItemTemplate>

This will auto bind the type id to the list and update with the selected id. The update command and parameters are as usual.