Viewed Times:
5371
Add Date:
05/07/2010
Sometime we want to add a Dropdownlist control in our gridview (or datalist...), and let the Dropdownlist value matches with the data from the database table. for example, if in the table, a column value only accept 1 or 0, and you want to show Yes/No in the dropdown list.
|
Note: In above html code, we added a hidden label, and Eval() the value from datasource, that can help the Dropdownlist to select correct value.
Also in the [ItemTemplate] section, we added a function call showYesOrNo(), and that function can be implemented from code behind:
That wasn't too hard, but we also need take care the dropdonwlist when user clicks the "Edit" Link button, otherwise, once the "Edit" Link button is clicked, the dropdownlist won't show the original yes/no value, and will jump to the first item in the list, in here, it will jump to the empty item, that will confuse the user.
By the way, since we leave an empty text item in the dropdownlist, if user didn't select yes/no value, will send an error message to the user:
Above is an simple example, but can be used in pretty complex situations, such as: there are a lot of dowpdownlist items, and the items' value and text are also vary...
|