DropDownList1' has a SelectedValue which is invalid

  • Thread starter Thread starter Ben
  • Start date Start date
B

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
 
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
 
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 ...
 
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
 
You may have to unbind the DD also as you are adding thru the HF and add the
text with the value.
John
 
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'
 
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
 
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?
 
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
 
Thanks

John said:
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
 
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
 
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
 
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 said:
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 said:
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
 
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 said:
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 said:
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 said:
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
 
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 said:
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 said:
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 said:
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



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
 
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 said:
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 said:
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 said:
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



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
 
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_SelectedIndexChanged">

</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 said:
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 said:
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 said:
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" <[email protected]> schreef in bericht
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



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
 
Thank you very much, John. It works now
I forgot to put onselectedindexchanged="DropDownList2_SelectedIndexChanged"
in the hiddenfield tag.


John said:
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_SelectedIndexChanged">

</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 said:
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 said:
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

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" <[email protected]> schreef in bericht
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



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
 
Back
Top