problem with dropdownlists in gridview

M

Mark

Hi,

There are two dropdownlists in two EditItemTemplates of a gridview. They are
populated programmatically (see code below). This works.

But now, the selected value of the first ddl (dropdownlist1) takes always
the selected value of the second ddl (dropdownlist2) , and the new value
chosen for update in the first ddl is also always the value chosen (or
selectedvalue if not chosen) of the second ddl. It seems that the second ddl
overrules the first one. In normal mode, the values of both ddl are rendered
correctly.

I can't find any reason for that, and the only way i found is to populate
one of the ddl manually (in aspx file). But that is fastidious, isn't it?)
Thanks for help
Mark

Here the code in aspx file:

<asp:TemplateField HeaderText="min" SortExpression="min">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("min") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("min") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="max" SortExpression="max">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%#
Bind("max") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("max") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

and here thet code-behind:

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dd1, dd2 As DropDownList
Dim z As ListItem
Dim i As Integer
dd1 = e.Row.FindControl("dropdownlist1")
dd2 = e.Row.FindControl("dropdownlist2")
For i = 0 To 200
z = New ListItem(i, i)
dd1.Items.Add(z)
dd2.Items.Add(z)
Next
End If
End If
End Sub
 
M

Mark

Yes that's it.
Thanks a lot.

Eliyahu Goldin said:
The problem is in 2 ddls sharing the same set of items. When you select an
item in the first ddl, the item becomes "Selected". "Selected" is an
attribute of the item itself and not of the ddl that contains the item.
This means that the item becomes "Selected" in all ddls it belongs to.

To solve the problem do

For i = 0 To 200
z1 = New ListItem(i, i)
z2 = New ListItem(i, i)
dd1.Items.Add(z1)
dd2.Items.Add(z2)
Next


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Mark said:
Hi,

There are two dropdownlists in two EditItemTemplates of a gridview. They are
populated programmatically (see code below). This works.

But now, the selected value of the first ddl (dropdownlist1) takes always
the selected value of the second ddl (dropdownlist2) , and the new value
chosen for update in the first ddl is also always the value chosen (or
selectedvalue if not chosen) of the second ddl. It seems that the second ddl
overrules the first one. In normal mode, the values of both ddl are rendered
correctly.

I can't find any reason for that, and the only way i found is to populate
one of the ddl manually (in aspx file). But that is fastidious, isn't
it?)
Thanks for help
Mark

Here the code in aspx file:

<asp:TemplateField HeaderText="min" SortExpression="min">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("min") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("min")
%>'> said:
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="max" SortExpression="max">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%#
Bind("max") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("max")
%>'> said:
</ItemTemplate>
</asp:TemplateField>

and here thet code-behind:

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
If (e.Row.RowState And DataControlRowState.Edit) =
DataControlRowState.Edit Then
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dd1, dd2 As DropDownList
Dim z As ListItem
Dim i As Integer
dd1 = e.Row.FindControl("dropdownlist1")
dd2 = e.Row.FindControl("dropdownlist2")
For i = 0 To 200
z = New ListItem(i, i)
dd1.Items.Add(z)
dd2.Items.Add(z)
Next
End If
End If
End Sub
 
E

Eliyahu Goldin

The problem is in 2 ddls sharing the same set of items. When you select an
item in the first ddl, the item becomes "Selected". "Selected" is an
attribute of the item itself and not of the ddl that contains the item.
This means that the item becomes "Selected" in all ddls it belongs to.

To solve the problem do

For i = 0 To 200
z1 = New ListItem(i, i)
z2 = New ListItem(i, i)
dd1.Items.Add(z1)
dd2.Items.Add(z2)
Next


--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin


Mark said:
Hi,

There are two dropdownlists in two EditItemTemplates of a gridview. They are
populated programmatically (see code below). This works.

But now, the selected value of the first ddl (dropdownlist1) takes always
the selected value of the second ddl (dropdownlist2) , and the new value
chosen for update in the first ddl is also always the value chosen (or
selectedvalue if not chosen) of the second ddl. It seems that the second ddl
overrules the first one. In normal mode, the values of both ddl are rendered
correctly.

I can't find any reason for that, and the only way i found is to populate
one of the ddl manually (in aspx file). But that is fastidious, isn't it?)
Thanks for help
Mark

Here the code in aspx file:

<asp:TemplateField HeaderText="min" SortExpression="min">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("min") %>'></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("min")
 

Ask a Question

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

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

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top