DropDownList in a DetailsView

C

C Silva

Hello,

I am trying to use a DropDownList in a DetailsView template in EditMode. The
dropdownlist works fine for values created by the datasource, but give me
errors when the ListItem was created in code.

Using the following code, you will see the error when you select the fourth
record.

Am I using a improper way to create the ListItem?
Thanks, Cloves

<%@ Page Language="VB"%>
<script runat=server>

Sub ddlCategory_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim ddlCategory As DropDownList = CType(sender, DropDownList)
Dim EmptyItem As New ListItem("Select one...", "0")
ddlCategory.Items.Insert(0, EmptyItem)
End Sub

</script>

<html>
<body>
<form id="Form1" runat=server>
<asp:DetailsView ID="DetailsView1" Runat="server"
DataSourceID="sdsProduct" AutoGenerateRows="False"
AllowPaging="True" DefaultMode="Edit">
<Fields>
<asp:BoundField ReadOnly="True" HeaderText="ProductID"
InsertVisible="False" DataField="ProductID"
SortExpression="ProductID"></asp:BoundField>
<asp:BoundField HeaderText="ProductName"
DataField="ProductName"></asp:BoundField>
<asp:BoundField HeaderText="CategoryID"
DataField="CategoryID" ></asp:BoundField>
<asp:TemplateField HeaderText="Category"><EditItemTemplate>
<asp:DropDownList ID="ddlCategory" Runat="server"
DataSourceID="sdsCategory" DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%#
Bind("CategoryID") %>' OnDataBound="ddlCategory_DataBound">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>

<asp:SqlDataSource ID="sdsCategory" Runat="server"
ConnectionString="Server=localhost;integrated
security=SSPI;Database=Northwind"
SelectCommand="SELECT CategoryID, CategoryName FROM Categories"
DataSourceMode="DataReader" >
</asp:SqlDataSource>

<asp:SqlDataSource ID="sdsProduct" Runat="server"
ConnectionString="Server=localhost;integrated
security=SSPI;Database=Northwind"
SelectCommand="SELECT top 3 ProductID, ProductName, CategoryID
FROM Products Union Select 99 as ProductID, 'My Product' as ProductName, '0'
as CategoryId" ProviderName="System.Data.SqlClient">
</asp:SqlDataSource>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top