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 :

<asp: ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="DataSet1TableAdapters.teachersTableAdapter" UpdateMethod="Update">
<DeleteParameters>
<asp: Parameter Name="Original_PKID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp: Parameter Name="name" Type="String" />
<asp: Parameter Name="lastname" Type="String" />
<asp: Parameter Name="department" Type="Int32" />
<asp: Parameter Name="PKID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp: Parameter Name="name" Type="String" />
<asp: Parameter Name="lastname" Type="String" />
<asp: Parameter Name="department" Type="Int32" />
</InsertParameters>
</asp: ObjectDataSource>
<br />
<asp: ObjectDataSource ID="ObjectDataSource2" runat="server" DeleteMethod="Delete"
InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="DataSet1TableAdapters.departmentsTableAdapter" UpdateMethod="Update">
<DeleteParameters>
<asp: Parameter Name="Original_PKID" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp: Parameter Name="departmentName" Type="String" />
<asp: Parameter Name="PKID" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp: Parameter Name="departmentName" Type="String" />
</InsertParameters>
</asp: ObjectDataSource>
&nbsp;</div>
<br />
<asp: FormView ID="FormView1" runat="server" AllowPaging="True" DataKeyNames="PKID"
DataSourceID="ObjectDataSource1" OnDataBound="FormView1_DataBound">
<EditItemTemplate>
PKID:
<asp: Label ID="PKIDLabel1" runat="server" Text='<%# Eval("PKID") %>'></asp:Label><br />
name:
<asp: TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
</asp: TextBox><br />
lastname:
<asp: TextBox ID="lastnameTextBox" runat="server" Text='<%# Bind("lastname") %>'>
</asp: TextBox><br />
department:
<asp: TextBox ID="departmentTextBox" runat="server" Text='<%# Bind("department") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">
</asp: LinkButton>
<asp: LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp: LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
name:
<asp: TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
</asp: TextBox><br />
lastname:
<asp: TextBox ID="lastnameTextBox" runat="server" Text='<%# Bind("lastname") %>'>
</asp: TextBox><br />
department:
<asp: TextBox ID="departmentTextBox" runat="server" Text='<%# Bind("department") %>'>
</asp: TextBox><br />
<asp: LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp: LinkButton>
<asp: LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp: LinkButton>
</InsertItemTemplate>
<ItemTemplate>
PKID:
<asp: Label ID="PKIDLabel" runat="server" Text='<%# Eval("PKID") %>'></asp:Label><br />
name:
<asp: Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp: Label><br />
lastname:
<asp: Label ID="lastnameLabel" runat="server" Text='<%# Bind("lastname") %>'></asp: Label><br />
department:
<asp: DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource2"
DataTextField="departmentName" DataValueField="PKID">
</asp: DropDownList><br />
<asp: LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp: LinkButton>
<asp: LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete"></asp: LinkButton>
<asp: LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="New"></asp: LinkButton>
</ItemTemplate>
</asp: FormView>

In the code behind i used itemdatabound event of the formview so that i set the selected value for the dropdownlist as follows : 


(Toggle Plain Text) 
protected void FormView1_DataBound(object sender, EventArgs e)
    {
        DataRowView drv = (DataRowView)FormView1.DataItem;
        ((DropDownList)FormView1.FindControl("DropDownList1")).SelectedValue = drv["department"].ToString();
    }protected void FormView1_DataBound(object sender, EventArgs e)
    {
        DataRowView drv = (DataRowView)FormView1.DataItem;
        ((DropDownList)FormView1.FindControl("DropDownList1")).SelectedValue = drv["department"].ToString();
    }

Recommended Answers

All 3 Replies

This is a helpful thread. I thought that posting how you can save the value in the drop down list is appropriate.

'VB
'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")
        e.Keys.Add("departmentName", drp.SelectedValue)

End Sub

Thanks, great info on something that had me stumped before.

Frank

Thank You very much....It helps a lot........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.