Today we were talking about using new databound controls in asp.net 2.0, i have been trying to persuade my partner to use these controls but he has been refusing and saying that those controls are not flexible enough. Then he challenged me to populate a dropdownlist in a formview; with a different datasource from the formview's and set its selected value according to the value in the formview's datasource. There were two things to do, first i needed to populate the dropdownlist anyway, and then second set its selected value. I started off with creating one object datasource and bind it to my formview, then i set dropdownlist datasource property with another objectdatasource. Actually this caused an error telling that i cannot declare a datasource property for the dropdownlist in formview. Then i deleted datasource property and used datasourceID property for the dropdownlist.
This time it got populated without an error. And for the setting selected value issue, i used formviews itemdatabound event. It worked correctly.
My sample datatables are as follows :
teachers table : PKID | name | lastname | department(int)
departments table : PKID | departmentName
I created my dataaccesslayer using dataset designer and in the presentation layer i created two object datasource configured to get data from teachears and departments table respectively. The generated markup is as follows :
This is a helpful thread. I thought that posting how you can save the value in the drop down list is appropriate.
'Use ItemUpdating method to add the drop down's selected value
'before the data is saved
Protected Sub FormView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewUpdateEventArgs) Handles FormView1.ItemUpdating
Dim drp As DropDownList
drp = FormView1.FindControl("DropDownList1")