Set value in dropdown list
Set value in dropdown list
Hi,
I have created an option “Size” for my product which generates a dropdown list which is hidden within a PlaceHolder control on the product page.
I have created a custom TextBox on the product page when the user enters a value for example 480 on TextChanged the txtWidth_TextChanged is fired and
it loops round the ProductOptionCollection productOptions = product.ProductOptions collection and returns the value that is nearest it in the collection for example 500.
I then want to set the value in the size dropdownlist to this value any ideas how to do this?
On the TextChanged the BuyProductDialog.Page_Init is run first which calls ProductHelper.BuildProductOptons which populates the dropdown list.
Then txtWidth_TextChanged is run which Calculates the value.
Thanks
Mark
I have created an option “Size” for my product which generates a dropdown list which is hidden within a PlaceHolder control on the product page.
I have created a custom TextBox on the product page when the user enters a value for example 480 on TextChanged the txtWidth_TextChanged is fired and
it loops round the ProductOptionCollection productOptions = product.ProductOptions collection and returns the value that is nearest it in the collection for example 500.
I then want to set the value in the size dropdownlist to this value any ideas how to do this?
On the TextChanged the BuyProductDialog.Page_Init is run first which calls ProductHelper.BuildProductOptons which populates the dropdown list.
Then txtWidth_TextChanged is run which Calculates the value.
Thanks
Mark
Re: Set value in dropdown list
On txtWidth_TextChanged navigate to each Item in Drop down List.The item which matches with user selected value set selected to true.
hope this helps!
__________________
s_ismail

AbleCommerce Customization
Free Plugins and Add-Ons
AbleCommerce Plugins and Add-Ons
Plugables Blog
__________________
s_ismail


AbleCommerce Customization
Free Plugins and Add-Ons
AbleCommerce Plugins and Add-Ons
Plugables Blog
Re: Set value in dropdown list
Well if dropdown is already populated with size options and now you want to select the value that is nearest to the typed. Then you can try something like this in your code
Code: Select all
ListItem item = dropdownlist.Items.FindByText(sizevalue);
if (item != null)
item.Selected = true;
Re: Set value in dropdown list
Hi,
Here is the code
protected void txtSize_TextChanged(object sender, EventArgs e)
{
int sizeValue = CalculateMeasurements(_Product, txtSize.Text);
DropDownList inputControl = (DropDownList)PageHelper.RecursiveFindControl(this, "option0");
//Hardcoded to test
ListItem item = inputControl.Items.FindByText("600");
if (item != null)
item.Selected = true;
}
It runs through the method and item.Selected is set to true.
A error is returned "Cannot have multiple items selected in a DropDownList."
Thanks
Mark
Here is the code
protected void txtSize_TextChanged(object sender, EventArgs e)
{
int sizeValue = CalculateMeasurements(_Product, txtSize.Text);
DropDownList inputControl = (DropDownList)PageHelper.RecursiveFindControl(this, "option0");
//Hardcoded to test
ListItem item = inputControl.Items.FindByText("600");
if (item != null)
item.Selected = true;
}
It runs through the method and item.Selected is set to true.
A error is returned "Cannot have multiple items selected in a DropDownList."
Thanks
Mark
Re: Set value in dropdown list
Hi All,
Thanks for the help.
This sorted the error.
inputControl.ClearSelection();
Cheers
Mark
Thanks for the help.
This sorted the error.
inputControl.ClearSelection();
Cheers
Mark
Re: Set value in dropdown list
Hi,
When I select the value in the dropdown list the page is not refresh so my price based on the size in the options is not changed.
Anyone know how to refresh the page?
Thanks
Mark
When I select the value in the dropdown list the page is not refresh so my price based on the size in the options is not changed.
Anyone know how to refresh the page?
Thanks
Mark
Re: Set value in dropdown list
Add AutoPostBack="true" to your dropdown list to make it do post backs upon selection changes.
Re: Set value in dropdown list
Hi Mazhar,
The AutoPostBack=”true” has been set up in the ProductHelper.cs
// CREATE A DROPDOWN FOR THE OPTIONS
DropDownList aspOptions = new DropDownList();
RequiredFieldValidator aspOptionsValidator = new RequiredFieldValidator();
aspOptions.ID = "option" + i;
spOptions.AutoPostBack = true;
When the txtWidth_TextChanged is fired there is a postback.
But when the code below is run there is no PostBack but the dropdownlist text is select.
DropDownList inputControl = (DropDownList)PageHelper.RecursiveFindControl(this, "option1");
inputControl.ClearSelection();
ListItem item = dropdownlist.Items.FindByText(sizevalue);
if (item != null)
item.Selected = true;
Any ideas how to run a postback so that the price is refresh the dropdownlist has the correct value.
Or is there an other way to refresh the price.
If I add the product to the basket it has the correct value even although the product has no value set.
Thanks
Mark
The AutoPostBack=”true” has been set up in the ProductHelper.cs
// CREATE A DROPDOWN FOR THE OPTIONS
DropDownList aspOptions = new DropDownList();
RequiredFieldValidator aspOptionsValidator = new RequiredFieldValidator();
aspOptions.ID = "option" + i;
spOptions.AutoPostBack = true;
When the txtWidth_TextChanged is fired there is a postback.
But when the code below is run there is no PostBack but the dropdownlist text is select.
DropDownList inputControl = (DropDownList)PageHelper.RecursiveFindControl(this, "option1");
inputControl.ClearSelection();
ListItem item = dropdownlist.Items.FindByText(sizevalue);
if (item != null)
item.Selected = true;
Any ideas how to run a postback so that the price is refresh the dropdownlist has the correct value.
Or is there an other way to refresh the price.
If I add the product to the basket it has the correct value even although the product has no value set.
Thanks
Mark