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

Dropdownlist problem

Scheduled Pinned Locked Moved ASP.NET
helpdatabasequestion
8 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.
  • K Offline
    K Offline
    khuzwayom
    wrote on last edited by
    #1

    Hi All Please help, here us the scenario. I get Source and Source code from the database. I assign Source to the Text property of the DropDownlist and I assign SourceCode to the Value property of the same DropDownList. What I want to do is that when you select an item from the DropDownlist, I want take the Value for that Item and do some calculations and keep the selected item shown on the dropDownList. The problem is that..if the selected item has something eg 2 for the Value the application works fine, but if the Value property for that item in null the dropdown display the first item in the list. It should display the selected item..it only do this if there is something in the value property. Here is my code, that populates the dropdownlis. drpDepot.DataSource = ds; drpDepot.DataTextField = "SourceName"; drpDepot.DataValueField = "SourceCode"; drpDepot.DataBind(); drpDepot.Items.Insert(0, new ListItem(string.Empty, string.Empty)); Please help, I don’t know what is the problem. Please help. Thank you in advanced

    MP

    A 1 Reply Last reply
    0
    • K khuzwayom

      Hi All Please help, here us the scenario. I get Source and Source code from the database. I assign Source to the Text property of the DropDownlist and I assign SourceCode to the Value property of the same DropDownList. What I want to do is that when you select an item from the DropDownlist, I want take the Value for that Item and do some calculations and keep the selected item shown on the dropDownList. The problem is that..if the selected item has something eg 2 for the Value the application works fine, but if the Value property for that item in null the dropdown display the first item in the list. It should display the selected item..it only do this if there is something in the value property. Here is my code, that populates the dropdownlis. drpDepot.DataSource = ds; drpDepot.DataTextField = "SourceName"; drpDepot.DataValueField = "SourceCode"; drpDepot.DataBind(); drpDepot.Items.Insert(0, new ListItem(string.Empty, string.Empty)); Please help, I don’t know what is the problem. Please help. Thank you in advanced

      MP

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      khuzwayom wrote:

      What I want to do is that when you select an item from the DropDownlist, I want take the Value for that Item and do some calculations and keep the selected item shown on the dropDownList.

      Please put the code for this section.

      cheers, Abhijit CodeProject MVP

      K 1 Reply Last reply
      0
      • A Abhijit Jana

        khuzwayom wrote:

        What I want to do is that when you select an item from the DropDownlist, I want take the Value for that Item and do some calculations and keep the selected item shown on the dropDownList.

        Please put the code for this section.

        cheers, Abhijit CodeProject MVP

        K Offline
        K Offline
        khuzwayom
        wrote on last edited by
        #3

        Hi Thanx for the reply. Here is my code _kilometers = Convert.ToDouble(txtKM.Text); _value = drpDepot.SelectedValue.ToString().Trim(); if (_value != "") { _value4 = double.Parse(_value); _transportCost = _kilometers * _value4; lblMessage.Text = "Estimated Transport Cost is: £ " + _transportCost.ToString(); } I suppose after finishing executing it should keep the Item selected displayed...

        MP

        A 1 Reply Last reply
        0
        • K khuzwayom

          Hi Thanx for the reply. Here is my code _kilometers = Convert.ToDouble(txtKM.Text); _value = drpDepot.SelectedValue.ToString().Trim(); if (_value != "") { _value4 = double.Parse(_value); _transportCost = _kilometers * _value4; lblMessage.Text = "Estimated Transport Cost is: £ " + _transportCost.ToString(); } I suppose after finishing executing it should keep the Item selected displayed...

          MP

          A Offline
          A Offline
          Abhijit Jana
          wrote on last edited by
          #4

          khuzwayom wrote:

          _kilometers = Convert.ToDouble(txtKM.Text); _value = drpDepot.SelectedValue.ToString().Trim(); if (_value != "") { _value4 = double.Parse(_value); _transportCost = _kilometers * _value4; lblMessage.Text = "Estimated Transport Cost is: £ " + _transportCost.ToString(); }

          What ever you have to done in the else put.

          else
          {
          //
          }

          But still i am not able to understand where did you written the code means in which event you are loading the data and checking the value.

          cheers, Abhijit CodeProject MVP

          K 1 Reply Last reply
          0
          • A Abhijit Jana

            khuzwayom wrote:

            _kilometers = Convert.ToDouble(txtKM.Text); _value = drpDepot.SelectedValue.ToString().Trim(); if (_value != "") { _value4 = double.Parse(_value); _transportCost = _kilometers * _value4; lblMessage.Text = "Estimated Transport Cost is: £ " + _transportCost.ToString(); }

            What ever you have to done in the else put.

            else
            {
            //
            }

            But still i am not able to understand where did you written the code means in which event you are loading the data and checking the value.

            cheers, Abhijit CodeProject MVP

            K Offline
            K Offline
            khuzwayom
            wrote on last edited by
            #5

            Hi Abhijit Thanx for the assitance. I populate the Dropwdown in page_load if (_value != "") { _value4 = double.Parse(_value); _transportCost = _kilometers * _value4; lblMessage.Text = "Estimated Transport Cost is: £ " + _transportCost.ToString(); } else { lblMessage.Text = "The Source does not have a value" } If the execution got inside the if statement, it works fine, it does the calculation and keep the selected item displayed on the dropdown but if it went ti the ELSe block, it displays the message but the dropdown displays the fisrt item which i sin index 0.

            MP

            A 1 Reply Last reply
            0
            • K khuzwayom

              Hi Abhijit Thanx for the assitance. I populate the Dropwdown in page_load if (_value != "") { _value4 = double.Parse(_value); _transportCost = _kilometers * _value4; lblMessage.Text = "Estimated Transport Cost is: £ " + _transportCost.ToString(); } else { lblMessage.Text = "The Source does not have a value" } If the execution got inside the if statement, it works fine, it does the calculation and keep the selected item displayed on the dropdown but if it went ti the ELSe block, it displays the message but the dropdown displays the fisrt item which i sin index 0.

              MP

              A Offline
              A Offline
              Abhijit Jana
              wrote on last edited by
              #6

              Problem looks in Page_Load Did you checked the ISPostBack while Loading Data on dropdown list.

              if(!Page.IsPostBack)
              {
              //Load Dropdown data
              }

              cheers, Abhijit CodeProject MVP

              K 1 Reply Last reply
              0
              • A Abhijit Jana

                Problem looks in Page_Load Did you checked the ISPostBack while Loading Data on dropdown list.

                if(!Page.IsPostBack)
                {
                //Load Dropdown data
                }

                cheers, Abhijit CodeProject MVP

                K Offline
                K Offline
                khuzwayom
                wrote on last edited by
                #7

                Yes, I did. I think the problem is somewhere else not postback, because if th eproblem was postback it should behave the same way even when an item something in its Value property, but it only misbahes when the item has null/nothing in its Value property. it's C# if (!IsPostBack) { BindDepots(); }

                MP

                A 1 Reply Last reply
                0
                • K khuzwayom

                  Yes, I did. I think the problem is somewhere else not postback, because if th eproblem was postback it should behave the same way even when an item something in its Value property, but it only misbahes when the item has null/nothing in its Value property. it's C# if (!IsPostBack) { BindDepots(); }

                  MP

                  A Offline
                  A Offline
                  Abhijit Jana
                  wrote on last edited by
                  #8

                  Ya got it. :jig: . Problem is with If checking Try this

                  string _value = drpDepot.SelectedValue.ToString().Trim();
                  if (!string.IsNullOrEmpty(_value))
                  {
                  //Do what ever you want
                  }
                  else
                  {
                  //Do what ever you want
                  }

                  Enjoy ;)

                  cheers, Abhijit CodeProject MVP

                  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