473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

DropDownList1' has a SelectedValue which is invalid

Ben
Hi,

i dynamically feed a dropdownlist which value from 1 to 20. That
dropdownlist is bound to field 'wa' (type nvarchar(4)) in 'mytable'. There
are 4 records and the values of field 'wa' are: 2 3 1 4. All those values
are contained in the list of items of the DD.
In normal mode, the field 'wa' appears for each record with the right value.
When i click on the Edit button, i get this error:

"DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value

The EnableViewState property of the page ="true"

<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("wa") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit
Then
If e.Row.RowType = DataControlRowType.DataRow Then

Dim i As Integer
Dim z As ListItem
Dim dd As DropDownList

dd = e.Row.FindControl("DropDownList1")
For i = 1 To 20
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
Next
End If
End If

end sub
Jun 27 '08 #1
18 4951
During the ItemDataBound Event you need to add the stored text & value to
the DD because it is not in the list you are trying to create, which is
requied by the list control.

I can give you an example in VB if it would help

John
"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Hi,

i dynamically feed a dropdownlist which value from 1 to 20. That
dropdownlist is bound to field 'wa' (type nvarchar(4)) in 'mytable'. There
are 4 records and the values of field 'wa' are: 2 3 1 4. All those values
are contained in the list of items of the DD.
In normal mode, the field 'wa' appears for each record with the right
value.
When i click on the Edit button, i get this error:

"DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value

The EnableViewState property of the page ="true"

<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("wa") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then

Dim i As Integer
Dim z As ListItem
Dim dd As DropDownList

dd = e.Row.FindControl("DropDownList1")
For i = 1 To 20
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
Next
End If
End If

end sub

Jun 27 '08 #2
Ben
Thanks for your reply.
I can't find the ItemDataBound event, only RowDataBound, DataBound and
RowCreated.

If you have an example, it would be fine ...

"John" <no*@non.comschreef in bericht
news:O7**************@TK2MSFTNGP05.phx.gbl...
During the ItemDataBound Event you need to add the stored text & value to
the DD because it is not in the list you are trying to create, which is
requied by the list control.

I can give you an example in VB if it would help

John
"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Hi,

i dynamically feed a dropdownlist which value from 1 to 20. That
dropdownlist is bound to field 'wa' (type nvarchar(4)) in 'mytable'.
There are 4 records and the values of field 'wa' are: 2 3 1 4. All those
values are contained in the list of items of the DD.
In normal mode, the field 'wa' appears for each record with the right
value.
When i click on the Edit button, i get this error:

"DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value

The EnableViewState property of the page ="true"

<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("wa") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then

Dim i As Integer
Dim z As ListItem
Dim dd As DropDownList

dd = e.Row.FindControl("DropDownList1")
For i = 1 To 20
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
Next
End If
End If

end sub


Jun 27 '08 #3
My error, my mind was on Listview not GridView RowDataBound is correct, but
the idea is the same, what I do is add a hidden field to the edit template
that is bound to the source. Then I use findcontrol to get it's value ex.
HF.Value, then add it first to using dd.SelectedText.Value, I get odd
results with dd.SelectedValue, somtimes it won't take it.

Then in your "for" filter out the HF value if you want to so it is not in
the list twice.

Give it a try
John

"Ben" <be*@nol.vbwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl...
Thanks for your reply.
I can't find the ItemDataBound event, only RowDataBound, DataBound and
RowCreated.

If you have an example, it would be fine ...

"John" <no*@non.comschreef in bericht
news:O7**************@TK2MSFTNGP05.phx.gbl...
>During the ItemDataBound Event you need to add the stored text & value to
the DD because it is not in the list you are trying to create, which is
requied by the list control.

I can give you an example in VB if it would help

John
"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>Hi,

i dynamically feed a dropdownlist which value from 1 to 20. That
dropdownlist is bound to field 'wa' (type nvarchar(4)) in 'mytable'.
There are 4 records and the values of field 'wa' are: 2 3 1 4. All those
values are contained in the list of items of the DD.
In normal mode, the field 'wa' appears for each record with the right
value.
When i click on the Edit button, i get this error:

"DropDownList1' has a SelectedValue which is invalid because it does not
exist in the list of items.
Parameter name: value

The EnableViewState property of the page ="true"

<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("wa") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs ) Handles
GridView1.RowDataBound

If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then

Dim i As Integer
Dim z As ListItem
Dim dd As DropDownList

dd = e.Row.FindControl("DropDownList1")
For i = 1 To 20
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
Next
End If
End If

end sub



Jun 27 '08 #4
You may have to unbind the DD also as you are adding thru the HF and add the
text with the value.
John

"John" <no*@non.comwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl...
My error, my mind was on Listview not GridView RowDataBound is correct,
but the idea is the same, what I do is add a hidden field to the edit
template that is bound to the source. Then I use findcontrol to get it's
value ex. HF.Value, then add it first to using dd.SelectedText.Value, I
get odd results with dd.SelectedValue, somtimes it won't take it.

Then in your "for" filter out the HF value if you want to so it is not in
the list twice.

Give it a try
John

"Ben" <be*@nol.vbwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl...
>Thanks for your reply.
I can't find the ItemDataBound event, only RowDataBound, DataBound and
RowCreated.

If you have an example, it would be fine ...

"John" <no*@non.comschreef in bericht
news:O7**************@TK2MSFTNGP05.phx.gbl...
>>During the ItemDataBound Event you need to add the stored text & value
to the DD because it is not in the list you are trying to create, which
is requied by the list control.

I can give you an example in VB if it would help

John
"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl.. .
Hi,

i dynamically feed a dropdownlist which value from 1 to 20. That
dropdownlist is bound to field 'wa' (type nvarchar(4)) in 'mytable'.
There are 4 records and the values of field 'wa' are: 2 3 1 4. All
those values are contained in the list of items of the DD.
In normal mode, the field 'wa' appears for each record with the right
value.
When i click on the Edit button, i get this error:

"DropDownList1' has a SelectedValue which is invalid because it does
not exist in the list of items.
Parameter name: value

The EnableViewState property of the page ="true"

<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("wa") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArg s) Handles
GridView1.RowDataBound

If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then

Dim i As Integer
Dim z As ListItem
Dim dd As DropDownList

dd = e.Row.FindControl("DropDownList1")
For i = 1 To 20
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
Next
End If
End If

end sub



Jun 27 '08 #5
Ben
Thanks again.
Could you give me a little example in vb? I tried to add a hiddenfield in
the Edit template in the aspx file, but it's a unrecognized element'
"John" <no*@non.comschreef in bericht
news:eY**************@TK2MSFTNGP05.phx.gbl...
You may have to unbind the DD also as you are adding thru the HF and add
the text with the value.
John

"John" <no*@non.comwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl...
>My error, my mind was on Listview not GridView RowDataBound is correct,
but the idea is the same, what I do is add a hidden field to the edit
template that is bound to the source. Then I use findcontrol to get it's
value ex. HF.Value, then add it first to using dd.SelectedText.Value, I
get odd results with dd.SelectedValue, somtimes it won't take it.

Then in your "for" filter out the HF value if you want to so it is not in
the list twice.

Give it a try
John

"Ben" <be*@nol.vbwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl...
>>Thanks for your reply.
I can't find the ItemDataBound event, only RowDataBound, DataBound and
RowCreated.

If you have an example, it would be fine ...

"John" <no*@non.comschreef in bericht
news:O7**************@TK2MSFTNGP05.phx.gbl...
During the ItemDataBound Event you need to add the stored text & value
to the DD because it is not in the list you are trying to create, which
is requied by the list control.

I can give you an example in VB if it would help

John
"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl. ..
Hi,
>
i dynamically feed a dropdownlist which value from 1 to 20. That
dropdownlist is bound to field 'wa' (type nvarchar(4)) in 'mytable'.
There are 4 records and the values of field 'wa' are: 2 3 1 4. All
those values are contained in the list of items of the DD.
In normal mode, the field 'wa' appears for each record with the right
value.
When i click on the Edit button, i get this error:
>
"DropDownList1' has a SelectedValue which is invalid because it does
not exist in the list of items.
Parameter name: value
>
The EnableViewState property of the page ="true"
>
<asp:TemplateField>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("wa") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
>
>
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound
>
If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then
>
Dim i As Integer
Dim z As ListItem
Dim dd As DropDownList
>
dd = e.Row.FindControl("DropDownList1")
For i = 1 To 20
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
Next
End If
End If
>
end sub
>




Jun 27 '08 #6
I ended up with more of a version like your add to get it to work, but don't
forget to unbind theDD
<EditItemTemplate>

<asp:HiddenField ID="HiddenField1" runat="server"

Value='<%# Eval("wa") %>' />

</EditItemTemplate>

Dim HF As HiddenField = e.Row.FindControl("HiddenField1")

If Not HF Is Nothing Then

MsgBox("found it")

End If

dd = e.Row.FindControl("DropDownList1")

If Not dd Is Nothing Then

Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)

dd.Items.Add(z)

End If

"Ben" <be*@nol.vbwrote in message
news:OZ**************@TK2MSFTNGP03.phx.gbl...
Thanks again.
Could you give me a little example in vb? I tried to add a hiddenfield in
the Edit template in the aspx file, but it's a unrecognized element'
"John" <no*@non.comschreef in bericht
news:eY**************@TK2MSFTNGP05.phx.gbl...
>You may have to unbind the DD also as you are adding thru the HF and add
the text with the value.
John

"John" <no*@non.comwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl...
>>My error, my mind was on Listview not GridView RowDataBound is correct,
but the idea is the same, what I do is add a hidden field to the edit
template that is bound to the source. Then I use findcontrol to get it's
value ex. HF.Value, then add it first to using dd.SelectedText.Value, I
get odd results with dd.SelectedValue, somtimes it won't take it.

Then in your "for" filter out the HF value if you want to so it is not
in the list twice.

Give it a try
John

"Ben" <be*@nol.vbwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl...
Thanks for your reply.
I can't find the ItemDataBound event, only RowDataBound, DataBound and
RowCreated.

If you have an example, it would be fine ...

"John" <no*@non.comschreef in bericht
news:O7**************@TK2MSFTNGP05.phx.gbl...
During the ItemDataBound Event you need to add the stored text & value
to the DD because it is not in the list you are trying to create,
which is requied by the list control.
>
I can give you an example in VB if it would help
>
John
"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl.. .
>Hi,
>>
>i dynamically feed a dropdownlist which value from 1 to 20. That
>dropdownlist is bound to field 'wa' (type nvarchar(4)) in 'mytable'.
>There are 4 records and the values of field 'wa' are: 2 3 1 4. All
>those values are contained in the list of items of the DD.
>In normal mode, the field 'wa' appears for each record with the right
>value.
>When i click on the Edit button, i get this error:
>>
>"DropDownList1' has a SelectedValue which is invalid because it does
>not exist in the list of items.
>Parameter name: value
>>
>The EnableViewState property of the page ="true"
>>
><asp:TemplateField>
><EditItemTemplate>
><asp:DropDownList ID="DropDownList1" runat="server"
>SelectedValue='<%# Bind("wa") %>'>
></asp:DropDownList>
></EditItemTemplate>
><ItemTemplate>
><asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
>%>'></asp:Label>
></ItemTemplate>
></asp:TemplateField>
>>
>>
>Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e
>As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
>GridView1.RowDataBound
>>
>If (e.Row.RowState And DataControlRowState.Edit) =
>DataControlRowState.Edit Then
>If e.Row.RowType = DataControlRowType.DataRow Then
>>
>Dim i As Integer
>Dim z As ListItem
>Dim dd As DropDownList
>>
>dd = e.Row.FindControl("DropDownList1")
>For i = 1 To 20
>z = New ListItem(i.ToString, i.ToString)
>dd.Items.Add(z)
>Next
>End If
>End If
>>
>end sub
>>
>
>




Jun 27 '08 #7
Ben
The problem now is that the DD must be fed dynamically in code-behind (i
want values from 1 to 20 in the DD). So where do i have to put the loop:
for i=1 to 20
....
next

because with z = New ListItem(HF.Value, HF.Value)
i will only put the existing values coming from tha table ....
You understand?
"John" <no*@non.comschreef in bericht
news:uh**************@TK2MSFTNGP02.phx.gbl...
>I ended up with more of a version like your add to get it to work, but
don't forget to unbind theDD
<EditItemTemplate>

<asp:HiddenField ID="HiddenField1" runat="server"

Value='<%# Eval("wa") %>' />

</EditItemTemplate>

Dim HF As HiddenField = e.Row.FindControl("HiddenField1")

If Not HF Is Nothing Then

MsgBox("found it")

End If

dd = e.Row.FindControl("DropDownList1")

If Not dd Is Nothing Then

Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)

dd.Items.Add(z)

End If

"Ben" <be*@nol.vbwrote in message
news:OZ**************@TK2MSFTNGP03.phx.gbl...
>Thanks again.
Could you give me a little example in vb? I tried to add a hiddenfield in
the Edit template in the aspx file, but it's a unrecognized element'
"John" <no*@non.comschreef in bericht
news:eY**************@TK2MSFTNGP05.phx.gbl...
>>You may have to unbind the DD also as you are adding thru the HF and add
the text with the value.
John

"John" <no*@non.comwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl...
My error, my mind was on Listview not GridView RowDataBound is correct,
but the idea is the same, what I do is add a hidden field to the edit
template that is bound to the source. Then I use findcontrol to get
it's value ex. HF.Value, then add it first to using
dd.SelectedText.Value, I get odd results with dd.SelectedValue,
somtimes it won't take it.

Then in your "for" filter out the HF value if you want to so it is not
in the list twice.

Give it a try
John

"Ben" <be*@nol.vbwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl...
Thanks for your reply.
I can't find the ItemDataBound event, only RowDataBound, DataBound and
RowCreated.
>
If you have an example, it would be fine ...
>
"John" <no*@non.comschreef in bericht
news:O7**************@TK2MSFTNGP05.phx.gbl.. .
>During the ItemDataBound Event you need to add the stored text &
>value to the DD because it is not in the list you are trying to
>create, which is requied by the list control.
>>
>I can give you an example in VB if it would help
>>
>John
>"Ben" <be*@nol.vbwrote in message
>news:%2****************@TK2MSFTNGP03.phx.gbl. ..
>>Hi,
>>>
>>i dynamically feed a dropdownlist which value from 1 to 20. That
>>dropdownlist is bound to field 'wa' (type nvarchar(4)) in 'mytable'.
>>There are 4 records and the values of field 'wa' are: 2 3 1 4. All
>>those values are contained in the list of items of the DD.
>>In normal mode, the field 'wa' appears for each record with the
>>right value.
>>When i click on the Edit button, i get this error:
>>>
>>"DropDownList1' has a SelectedValue which is invalid because it does
>>not exist in the list of items.
>>Parameter name: value
>>>
>>The EnableViewState property of the page ="true"
>>>
>><asp:TemplateField>
>><EditItemTemplate>
>><asp:DropDownList ID="DropDownList1" runat="server"
>>SelectedValue='<%# Bind("wa") %>'>
>></asp:DropDownList>
>></EditItemTemplate>
>><ItemTemplate>
>><asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
>>%>'></asp:Label>
>></ItemTemplate>
>></asp:TemplateField>
>>>
>>>
>>Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e
>>As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
>>GridView1.RowDataBound
>>>
>>If (e.Row.RowState And DataControlRowState.Edit) =
>>DataControlRowState.Edit Then
>>If e.Row.RowType = DataControlRowType.DataRow Then
>>>
>>Dim i As Integer
>>Dim z As ListItem
>>Dim dd As DropDownList
>>>
>>dd = e.Row.FindControl("DropDownList1")
>>For i = 1 To 20
>>z = New ListItem(i.ToString, i.ToString)
>>dd.Items.Add(z)
>>Next
>>End If
>>End If
>>>
>>end sub
>>>
>>
>>
>
>




Jun 27 '08 #8
try this

If Not dd Is Nothing Then

Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)

dd.Items.Add(z)

dd.SelectedValue = HF.Value

For i = 1 To 20

If i <CInt(HF.Value) Then

z = New ListItem(i.ToString, i.ToString)

dd.Items.Add(z)

End If

Next

End If

John

"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
The problem now is that the DD must be fed dynamically in code-behind (i
want values from 1 to 20 in the DD). So where do i have to put the loop:
for i=1 to 20
...
next

because with z = New ListItem(HF.Value, HF.Value)
i will only put the existing values coming from tha table ....
You understand?
"John" <no*@non.comschreef in bericht
news:uh**************@TK2MSFTNGP02.phx.gbl...
>>I ended up with more of a version like your add to get it to work, but
don't forget to unbind theDD
<EditItemTemplate>

<asp:HiddenField ID="HiddenField1" runat="server"

Value='<%# Eval("wa") %>' />

</EditItemTemplate>

Dim HF As HiddenField = e.Row.FindControl("HiddenField1")

If Not HF Is Nothing Then

MsgBox("found it")

End If

dd = e.Row.FindControl("DropDownList1")

If Not dd Is Nothing Then

Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)

dd.Items.Add(z)

End If

"Ben" <be*@nol.vbwrote in message
news:OZ**************@TK2MSFTNGP03.phx.gbl...
>>Thanks again.
Could you give me a little example in vb? I tried to add a hiddenfield
in the Edit template in the aspx file, but it's a unrecognized element'
"John" <no*@non.comschreef in bericht
news:eY**************@TK2MSFTNGP05.phx.gbl...
You may have to unbind the DD also as you are adding thru the HF and
add the text with the value.
John

"John" <no*@non.comwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl...
My error, my mind was on Listview not GridView RowDataBound is
correct, but the idea is the same, what I do is add a hidden field to
the edit template that is bound to the source. Then I use findcontrol
to get it's value ex. HF.Value, then add it first to using
dd.SelectedText.Value, I get odd results with dd.SelectedValue,
somtimes it won't take it.
>
Then in your "for" filter out the HF value if you want to so it is not
in the list twice.
>
Give it a try
John
>
"Ben" <be*@nol.vbwrote in message
news:ua**************@TK2MSFTNGP05.phx.gbl.. .
>Thanks for your reply.
>I can't find the ItemDataBound event, only RowDataBound, DataBound
>and RowCreated.
>>
>If you have an example, it would be fine ...
>>
>"John" <no*@non.comschreef in bericht
>news:O7**************@TK2MSFTNGP05.phx.gbl. ..
>>During the ItemDataBound Event you need to add the stored text &
>>value to the DD because it is not in the list you are trying to
>>create, which is requied by the list control.
>>>
>>I can give you an example in VB if it would help
>>>
>>John
>>"Ben" <be*@nol.vbwrote in message
>>news:%2****************@TK2MSFTNGP03.phx.gbl ...
>>>Hi,
>>>>
>>>i dynamically feed a dropdownlist which value from 1 to 20. That
>>>dropdownlist is bound to field 'wa' (type nvarchar(4)) in
>>>'mytable'. There are 4 records and the values of field 'wa' are: 2
>>>3 1 4. All those values are contained in the list of items of the
>>>DD.
>>>In normal mode, the field 'wa' appears for each record with the
>>>right value.
>>>When i click on the Edit button, i get this error:
>>>>
>>>"DropDownList1' has a SelectedValue which is invalid because it
>>>does not exist in the list of items.
>>>Parameter name: value
>>>>
>>>The EnableViewState property of the page ="true"
>>>>
>>><asp:TemplateField>
>>><EditItemTemplate>
>>><asp:DropDownList ID="DropDownList1" runat="server"
>>>SelectedValue='<%# Bind("wa") %>'>
>>></asp:DropDownList>
>>></EditItemTemplate>
>>><ItemTemplate>
>>><asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
>>>%>'></asp:Label>
>>></ItemTemplate>
>>></asp:TemplateField>
>>>>
>>>>
>>>Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal
>>>e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
>>>GridView1.RowDataBound
>>>>
>>>If (e.Row.RowState And DataControlRowState.Edit) =
>>>DataControlRowState.Edit Then
>>>If e.Row.RowType = DataControlRowType.DataRow Then
>>>>
>>>Dim i As Integer
>>>Dim z As ListItem
>>>Dim dd As DropDownList
>>>>
>>>dd = e.Row.FindControl("DropDownList1")
>>>For i = 1 To 20
>>>z = New ListItem(i.ToString, i.ToString)
>>>dd.Items.Add(z)
>>>Next
>>>End If
>>>End If
>>>>
>>>end sub
>>>>
>>>
>>>
>>
>>
>
>




Jun 27 '08 #9
Ben
Thanks

"John" <no*@non.comschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
try this

If Not dd Is Nothing Then

Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)

dd.Items.Add(z)

dd.SelectedValue = HF.Value

For i = 1 To 20

If i <CInt(HF.Value) Then

z = New ListItem(i.ToString, i.ToString)

dd.Items.Add(z)

End If

Next

End If

John

"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>The problem now is that the DD must be fed dynamically in code-behind (i
want values from 1 to 20 in the DD). So where do i have to put the loop:
for i=1 to 20
...
next

because with z = New ListItem(HF.Value, HF.Value)
i will only put the existing values coming from tha table ....
You understand?
"John" <no*@non.comschreef in bericht
news:uh**************@TK2MSFTNGP02.phx.gbl...
>>>I ended up with more of a version like your add to get it to work, but
don't forget to unbind theDD
<EditItemTemplate>

<asp:HiddenField ID="HiddenField1" runat="server"

Value='<%# Eval("wa") %>' />

</EditItemTemplate>

Dim HF As HiddenField = e.Row.FindControl("HiddenField1")

If Not HF Is Nothing Then

MsgBox("found it")

End If

dd = e.Row.FindControl("DropDownList1")

If Not dd Is Nothing Then

Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)

dd.Items.Add(z)

End If

"Ben" <be*@nol.vbwrote in message
news:OZ**************@TK2MSFTNGP03.phx.gbl...

Thanks again.
Could you give me a little example in vb? I tried to add a hiddenfield
in the Edit template in the aspx file, but it's a unrecognized element'
"John" <no*@non.comschreef in bericht
news:eY**************@TK2MSFTNGP05.phx.gbl...
You may have to unbind the DD also as you are adding thru the HF and
add the text with the value.
John
>
"John" <no*@non.comwrote in message
news:eJ**************@TK2MSFTNGP05.phx.gbl.. .
>My error, my mind was on Listview not GridView RowDataBound is
>correct, but the idea is the same, what I do is add a hidden field to
>the edit template that is bound to the source. Then I use findcontrol
>to get it's value ex. HF.Value, then add it first to using
>dd.SelectedText.Value, I get odd results with dd.SelectedValue,
>somtimes it won't take it.
>>
>Then in your "for" filter out the HF value if you want to so it is
>not in the list twice.
>>
>Give it a try
>John
>>
>"Ben" <be*@nol.vbwrote in message
>news:ua**************@TK2MSFTNGP05.phx.gbl. ..
>>Thanks for your reply.
>>I can't find the ItemDataBound event, only RowDataBound, DataBound
>>and RowCreated.
>>>
>>If you have an example, it would be fine ...
>>>
>>"John" <no*@non.comschreef in bericht
>>news:O7**************@TK2MSFTNGP05.phx.gbl.. .
>>>During the ItemDataBound Event you need to add the stored text &
>>>value to the DD because it is not in the list you are trying to
>>>create, which is requied by the list control.
>>>>
>>>I can give you an example in VB if it would help
>>>>
>>>John
>>>"Ben" <be*@nol.vbwrote in message
>>>news:%2****************@TK2MSFTNGP03.phx.gb l...
>>>>Hi,
>>>>>
>>>>i dynamically feed a dropdownlist which value from 1 to 20. That
>>>>dropdownlist is bound to field 'wa' (type nvarchar(4)) in
>>>>'mytable'. There are 4 records and the values of field 'wa' are: 2
>>>>3 1 4. All those values are contained in the list of items of the
>>>>DD.
>>>>In normal mode, the field 'wa' appears for each record with the
>>>>right value.
>>>>When i click on the Edit button, i get this error:
>>>>>
>>>>"DropDownList1' has a SelectedValue which is invalid because it
>>>>does not exist in the list of items.
>>>>Parameter name: value
>>>>>
>>>>The EnableViewState property of the page ="true"
>>>>>
>>>><asp:TemplateField>
>>>><EditItemTemplate>
>>>><asp:DropDownList ID="DropDownList1" runat="server"
>>>>SelectedValue='<%# Bind("wa") %>'>
>>>></asp:DropDownList>
>>>></EditItemTemplate>
>>>><ItemTemplate>
>>>><asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
>>>>%>'></asp:Label>
>>>></ItemTemplate>
>>>></asp:TemplateField>
>>>>>
>>>>>
>>>>Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal
>>>>e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles
>>>>GridView1.RowDataBound
>>>>>
>>>>If (e.Row.RowState And DataControlRowState.Edit) =
>>>>DataControlRowState.Edit Then
>>>>If e.Row.RowType = DataControlRowType.DataRow Then
>>>>>
>>>>Dim i As Integer
>>>>Dim z As ListItem
>>>>Dim dd As DropDownList
>>>>>
>>>>dd = e.Row.FindControl("DropDownList1")
>>>>For i = 1 To 20
>>>>z = New ListItem(i.ToString, i.ToString)
>>>>dd.Items.Add(z)
>>>>Next
>>>>End If
>>>>End If
>>>>>
>>>>end sub
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>




Jun 27 '08 #10
Ben
Hi John,

i tried your code: in normal mode, the values are rendered normally;
with Eval, when i click on Update button, the value disappears (Null value
in the table)
with Bind, the value displayed before clicking the Update button remains
unchanged
The update works for the other (normal) field ("operator").

UpdateCommand="UPDATE [mytable] SET [wa] = @wa, [operator] = @operator WHERE
[id] = @id">
<UpdateParameters>
<asp:Parameter Name="wa" Type="String" />
<asp:Parameter Name="operator" Type="String" />
</UpdateParameters>

<asp:TemplateField HeaderText="wa">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#
Bind("wa") %>' />
<asp:DropDownList ID="DropDownList2" runat="server"
Width="90px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)
dd = e.Row.FindControl("DropDownList2")
dd.Items.Add(z)
dd.SelectedValue = HF.Value
For i = 1 To 20
If i <CInt(HF.Value) Then
z = New ListItem(HF.Value, HF.Value)
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
End If
Next

Thanks for your time.
Ben
Jun 27 '08 #11
if I get what your saying, because the DD is now unbound you will need add
an event on the DD that assigns the new value to the HF.

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

Dim HF As HiddenField = sender.FindControl("HiddenField1")

Dim dd As DropDownList = sender.FindControl("DropDownList1")

If Not HF Is Nothing Then

HF.Value = dd.SelectedValue

End If

End Sub

"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Hi John,

i tried your code: in normal mode, the values are rendered normally;
with Eval, when i click on Update button, the value disappears (Null value
in the table)
with Bind, the value displayed before clicking the Update button remains
unchanged
The update works for the other (normal) field ("operator").

UpdateCommand="UPDATE [mytable] SET [wa] = @wa, [operator] = @operator
WHERE [id] = @id">
<UpdateParameters>
<asp:Parameter Name="wa" Type="String" />
<asp:Parameter Name="operator" Type="String" />
</UpdateParameters>

<asp:TemplateField HeaderText="wa">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#
Bind("wa") %>' />
<asp:DropDownList ID="DropDownList2" runat="server"
Width="90px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)
dd = e.Row.FindControl("DropDownList2")
dd.Items.Add(z)
dd.SelectedValue = HF.Value
For i = 1 To 20
If i <CInt(HF.Value) Then
z = New ListItem(HF.Value, HF.Value)
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
End If
Next

Thanks for your time.
Ben


Jun 27 '08 #12
Ben
Sorry, but i still get NULL with Eval and the same value with BIND.
I wonder whether the event DropDownList1_SelectedIndexChanged will be fired,
because dropdownlist is embedded in the gridview ...
I put a response.write("ok") in it, but this is never executed.

Tricky problem, isn't it?

"John" <no*@non.comschreef in bericht
news:ua**************@TK2MSFTNGP02.phx.gbl...
if I get what your saying, because the DD is now unbound you will need add
an event on the DD that assigns the new value to the HF.

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

Dim HF As HiddenField = sender.FindControl("HiddenField1")

Dim dd As DropDownList = sender.FindControl("DropDownList1")

If Not HF Is Nothing Then

HF.Value = dd.SelectedValue

End If

End Sub

"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>Hi John,

i tried your code: in normal mode, the values are rendered normally;
with Eval, when i click on Update button, the value disappears (Null
value in the table)
with Bind, the value displayed before clicking the Update button remains
unchanged
The update works for the other (normal) field ("operator").

UpdateCommand="UPDATE [mytable] SET [wa] = @wa, [operator] = @operator
WHERE [id] = @id">
<UpdateParameters>
<asp:Parameter Name="wa" Type="String" />
<asp:Parameter Name="operator" Type="String" />
</UpdateParameters>

<asp:TemplateField HeaderText="wa">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#
Bind("wa") %>' />
<asp:DropDownList ID="DropDownList2" runat="server"
Width="90px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)
dd = e.Row.FindControl("DropDownList2")
dd.Items.Add(z)
dd.SelectedValue = HF.Value
For i = 1 To 20
If i <CInt(HF.Value) Then
z = New ListItem(HF.Value, HF.Value)
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
End If
Next

Thanks for your time.
Ben



Jun 27 '08 #13
check to see if DD autopostback is set to true, if it still does not fire,
try puting that code in the event updating.

it is firing in my test though and saving changes
John

"Ben" <be*@nol.vbwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
Sorry, but i still get NULL with Eval and the same value with BIND.
I wonder whether the event DropDownList1_SelectedIndexChanged will be
fired, because dropdownlist is embedded in the gridview ...
I put a response.write("ok") in it, but this is never executed.

Tricky problem, isn't it?

"John" <no*@non.comschreef in bericht
news:ua**************@TK2MSFTNGP02.phx.gbl...
>if I get what your saying, because the DD is now unbound you will need
add an event on the DD that assigns the new value to the HF.

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

Dim HF As HiddenField = sender.FindControl("HiddenField1")

Dim dd As DropDownList = sender.FindControl("DropDownList1")

If Not HF Is Nothing Then

HF.Value = dd.SelectedValue

End If

End Sub

"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>>Hi John,

i tried your code: in normal mode, the values are rendered normally;
with Eval, when i click on Update button, the value disappears (Null
value in the table)
with Bind, the value displayed before clicking the Update button remains
unchanged
The update works for the other (normal) field ("operator").

UpdateCommand="UPDATE [mytable] SET [wa] = @wa, [operator] = @operator
WHERE [id] = @id">
<UpdateParameters>
<asp:Parameter Name="wa" Type="String" />
<asp:Parameter Name="operator" Type="String" />
</UpdateParameters>

<asp:TemplateField HeaderText="wa">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#
Bind("wa") %>' />
<asp:DropDownList ID="DropDownList2" runat="server"
Width="90px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)
dd = e.Row.FindControl("DropDownList2")
dd.Items.Add(z)
dd.SelectedValue = HF.Value
For i = 1 To 20
If i <CInt(HF.Value) Then
z = New ListItem(HF.Value, HF.Value)
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
End If
Next

Thanks for your time.
Ben




Jun 27 '08 #14
Ben
I'm a little bit confused and probably it's due to something wrong in my
code i don't see, but now i tried this:

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles
GridView1.RowUpdating
response.write("ok")
Dim HF As HiddenField = GridView1.FindControl("HiddenField1")
Dim dd As DropDownList = sender.FindControl("DropDownList2")
HF.Value = dd.SelectedValue
Response.Write(HF.Value & " " & dd.SelectedValue)
End Sub

and i put the DD autoPostBack=true, but, although this event is fired (i see
the "ok"), still same result: with Eval: NULL, with Bind: updated value
remains the same.

If your code works, would you mind to give me your aspx file and the
code-behind?
Thanks
"John" <no*@non.comschreef in bericht
news:Oi**************@TK2MSFTNGP04.phx.gbl...
check to see if DD autopostback is set to true, if it still does not fire,
try puting that code in the event updating.

it is firing in my test though and saving changes
John

"Ben" <be*@nol.vbwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>Sorry, but i still get NULL with Eval and the same value with BIND.
I wonder whether the event DropDownList1_SelectedIndexChanged will be
fired, because dropdownlist is embedded in the gridview ...
I put a response.write("ok") in it, but this is never executed.

Tricky problem, isn't it?

"John" <no*@non.comschreef in bericht
news:ua**************@TK2MSFTNGP02.phx.gbl...
>>if I get what your saying, because the DD is now unbound you will need
add an event on the DD that assigns the new value to the HF.

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

Dim HF As HiddenField = sender.FindControl("HiddenField1")

Dim dd As DropDownList = sender.FindControl("DropDownList1")

If Not HF Is Nothing Then

HF.Value = dd.SelectedValue

End If

End Sub

"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .
Hi John,

i tried your code: in normal mode, the values are rendered normally;
with Eval, when i click on Update button, the value disappears (Null
value in the table)
with Bind, the value displayed before clicking the Update button
remains unchanged
The update works for the other (normal) field ("operator").

UpdateCommand="UPDATE [mytable] SET [wa] = @wa, [operator] = @operator
WHERE [id] = @id">
<UpdateParameters>
<asp:Parameter Name="wa" Type="String" />
<asp:Parameter Name="operator" Type="String" />
</UpdateParameters>

<asp:TemplateField HeaderText="wa">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#
Bind("wa") %>' />
<asp:DropDownList ID="DropDownList2" runat="server"
Width="90px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)
dd = e.Row.FindControl("DropDownList2")
dd.Items.Add(z)
dd.SelectedValue = HF.Value
For i = 1 To 20
If i <CInt(HF.Value) Then
z = New ListItem(HF.Value, HF.Value)
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
End If
Next

Thanks for your time.
Ben




Jun 27 '08 #15
Ben
when i remove the If Not HF Is Nothing Then

i get the error: Object reference not set to an instance of an object.
at line: HF.Value = dd.SelectedValue

it seems that the hiddenfield is not recognized.

"John" <no*@non.comschreef in bericht
news:Oi**************@TK2MSFTNGP04.phx.gbl...
check to see if DD autopostback is set to true, if it still does not fire,
try puting that code in the event updating.

it is firing in my test though and saving changes
John

"Ben" <be*@nol.vbwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>Sorry, but i still get NULL with Eval and the same value with BIND.
I wonder whether the event DropDownList1_SelectedIndexChanged will be
fired, because dropdownlist is embedded in the gridview ...
I put a response.write("ok") in it, but this is never executed.

Tricky problem, isn't it?

"John" <no*@non.comschreef in bericht
news:ua**************@TK2MSFTNGP02.phx.gbl...
>>if I get what your saying, because the DD is now unbound you will need
add an event on the DD that assigns the new value to the HF.

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

Dim HF As HiddenField = sender.FindControl("HiddenField1")

Dim dd As DropDownList = sender.FindControl("DropDownList1")

If Not HF Is Nothing Then

HF.Value = dd.SelectedValue

End If

End Sub

"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .
Hi John,

i tried your code: in normal mode, the values are rendered normally;
with Eval, when i click on Update button, the value disappears (Null
value in the table)
with Bind, the value displayed before clicking the Update button
remains unchanged
The update works for the other (normal) field ("operator").

UpdateCommand="UPDATE [mytable] SET [wa] = @wa, [operator] = @operator
WHERE [id] = @id">
<UpdateParameters>
<asp:Parameter Name="wa" Type="String" />
<asp:Parameter Name="operator" Type="String" />
</UpdateParameters>

<asp:TemplateField HeaderText="wa">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%#
Bind("wa") %>' />
<asp:DropDownList ID="DropDownList2" runat="server"
Width="90px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("wa")
%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)
dd = e.Row.FindControl("DropDownList2")
dd.Items.Add(z)
dd.SelectedValue = HF.Value
For i = 1 To 20
If i <CInt(HF.Value) Then
z = New ListItem(HF.Value, HF.Value)
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
End If
Next

Thanks for your time.
Ben




Jun 27 '08 #16
Keep in mind I am using a different database than you

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="newsgroup.aspx.vb"
Inherits="Default4" %>

<!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>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

DataKeyNames="TaskID" DataSourceID="SqlTempTbl">

<Columns>

<asp:CommandField ShowEditButton="True" ShowDeleteButton="True"

ShowSelectButton="True" />

<asp:BoundField DataField="Description" HeaderText="Description"

SortExpression="Description" />

<asp:BoundField DataField="TaskID" HeaderText="TaskID" InsertVisible="False"

ReadOnly="True" SortExpression="TaskID" />

<asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task" />

<asp:BoundField DataField="TaskGroup" HeaderText="TaskGroup"

SortExpression="TaskGroup" />

<asp:BoundField DataField="FrKey" HeaderText="FrKey" SortExpression="FrKey"
/>

<asp:TemplateField HeaderText="Edit">

<EditItemTemplate>

<asp:HiddenField ID="HiddenField1" runat="server"

Value='<%# Bind("TaskGroup") %>' />

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

onselectedindexchanged="DropDownList1_SelectedInde xChanged">

</asp:DropDownList>

</EditItemTemplate>

</asp:TemplateField>

</Columns>
</asp:GridView>

</div>
<asp:SqlDataSource ID="SqlTempTbl"

runat="server" ConnectionString="<%$ ConnectionStrings:ItemConnStr %>"


SelectCommand="SELECT [Description], [TaskID], [Task], [TaskGroup], [FrKey]
FROM [tblTask]"

DeleteCommand="DELETE FROM [tblTask] WHERE [TaskID] = @TaskID"

InsertCommand="INSERT INTO [tblTask] ([Description], [Task], [TaskGroup],
[FrKey]) VALUES (@Description, @Task, @TaskGroup, @FrKey)"

UpdateCommand="UPDATE [tblTask] SET [Description] = @Description, [Task] =
@Task, [TaskGroup] = @TaskGroup, [FrKey] = @FrKey WHERE [TaskID] = @TaskID">

<DeleteParameters>

<asp:Parameter Name="TaskID" Type="Int32" />

</DeleteParameters>

<UpdateParameters>

<asp:Parameter Name="Description" Type="String" />

<asp:Parameter Name="Task" Type="String" />

<asp:Parameter Name="TaskGroup" Type="String" />

<asp:Parameter Name="FrKey" Type="Int32" />

<asp:Parameter Name="TaskID" Type="Int32" />

</UpdateParameters>

<InsertParameters>

<asp:Parameter Name="Description" Type="String" />

<asp:Parameter Name="Task" Type="String" />

<asp:Parameter Name="TaskGroup" Type="String" />

<asp:Parameter Name="FrKey" Type="Int32" />

</InsertParameters>

</asp:SqlDataSource>
</form>

</body>

</html>

Partial Class Default4

Inherits System.Web.UI.Page

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If (e.Row.RowState And DataControlRowState.Edit) = DataControlRowState.Edit
Then

If e.Row.RowType = DataControlRowType.DataRow Then

Dim HF As HiddenField = e.Row.FindControl("HiddenField1")

Dim dd As DropDownList

dd = e.Row.FindControl("DropDownList1")

If Not dd Is Nothing Then

Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)

dd.Items.Add(z)

dd.SelectedValue = HF.Value

For i = 1 To 20

If i <CInt(HF.Value) Then

z = New ListItem(i.ToString, i.ToString)

dd.Items.Add(z)

End If

Next

End If

End If

End If

End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)

Dim HF As HiddenField = sender.FindControl("HiddenField1")

Dim dd As DropDownList = sender.FindControl("DropDownList1")

If Not HF Is Nothing Then

HF.Value = dd.SelectedValue

End If

End Sub

End Class

"Ben" <be*@nol.vbwrote in message
news:eb**************@TK2MSFTNGP05.phx.gbl...
when i remove the If Not HF Is Nothing Then

i get the error: Object reference not set to an instance of an object.
at line: HF.Value = dd.SelectedValue

it seems that the hiddenfield is not recognized.

"John" <no*@non.comschreef in bericht
news:Oi**************@TK2MSFTNGP04.phx.gbl...
>check to see if DD autopostback is set to true, if it still does not
fire, try puting that code in the event updating.

it is firing in my test though and saving changes
John

"Ben" <be*@nol.vbwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>>Sorry, but i still get NULL with Eval and the same value with BIND.
I wonder whether the event DropDownList1_SelectedIndexChanged will be
fired, because dropdownlist is embedded in the gridview ...
I put a response.write("ok") in it, but this is never executed.

Tricky problem, isn't it?

"John" <no*@non.comschreef in bericht
news:ua**************@TK2MSFTNGP02.phx.gbl...
if I get what your saying, because the DD is now unbound you will need
add an event on the DD that assigns the new value to the HF.

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

Dim HF As HiddenField = sender.FindControl("HiddenField1")

Dim dd As DropDownList = sender.FindControl("DropDownList1")

If Not HF Is Nothing Then

HF.Value = dd.SelectedValue

End If

End Sub

"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl. ..
Hi John,
>
i tried your code: in normal mode, the values are rendered normally;
with Eval, when i click on Update button, the value disappears (Null
value in the table)
with Bind, the value displayed before clicking the Update button
remains unchanged
The update works for the other (normal) field ("operator").
>
UpdateCommand="UPDATE [mytable] SET [wa] = @wa, [operator] = @operator
WHERE [id] = @id">
<UpdateParameters>
<asp:Parameter Name="wa" Type="String" />
<asp:Parameter Name="operator" Type="String" />
</UpdateParameters>
>
<asp:TemplateField HeaderText="wa">
<EditItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server"
Value='<%# Bind("wa") %>' />
<asp:DropDownList ID="DropDownList2" runat="server"
Width="90px">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%#
Bind("wa") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
>
>
Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
Dim dd As DropDownList
Dim i As Integer
Dim z As ListItem
>
z = New ListItem(HF.Value, HF.Value)
dd = e.Row.FindControl("DropDownList2")
dd.Items.Add(z)
dd.SelectedValue = HF.Value
For i = 1 To 20
If i <CInt(HF.Value) Then
z = New ListItem(HF.Value, HF.Value)
z = New ListItem(i.ToString, i.ToString)
dd.Items.Add(z)
End If
Next
>
Thanks for your time.
Ben
>
>




Jun 27 '08 #17
Ben
Thank you very much, John. It works now
I forgot to put onselectedindexchanged="DropDownList2_SelectedInde xChanged"
in the hiddenfield tag.
"John" <no*@non.comschreef in bericht
news:%2******************@TK2MSFTNGP02.phx.gbl...
Keep in mind I am using a different database than you

<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="newsgroup.aspx.vb" Inherits="Default4" %>

<!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>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"

DataKeyNames="TaskID" DataSourceID="SqlTempTbl">

<Columns>

<asp:CommandField ShowEditButton="True" ShowDeleteButton="True"

ShowSelectButton="True" />

<asp:BoundField DataField="Description" HeaderText="Description"

SortExpression="Description" />

<asp:BoundField DataField="TaskID" HeaderText="TaskID"
InsertVisible="False"

ReadOnly="True" SortExpression="TaskID" />

<asp:BoundField DataField="Task" HeaderText="Task" SortExpression="Task"
/>

<asp:BoundField DataField="TaskGroup" HeaderText="TaskGroup"

SortExpression="TaskGroup" />

<asp:BoundField DataField="FrKey" HeaderText="FrKey"
SortExpression="FrKey" />

<asp:TemplateField HeaderText="Edit">

<EditItemTemplate>

<asp:HiddenField ID="HiddenField1" runat="server"

Value='<%# Bind("TaskGroup") %>' />

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"

onselectedindexchanged="DropDownList1_SelectedInde xChanged">

</asp:DropDownList>

</EditItemTemplate>

</asp:TemplateField>

</Columns>
</asp:GridView>

</div>
<asp:SqlDataSource ID="SqlTempTbl"

runat="server" ConnectionString="<%$ ConnectionStrings:ItemConnStr %>"


SelectCommand="SELECT [Description], [TaskID], [Task], [TaskGroup],
[FrKey] FROM [tblTask]"

DeleteCommand="DELETE FROM [tblTask] WHERE [TaskID] = @TaskID"

InsertCommand="INSERT INTO [tblTask] ([Description], [Task], [TaskGroup],
[FrKey]) VALUES (@Description, @Task, @TaskGroup, @FrKey)"

UpdateCommand="UPDATE [tblTask] SET [Description] = @Description, [Task] =
@Task, [TaskGroup] = @TaskGroup, [FrKey] = @FrKey WHERE [TaskID] =
@TaskID">

<DeleteParameters>

<asp:Parameter Name="TaskID" Type="Int32" />

</DeleteParameters>

<UpdateParameters>

<asp:Parameter Name="Description" Type="String" />

<asp:Parameter Name="Task" Type="String" />

<asp:Parameter Name="TaskGroup" Type="String" />

<asp:Parameter Name="FrKey" Type="Int32" />

<asp:Parameter Name="TaskID" Type="Int32" />

</UpdateParameters>

<InsertParameters>

<asp:Parameter Name="Description" Type="String" />

<asp:Parameter Name="Task" Type="String" />

<asp:Parameter Name="TaskGroup" Type="String" />

<asp:Parameter Name="FrKey" Type="Int32" />

</InsertParameters>

</asp:SqlDataSource>
</form>

</body>

</html>

Partial Class Default4

Inherits System.Web.UI.Page

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles
GridView1.RowDataBound

If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then

If e.Row.RowType = DataControlRowType.DataRow Then

Dim HF As HiddenField = e.Row.FindControl("HiddenField1")

Dim dd As DropDownList

dd = e.Row.FindControl("DropDownList1")

If Not dd Is Nothing Then

Dim z As ListItem

z = New ListItem(HF.Value, HF.Value)

dd.Items.Add(z)

dd.SelectedValue = HF.Value

For i = 1 To 20

If i <CInt(HF.Value) Then

z = New ListItem(i.ToString, i.ToString)

dd.Items.Add(z)

End If

Next

End If

End If

End If

End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs)

Dim HF As HiddenField = sender.FindControl("HiddenField1")

Dim dd As DropDownList = sender.FindControl("DropDownList1")

If Not HF Is Nothing Then

HF.Value = dd.SelectedValue

End If

End Sub

End Class

"Ben" <be*@nol.vbwrote in message
news:eb**************@TK2MSFTNGP05.phx.gbl...
>when i remove the If Not HF Is Nothing Then

i get the error: Object reference not set to an instance of an object.
at line: HF.Value = dd.SelectedValue

it seems that the hiddenfield is not recognized.

"John" <no*@non.comschreef in bericht
news:Oi**************@TK2MSFTNGP04.phx.gbl...
>>check to see if DD autopostback is set to true, if it still does not
fire, try puting that code in the event updating.

it is firing in my test though and saving changes
John

"Ben" <be*@nol.vbwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
Sorry, but i still get NULL with Eval and the same value with BIND.
I wonder whether the event DropDownList1_SelectedIndexChanged will be
fired, because dropdownlist is embedded in the gridview ...
I put a response.write("ok") in it, but this is never executed.

Tricky problem, isn't it?

"John" <no*@non.comschreef in bericht
news:ua**************@TK2MSFTNGP02.phx.gbl...
if I get what your saying, because the DD is now unbound you will need
add an event on the DD that assigns the new value to the HF.
>
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs)
>
Dim HF As HiddenField = sender.FindControl("HiddenField1")
>
Dim dd As DropDownList = sender.FindControl("DropDownList1")
>
If Not HF Is Nothing Then
>
HF.Value = dd.SelectedValue
>
End If
>
End Sub
>
>
>
"Ben" <be*@nol.vbwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl.. .
>Hi John,
>>
>i tried your code: in normal mode, the values are rendered normally;
>with Eval, when i click on Update button, the value disappears (Null
>value in the table)
>with Bind, the value displayed before clicking the Update button
>remains unchanged
>The update works for the other (normal) field ("operator").
>>
>UpdateCommand="UPDATE [mytable] SET [wa] = @wa, [operator] =
>@operator WHERE [id] = @id">
><UpdateParameters>
><asp:Parameter Name="wa" Type="String" />
><asp:Parameter Name="operator" Type="String" />
></UpdateParameters>
>>
><asp:TemplateField HeaderText="wa">
> <EditItemTemplate>
> <asp:HiddenField ID="HiddenField1" runat="server"
>Value='<%# Bind("wa") %>' />
> <asp:DropDownList ID="DropDownList2" runat="server"
>Width="90px">
> </asp:DropDownList>
> </EditItemTemplate>
> <ItemTemplate>
> <asp:Label ID="Label2" runat="server" Text='<%#
>Bind("wa") %>'></asp:Label>
> </ItemTemplate>
> </asp:TemplateField>
>>
>>
>Dim HF As HiddenField = e.Row.FindControl("HiddenField1")
> Dim dd As DropDownList
> Dim i As Integer
> Dim z As ListItem
>>
> z = New ListItem(HF.Value, HF.Value)
> dd = e.Row.FindControl("DropDownList2")
> dd.Items.Add(z)
> dd.SelectedValue = HF.Value
> For i = 1 To 20
> If i <CInt(HF.Value) Then
> z = New ListItem(HF.Value, HF.Value)
> z = New ListItem(i.ToString, i.ToString)
> dd.Items.Add(z)
> End If
> Next
>>
>Thanks for your time.
>Ben
>>
>>
>
>




Jun 27 '08 #18
Hi,

I have an article explaining the problem and how to solve it here:

http://blog.evonet.com.au/post/2008/...-SelectedValue
-which-is-invalid-because-it-does-not-exist-in-the-list-of-items.aspx

Hope this helps,
Bartek

*** Sent via Developersdex http://www.developersdex.com ***
Aug 11 '08 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Carlo Marchesoni | last post by:
I try to move textBoxes and ddl dynamically to a ASP:table and everything works correctly, except that for the ddl's the selectedValue is not retained (it is for the TextBoxes). Here is my code:...
0
by: Elmo Watson | last post by:
OK here's the scenario - GridView - populated with Object DataSource - technically no problem there (except what will be shown later in the post) Edit/Delete - first column - 'JobTypeID' - in...
1
by: John | last post by:
If you set DropDownList.SelectedValue to an item not in the list, shouldn't an exception be thrown? ie: <asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="1"...
0
by: Ben | last post by:
Hi, It's about a database containing (for simplifying) three fields: 'placenr' (primary key), 'place' and 'color'. The records are updatable (Edit/Update button) in a gridview. For updating the...
7
by: bryant | last post by:
Hi all. I am new to ASP and working in Expression Web. The following query displays the information I need in the gridview for a single record. SELECT "OE_HDR"."ORD_NO", "OE_HDR"."CUST_NAM",...
0
by: Ben | last post by:
Hi, With this code, when i click on Update button, the value updated of field "wa" in the table is NULL.The update of the other field (not in a DD) works good. It seems that the selectedvalue...
0
by: Ben | last post by:
Hi, With this code, when i click on Update button, the value updated of field "wa" in the table is NULL.The update of the other field (not in a DD) works good. It seems that the selectedvalue...
0
by: randy.buchholz | last post by:
I am trying to trap the following error. I have tried to catch it on page load and every event on the data source and dd. No luck. I'm forcing the error by setting the dd.text to an invalid value...
1
by: Brett | last post by:
I have a DropDownList in an ASP.NET web form that is populated with items from a lookup table by binding that DropDownList to a SqlDataSource. However, the items in the lookup table can change over...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.