Dropdownlist problem
-
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
-
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
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
-
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
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
-
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
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
-
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
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
-
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
Problem looks in
Page_Load
Did you checked theISPostBack
while Loading Data on dropdown list.if(!Page.IsPostBack)
{
//Load Dropdown data
}cheers, Abhijit CodeProject MVP
-
Problem looks in
Page_Load
Did you checked theISPostBack
while Loading Data on dropdown list.if(!Page.IsPostBack)
{
//Load Dropdown data
}cheers, Abhijit CodeProject MVP
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
-
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
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