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>
<aspetailsView 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>
<aspropDownList ID="ddlCategory" Runat="server"
DataSourceID="sdsCategory" DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%#
Bind("CategoryID") %>' OnDataBound="ddlCategory_DataBound">
</aspropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</aspetailsView>
<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>
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>
<aspetailsView 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>
<aspropDownList ID="ddlCategory" Runat="server"
DataSourceID="sdsCategory" DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%#
Bind("CategoryID") %>' OnDataBound="ddlCategory_DataBound">
</aspropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</aspetailsView>
<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>