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 list problem

drop down list problem

Scheduled Pinned Locked Moved ASP.NET
databasedebugginghelp
16 Posts 7 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.
  • _ Offline
    _ Offline
    _tasleem
    wrote on last edited by
    #1

    hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif

    T _ M V S 6 Replies Last reply
    0
    • _ _tasleem

      hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif

      T Offline
      T Offline
      Talal Sultan
      wrote on last edited by
      #2

      Are you filling the DropDownList with data when the page loads? i.e. in this method: private void Page_Load(object sender, System.EventArgs e) { } "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

      _ 2 Replies Last reply
      0
      • _ _tasleem

        hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif

        _ Offline
        _ Offline
        _AK_
        wrote on last edited by
        #3

        Hi, if you are using SelectedIndexChanged event then you need to make thae autopostback of the control to true. And to get the value of the selected item you have to use ddlMyDdl.SelectedItem.Value. Hope this will help you. Best Regards, Apurva Kaushal

        1 Reply Last reply
        0
        • _ _tasleem

          hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif

          M Offline
          M Offline
          Minuab
          wrote on last edited by
          #4

          Use the following code to fill combo dim dread As SqlDataReader Conn.Open() SqlCommand1.CommandText = "Select * from Table" dread = SqlCommand1.ExecuteReader ddl.DataSource = dread ddl.DataTextField = "Field1" ddl.DataValueField = "Field2" ddl.DataBind() dread.Close() On the Selectedindexchange of dropdown use this code label1.text=ddl.SelectedItem.text ' To get the item seleted label1.text=ddl.SelectedItem.Value ' To get value of item selected. hope this will help you.

          _ 1 Reply Last reply
          0
          • _ _tasleem

            hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif

            V Offline
            V Offline
            varun_khanna17
            wrote on last edited by
            #5

            I think u r filling Up the data in Dropdown at page load.If yes,Then at page load put page load() If not ispostback then //put the code to fill the dropdown here end if So when u submit u will see it moves to page load and then to submit function and put auto post back property of dropDown=True Bye Varun

            _ 1 Reply Last reply
            0
            • V varun_khanna17

              I think u r filling Up the data in Dropdown at page load.If yes,Then at page load put page load() If not ispostback then //put the code to fill the dropdown here end if So when u submit u will see it moves to page load and then to submit function and put auto post back property of dropDown=True Bye Varun

              _ Offline
              _ Offline
              _AK_
              wrote on last edited by
              #6

              this can do the trick. :) Best Regards, Apurva Kaushal

              1 Reply Last reply
              0
              • T Talal Sultan

                Are you filling the DropDownList with data when the page loads? i.e. in this method: private void Page_Load(object sender, System.EventArgs e) { } "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

                _ Offline
                _ Offline
                _tasleem
                wrote on last edited by
                #7

                yes Tasleem Arif

                _ T 2 Replies Last reply
                0
                • T Talal Sultan

                  Are you filling the DropDownList with data when the page loads? i.e. in this method: private void Page_Load(object sender, System.EventArgs e) { } "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

                  _ Offline
                  _ Offline
                  _tasleem
                  wrote on last edited by
                  #8

                  yes, and i want to put the data into databse by pressing submit button and getting the selected text Tasleem Arif

                  1 Reply Last reply
                  0
                  • M Minuab

                    Use the following code to fill combo dim dread As SqlDataReader Conn.Open() SqlCommand1.CommandText = "Select * from Table" dread = SqlCommand1.ExecuteReader ddl.DataSource = dread ddl.DataTextField = "Field1" ddl.DataValueField = "Field2" ddl.DataBind() dread.Close() On the Selectedindexchange of dropdown use this code label1.text=ddl.SelectedItem.text ' To get the item seleted label1.text=ddl.SelectedItem.Value ' To get value of item selected. hope this will help you.

                    _ Offline
                    _ Offline
                    _tasleem
                    wrote on last edited by
                    #9

                    i m using exactaly the same code to fill ddlist.and calling this function on page load event of webform. Tasleem Arif

                    1 Reply Last reply
                    0
                    • _ _tasleem

                      yes Tasleem Arif

                      _ Offline
                      _ Offline
                      _AK_
                      wrote on last edited by
                      #10

                      Is the code there in ispostback? Best Regards, Apurva Kaushal

                      _ 1 Reply Last reply
                      0
                      • _ _tasleem

                        yes Tasleem Arif

                        T Offline
                        T Offline
                        Talal Sultan
                        wrote on last edited by
                        #11

                        OK, the behavior you are getting is normal in this case. To understand why it is causing a problem, you need to understand how the execution of the page is done. Suppose you have a page that has been loaded and your dropdownlist is filled with data from the database. Now suppose the user selects something from the dropdownlist. When the page is posted back (either through autopostback or through a click of a button) the selectedIndexChanged event method will not be executed immediately..First, ASP.NET will execute the Page_Load method, and THEN it will execute the SelectedIndexChanged method. This means that if you are filling your dropdownlist in the Page_Load, it will be filled at every postback and consequently, the index 0 will always be selected (since this is what happens when a dropdownlist is filled) To remedy this problem, you need to make the fill of the dropdownlist only on the first load. After that, the data will remain in the dropdownlist if you keep the ViewState to true, which is by default the case. You need to use the isPostBack flag to determine if the page is being posted back or if it is the first load of the page. You would write something like this in the Page_Load method: // ...some code if (!IsPostBack) // on first load only { // here fill the dropdownlist from data from database } // ...some more code Doing this will resolve your problem (I hope!) Talal "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

                        _ 1 Reply Last reply
                        0
                        • _ _AK_

                          Is the code there in ispostback? Best Regards, Apurva Kaushal

                          _ Offline
                          _ Offline
                          _tasleem
                          wrote on last edited by
                          #12

                          no there is no ispostback Tasleem Arif

                          _ 1 Reply Last reply
                          0
                          • _ _tasleem

                            no there is no ispostback Tasleem Arif

                            _ Offline
                            _ Offline
                            _AK_
                            wrote on last edited by
                            #13

                            That is the cause of your problem. put the code which is binding the dropdownlist under that code like this: if(!IspostBack) { // place it here. } this will solve the problem. Best Regards, Apurva Kaushal

                            1 Reply Last reply
                            0
                            • T Talal Sultan

                              OK, the behavior you are getting is normal in this case. To understand why it is causing a problem, you need to understand how the execution of the page is done. Suppose you have a page that has been loaded and your dropdownlist is filled with data from the database. Now suppose the user selects something from the dropdownlist. When the page is posted back (either through autopostback or through a click of a button) the selectedIndexChanged event method will not be executed immediately..First, ASP.NET will execute the Page_Load method, and THEN it will execute the SelectedIndexChanged method. This means that if you are filling your dropdownlist in the Page_Load, it will be filled at every postback and consequently, the index 0 will always be selected (since this is what happens when a dropdownlist is filled) To remedy this problem, you need to make the fill of the dropdownlist only on the first load. After that, the data will remain in the dropdownlist if you keep the ViewState to true, which is by default the case. You need to use the isPostBack flag to determine if the page is being posted back or if it is the first load of the page. You would write something like this in the Page_Load method: // ...some code if (!IsPostBack) // on first load only { // here fill the dropdownlist from data from database } // ...some more code Doing this will resolve your problem (I hope!) Talal "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." --Rich Cook

                              _ Offline
                              _ Offline
                              _tasleem
                              wrote on last edited by
                              #14

                              thanks for such descriptive answer i will try this. Tasleem Arif

                              1 Reply Last reply
                              0
                              • _ _tasleem

                                hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif

                                S Offline
                                S Offline
                                Subhasis Chandra
                                wrote on last edited by
                                #15

                                you can try getting the value by Request["dropdownName"].ToString(); Subhasis Chandra -- modified at 8:14 Wednesday 31st May, 2006

                                1 Reply Last reply
                                0
                                • _ _tasleem

                                  hi all i want to get the selected value from the drop down list but it always give me the value at index 0 not the one i had selected.i set its postback to false and using this code to get the value in onselindexchange event strtext= ddlist.selecteditem.text and that variable always show me index 0 value when i debug that. Tasleem Arif

                                  P Offline
                                  P Offline
                                  postmaster programmingknowledge com
                                  wrote on last edited by
                                  #16

                                  Try, strtext= ddlist.SelectedValue Let me know if you still having problem. ======= Examples: http://www.programmingknowledge.com/[^]

                                  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