what's wrong with this code?

B

Ben

Hi,

i have a detailsview in edit mode which contains only (to simplify) a
dropdownlist (in a template). This dropdownlist must get values 1 to 5. The
purpose is to get a record from the database and to edit the field "myfield"
with the selected value of the dropdownlist.The present value of field
"myfield" is 1 (smallinteger).
I did this and it works perfectly.

<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Edit"
DataSourceID="SqlDataSource1" DataKeyNames="id" AutoGenerateRows="False">
<Fields>
<asp:TemplateField HeaderText="test">
<EditItemTemplate>
<asp:DropDownList ID="dd1" runat="server" SelectedValue='<%#
Bind("myfield") %>'>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
....
</Fields>
</asp:DetailsView>

My problem is: i want to feed the dropdownlist programmatically. So i
removed all the <asp:ListItem>1</asp:ListItem> lines and added this code
(vb.net):

Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DetailsView1.DataBound
If DetailsView1.CurrentMode = DetailsViewMode.Edit Then
Dim dvr As DetailsViewRow
Dim dd1 As DropDownList
Dim i As Integer
Dim z As ListItem
For Each dvr In DetailsView1.Rows
dd1 = CType(dvr.FindControl("dd1"), DropDownList)
Next
For i = 1 To 10
z = New ListItem(i, i)
dd1.Items.Add(z)
Next
End If
End Sub

But now i get the error: "selectedValue is wrong because it doesn't appear
in the list with items"
What's wrong with this code and how to fix it?
Thanks for help.
Ben
(in insert mode, the same code (except changing in DetailsViewMode.Insert)
works perfectly!).
 
R

Raghupathi K

Hi,

i have a detailsview in edit mode which contains only (to simplify) a
dropdownlist (in a template). This dropdownlist must get values 1 to 5. The
purpose is to get a record from the database and to edit the field "myfield"
with the selected value of the dropdownlist.The present value of field
"myfield" is 1 (smallinteger).
I did this and it works perfectly.

<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Edit"
DataSourceID="SqlDataSource1" DataKeyNames="id" AutoGenerateRows="False">
<Fields>
<asp:TemplateField HeaderText="test">
 <EditItemTemplate>
 <asp:DropDownList ID="dd1" runat="server" SelectedValue='<%#
Bind("myfield") %>'>
 <asp:ListItem>1</asp:ListItem>
 <asp:ListItem>2</asp:ListItem>
 <asp:ListItem>3</asp:ListItem>
 <asp:ListItem>4</asp:ListItem>
 <asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
...
 </Fields>
 </asp:DetailsView>

My problem is: i want to feed the dropdownlist programmatically. So i
removed all the <asp:ListItem>1</asp:ListItem> lines and added this code
(vb.net):

Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DetailsView1.DataBound
If DetailsView1.CurrentMode = DetailsViewMode.Edit Then
Dim dvr As DetailsViewRow
Dim dd1 As DropDownList
Dim i As Integer
Dim z As ListItem
            For Each dvr In DetailsView1.Rows
                dd1 = CType(dvr.FindControl("dd1"), DropDownList)
            Next
            For i = 1 To 10
                z = New ListItem(i, i)
                dd1.Items.Add(z)
            Next
End If
End Sub

But now i get the error: "selectedValue is wrong because it doesn't appear
in the list with items"
What's wrong with this code and how to fix it?
Thanks for help.
Ben
(in insert mode, the same code (except changing in DetailsViewMode.Insert)
works perfectly!).

May be the second For loop should be inside the first one............
 
L

Luc

Thanks for replying, but this is the same as when the two loops are not
nested.
The solution is to use itemcreated.

"Raghupathi K" <[email protected]> schreef in bericht
Hi,

i have a detailsview in edit mode which contains only (to simplify) a
dropdownlist (in a template). This dropdownlist must get values 1 to 5.
The
purpose is to get a record from the database and to edit the field
"myfield"
with the selected value of the dropdownlist.The present value of field
"myfield" is 1 (smallinteger).
I did this and it works perfectly.

<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Edit"
DataSourceID="SqlDataSource1" DataKeyNames="id" AutoGenerateRows="False">
<Fields>
<asp:TemplateField HeaderText="test">
<EditItemTemplate>
<asp:DropDownList ID="dd1" runat="server" SelectedValue='<%#
Bind("myfield") %>'>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
...
</Fields>
</asp:DetailsView>

My problem is: i want to feed the dropdownlist programmatically. So i
removed all the <asp:ListItem>1</asp:ListItem> lines and added this code
(vb.net):

Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As
System.EventArgs) Handles DetailsView1.DataBound
If DetailsView1.CurrentMode = DetailsViewMode.Edit Then
Dim dvr As DetailsViewRow
Dim dd1 As DropDownList
Dim i As Integer
Dim z As ListItem
For Each dvr In DetailsView1.Rows
dd1 = CType(dvr.FindControl("dd1"), DropDownList)
Next
For i = 1 To 10
z = New ListItem(i, i)
dd1.Items.Add(z)
Next
End If
End Sub

But now i get the error: "selectedValue is wrong because it doesn't appear
in the list with items"
What's wrong with this code and how to fix it?
Thanks for help.
Ben
(in insert mode, the same code (except changing in DetailsViewMode.Insert)
works perfectly!).

May be the second For loop should be inside the first one............
 

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

Top