Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Crisis with a dynamically created drop down list

Crisis with a dynamically created drop down list

Scheduled Pinned Locked Moved ASP.NET
questioncsharpdatabasesysadminhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    scar_face
    wrote on last edited by
    #1

    Here's the deal! I'm creating two drop down lists dynamically in a GridView control and populating the second dropdownlist(let's call it ddl2 from now on) on the basis of the selection on the first dropdownlist control. The crisis is that, for multiple rows on the gridview the ddl2 starts appending values to itself and therefore ends up having repeats of the data it should originally hold. How do I stop this repeat from happening? My code is as below.

    <asp:GridView ID="grd" runat="server" OnRowDataBound="grd_RowDataBound"> <Columns> <asp:TemplateField HeaderText="names"> <ItemTemplate> <asp:DropDownList ID="name" runat="server" OnSelectedIndexChanged="onSelectChangeddl"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="age"> <ItemTemplate> <asp:DropDownList ID="age" runat="server" AutoPostBack="true"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

    In c# code, i am trying to change the values of the second drop down list based on the value selected in the first. I am doing this as below:

    foreach (GridViewRow row in grd.Rows) { DropDownList ddl1 = (DropDownList)row.FindControl("name"); DropDownList ddl = (DropDownList)row.FindControl("age"); if (ddl.SelectedValue.ToString() != "---Select---") { } else { ddl.Items.Clear(); } SqlCommand SPComm = new SqlCommand("someStoredProcedure", myConn); SPComm.CommandType = CommandType.StoredProcedure; SqlParameter param = SPComm.Parameters.Add("@something", SqlDbType.NVarChar, 50); param.Value = something; dr = SPComm.ExecuteReader(); //dr is sqlDataReader int i = 0; while (dr.Read()) { ddl.Items.Insert(i, dr.GetString(0)); } dr.Close(); ddl.Items.Insert(0, "---Select---"); }

    In the above code, all the values in the second drop down list are getting duplicated based on the selected index changed event handler of the first drop down list. Can anyone help me with this? Been trying all kinds of stuff with this since three weeks now! H-e-L-p!

    scarface

    A 1 Reply Last reply
    0
    • S scar_face

      Here's the deal! I'm creating two drop down lists dynamically in a GridView control and populating the second dropdownlist(let's call it ddl2 from now on) on the basis of the selection on the first dropdownlist control. The crisis is that, for multiple rows on the gridview the ddl2 starts appending values to itself and therefore ends up having repeats of the data it should originally hold. How do I stop this repeat from happening? My code is as below.

      <asp:GridView ID="grd" runat="server" OnRowDataBound="grd_RowDataBound"> <Columns> <asp:TemplateField HeaderText="names"> <ItemTemplate> <asp:DropDownList ID="name" runat="server" OnSelectedIndexChanged="onSelectChangeddl"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="age"> <ItemTemplate> <asp:DropDownList ID="age" runat="server" AutoPostBack="true"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>

      In c# code, i am trying to change the values of the second drop down list based on the value selected in the first. I am doing this as below:

      foreach (GridViewRow row in grd.Rows) { DropDownList ddl1 = (DropDownList)row.FindControl("name"); DropDownList ddl = (DropDownList)row.FindControl("age"); if (ddl.SelectedValue.ToString() != "---Select---") { } else { ddl.Items.Clear(); } SqlCommand SPComm = new SqlCommand("someStoredProcedure", myConn); SPComm.CommandType = CommandType.StoredProcedure; SqlParameter param = SPComm.Parameters.Add("@something", SqlDbType.NVarChar, 50); param.Value = something; dr = SPComm.ExecuteReader(); //dr is sqlDataReader int i = 0; while (dr.Read()) { ddl.Items.Insert(i, dr.GetString(0)); } dr.Close(); ddl.Items.Insert(0, "---Select---"); }

      In the above code, all the values in the second drop down list are getting duplicated based on the selected index changed event handler of the first drop down list. Can anyone help me with this? Been trying all kinds of stuff with this since three weeks now! H-e-L-p!

      scarface

      A Offline
      A Offline
      anilpal
      wrote on last edited by
      #2

      Hi, I am also seeing the you are facing same kind of problem sice last couple of week. Please send me your complete requirement. at anil.bly@gmail.com I will develop complete solution for you and send it to u.

      Regards Anil Pal

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups