[thelist] SOLVED: ASP.NET dropdownlist error

Joel D Canfield joel at streamliine.com
Thu Sep 27 13:16:48 CDT 2007


I was making two primary errors:

1. The field which the DDL binds to has to exist in the query for the
row of data being edited.
2. The value being bound to should be the *value* portion of the DDL,
not the visible text.

Both obvious with hindsight.

Here's what I have working:
	<asp:SqlDataSource 
		ID="srcDepts" runat="server" 
		ConnectionString="<%$ ConnectionStrings:cnxHelm %>"
		SelectCommand="SELECT id AS DeptID, DeptName FROM
Departments ORDER BY DeptName">
	</asp:SqlDataSource>
	<asp:TemplateField HeaderText="Dept" SortExpression="DeptName">
		<ItemTemplate>
			<%#Eval("DeptName")%>
		</ItemTemplate>
		<EditItemTemplate>
			<asp:DropDownList 
				ID="DeptDDL" runat="server" 
				DataSourceID="srcDepts"
				DataTextField="DeptName" 
				DataValueField="DeptID"
				SelectedValue='<%# Bind("DeptID") %>' >
			</asp:DropDownList>
		</EditItemTemplate>
	</asp:TemplateField>

Departments.id is aliased as DeptID in the query for the employee info
(what's being updated) and also in the query for the DDL, which is the
list of available values. DeptName is included in both as the visible
text.

Thanks again for the patience and help on this.

I'll be back . . . 

joel



More information about the thelist mailing list