problem with dropdownlists in gridview

Discussion in 'ASP .Net' started by Mark, Jul 30, 2007.

  1. Mark

    Mark Guest

    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
    Mark, Jul 30, 2007
    #1
    1. Advertising

  2. Mark

    Mark Guest

    Yes that's it.
    Thanks a lot.

    "Eliyahu Goldin" <> schreef in
    bericht news:%...
    > 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" <> wrote in message
    > news:...
    >> 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
    >>
    >>

    >
    >
    Mark, Jul 30, 2007
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:...
    > 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
    >
    >
    Eliyahu Goldin, Jul 30, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmFzIFBhYXA=?=

    Problem with FormView and DropDownLists

    =?Utf-8?B?QmFzIFBhYXA=?=, Apr 10, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    10,521
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Apr 11, 2006
  2. =?Utf-8?B?cm9kY2hhcg==?=

    dropdownlists and gridview

    =?Utf-8?B?cm9kY2hhcg==?=, Nov 3, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    245
    =?Utf-8?B?cm9kY2hhcg==?=
    Nov 3, 2006
  3. Wouter
    Replies:
    0
    Views:
    393
    Wouter
    Nov 29, 2006
  4. ~john
    Replies:
    5
    Views:
    342
    Mark Rae
    Dec 19, 2006
  5. Carlos
    Replies:
    0
    Views:
    312
    Carlos
    Dec 30, 2006
Loading...

Share This Page