What am I missing? I can't figure it out. Granted, I'm a complete newb, but I've been trying for weeks! While in "template mode", removed the default textbox in the DetailsView EditItemTemplate and InsertItemTemplate and replaced with a dropdownlist control. When I place SelectedValue= '<%# Bind("Color") %>' in the DropDownList tag I get an error at runtime:
"'DropDownList2' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value "
If I leave out "SelectedValue='<%# Bind("Color") %>" from the tag, the selection gets lost and doesn't get saved to the database. What's going on and how do I fix it? All I want to do is provide a form and save it to a database. Seems like it should be straightfoward...
Thanks for the much needed help,
Greg
Here's my code:
<%@ Page Language="
VB" AutoEventWireup="false" CodeFile="Default.aspx.
vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
DataSourceID="SqlDataSource1" Height="50px" Width="125px">
<Fields>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Color" SortExpression="Color">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" SelectedValue='<%# Bind("Color") %>'>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Red</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" SelectedValue='<%# Bind("Color") %>'>
<asp:ListItem>Blue</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Red</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Color]"
InsertCommand="INSERT INTO Color([Name], [Color]) VALUES (@Name, @Color)"
UpdateCommand="UPDATE Color
SET Name =@Name,
Color =@Color
WHERE [Name]=@Name">
<UpdateParameters><asp:Parameter Name="Name" Type="string" /><asp:Parameter Name="Color" Type="string" /></UpdateParameters>
<InsertParameters><asp:Parameter Name="Name" Type="string" /><asp:Parameter Name="Color" Type="string" /></InsertParameters>
</asp:SqlDataSource>
</div>
</form>
</body>
</html>
What about removing "SelecedValue" from the DropDownList tag and replacing it with an OnSelectedIndexChanged function. Can't figure out how to put together the code behind to make it work. Don't have a clue...
Protected Sub MyMethod(ByVal sender As Object, ByVal e As System.EventArgs)
Dim DropDownList1 As New DropDownList
DropDownList1 = CType(Me.DetailsView1.FindControl("DropDownList1") , DropDownList)
'Dim DropDownList1 As DropDownList
'DropDownList1 = CType(Me.DetailsView1.FindControl("DropDownList1") , DropDownList)
'Dim selectedValue As String = CType(DetailsView1.FindControl("DropDownList1"), DropDownList).SelectedValue
End Sub