Page 1 of 1

Set value in dropdown list

Posted: Mon Aug 23, 2010 7:40 pm
by markc
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

Re: Set value in dropdown list

Posted: Mon Aug 23, 2010 11:36 pm
by s_ismail
On txtWidth_TextChanged navigate to each Item in Drop down List.The item which matches with user selected value set selected to true.

Re: Set value in dropdown list

Posted: Mon Aug 23, 2010 11:48 pm
by mazhar
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

Posted: Tue Aug 24, 2010 12:09 am
by markc
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

Re: Set value in dropdown list

Posted: Tue Aug 24, 2010 12:24 am
by markc
Hi All,

Thanks for the help.

This sorted the error.

inputControl.ClearSelection();



Cheers

Mark

Re: Set value in dropdown list

Posted: Thu Aug 26, 2010 11:06 pm
by markc
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

Re: Set value in dropdown list

Posted: Fri Aug 27, 2010 12:16 pm
by mazhar
Add AutoPostBack="true" to your dropdown list to make it do post backs upon selection changes.

Re: Set value in dropdown list

Posted: Sat Aug 28, 2010 6:44 am
by markc
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