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. Drop Down Lists

Drop Down Lists

Scheduled Pinned Locked Moved ASP.NET
help
6 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

    Hi friends, I want to create drop down lists for all the rows of a gridview. But i dont want to use TemplateField because i want to give different id's for all drop downs. Can anybody help me please.

    scarface

    A 1 Reply Last reply
    0
    • S scar_face

      Hi friends, I want to create drop down lists for all the rows of a gridview. But i dont want to use TemplateField because i want to give different id's for all drop downs. Can anybody help me please.

      scarface

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

      Hi friend, Can u provide yr exact requirement, like why u need to give unique ID to all Dropdown list at server side. So that i can help u. And what problem u have with TemplateField?

      Regards Anil Pal

      S 1 Reply Last reply
      0
      • A anilpal

        Hi friend, Can u provide yr exact requirement, like why u need to give unique ID to all Dropdown list at server side. So that i can help u. And what problem u have with TemplateField?

        Regards Anil Pal

        S Offline
        S Offline
        scar_face
        wrote on last edited by
        #3

        Hi, I am trying to create two sets of drop down lists in a gridview. If i change the index of the first drop down, the values of the corresponding drop down should change. I used event handler for the first drop down, but if i change the index of any one of the drop downs in the first set, all the values of second drop downs are getting changed.

        scarface

        A S 2 Replies Last reply
        0
        • S scar_face

          Hi, I am trying to create two sets of drop down lists in a gridview. If i change the index of the first drop down, the values of the corresponding drop down should change. I used event handler for the first drop down, but if i change the index of any one of the drop downs in the first set, all the values of second drop downs are getting changed.

          scarface

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

          Hi, Now i understand yr problem. So the initialization of second set of dropdown list u can not stop, while changing of index of first set of drop down list. So to maintain the state of the second dropdown set. First u need to take the state of all the dropdown list from second set and the again u need to set their state while changing the index of first one. Now please let me know if you code for that or any query.

          Regards Anil Pal

          1 Reply Last reply
          0
          • S scar_face

            Hi, I am trying to create two sets of drop down lists in a gridview. If i change the index of the first drop down, the values of the corresponding drop down should change. I used event handler for the first drop down, but if i change the index of any one of the drop downs in the first set, all the values of second drop downs are getting changed.

            scarface

            S Offline
            S Offline
            scar_face
            wrote on last edited by
            #5

            Please have a look at my code:

            <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.

            scarface

            A 1 Reply Last reply
            0
            • S scar_face

              Please have a look at my code:

              <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.

              scarface

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

              Hi, I have gone through the your code. int i = 0; while (dr.Read()) { ddl.Items.Insert(i, dr.GetString(0)); } dr.Close(); In the above code where u increment the I . According to yr code there always i=0. Hence it replicate the value. Why u can not use Datatable or dataset. Howevere using of Collection of object is good choice.

              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