Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > problem with dropdownlists in gridview

Reply
Thread Tools

problem with dropdownlists in gridview

 
 
Mark
Guest
Posts: n/a
 
      07-30-2007
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>
<aspropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
Bind("min") %>'></aspropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("min") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="max" SortExpression="max">
<EditItemTemplate>
<aspropDownList ID="DropDownList2" runat="server" SelectedValue='<%#
Bind("max") %>'></aspropDownList>
</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


 
Reply With Quote
 
 
 
 
Mark
Guest
Posts: n/a
 
      07-30-2007
Yes that's it.
Thanks a lot.

"Eliyahu Goldin" <(E-Mail Removed)> schreef in
bericht news:%(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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>
>> <aspropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
>> Bind("min") %>'></aspropDownList>
>> </EditItemTemplate>
>> <ItemTemplate>
>> <asp:Label ID="Label2" runat="server" Text='<%# Bind("min")

> %>'></asp:Label>
>> </ItemTemplate>
>> </asp:TemplateField>
>>
>> <asp:TemplateField HeaderText="max" SortExpression="max">
>> <EditItemTemplate>
>> <aspropDownList ID="DropDownList2" runat="server" SelectedValue='<%#
>> Bind("max") %>'></aspropDownList>
>> </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
>>
>>

>
>



 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      07-30-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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>
> <aspropDownList ID="DropDownList1" runat="server" SelectedValue='<%#
> Bind("min") %>'></aspropDownList>
> </EditItemTemplate>
> <ItemTemplate>
> <asp:Label ID="Label2" runat="server" Text='<%# Bind("min")

%>'></asp:Label>
> </ItemTemplate>
> </asp:TemplateField>
>
> <asp:TemplateField HeaderText="max" SortExpression="max">
> <EditItemTemplate>
> <aspropDownList ID="DropDownList2" runat="server" SelectedValue='<%#
> Bind("max") %>'></aspropDownList>
> </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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dropdownlists as template fields in a GridView Carlos ASP .Net 0 12-30-2006 09:42 PM
Problem with DropDownLists and Post Backs ~john ASP .Net 5 12-19-2006 04:21 PM
Newbie problem with 2 dropdownlists to fill gridview Wouter ASP .Net 0 11-29-2006 04:19 PM
dropdownlists and gridview =?Utf-8?B?cm9kY2hhcg==?= ASP .Net 0 11-03-2006 10:31 PM
Problem with FormView and DropDownLists =?Utf-8?B?QmFzIFBhYXA=?= ASP .Net 3 04-11-2006 01:46 PM



Advertisments