954,595 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Populate dropdownlist in formview

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>

<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>

<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>
name:
<asp: TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>'>
</asp: TextBox>
lastname:
<asp: TextBox ID="lastnameTextBox" runat="server" Text='<%# Bind("lastname") %>'>
</asp: TextBox>
department:
<asp: TextBox ID="departmentTextBox" runat="server" Text='<%# Bind("department") %>'>
</asp:TextBox>
<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>
lastname:
<asp: TextBox ID="lastnameTextBox" runat="server" Text='<%# Bind("lastname") %>'>
</asp: TextBox>
department:
<asp: TextBox ID="departmentTextBox" runat="server" Text='<%# Bind("department") %>'>
</asp: TextBox>
<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>
name:
<asp: Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>'></asp: Label>
lastname:
<asp: Label ID="lastnameLabel" runat="server" Text='<%# Bind("lastname") %>'></asp: Label>
department:
<asp: DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource2"
DataTextField="departmentName" DataValueField="PKID">
</asp: DropDownList>
<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();
    }
serkan sendur
Postaholic
Banned
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
 

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
lance.stine
Newbie Poster
4 posts since Mar 2008
Reputation Points: 10
Solved Threads: 1
 

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

Frank

Daremo
Newbie Poster
1 post since Jan 2009
Reputation Points: 10
Solved Threads: 1
 

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

vyasdev2005
Newbie Poster
4 posts since Oct 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You