Custom RadioButtonList on One Page Checkout
Posted: Mon Apr 06, 2009 2:48 pm
So I have a dynamic, custom RadioButtonList on the One Page Checkout. I need the customer to be able to select one of the options, and then when they click "Checkout" to complete their order I need to have the value of their selection inserted into a new table I created in the database.
So I have the RadioButtonList populating successfully, but when a customer selects one of the options the selection disappears when the customer clicks on a Payment Method. I noticed that several of the other values in the One Page Checkout are being saved using the LoadCustomViewState() and SaveCustomViewState() functions. So I tried adding my custom field information to the code inside those functions. I am now getting this error: "System.NullReferenceException: Object reference not set to an instance of an object." I have tried a bunch of different combinations without much success. I believe the issue lies with finding the actual control on the page and extracting that value. Here are the two functions listed above as I currently have them:
LoadCustomViewState()
SaveCustomViewState()
The reason I say that I believe the issue lies with finding the actual control on the page and extracting that value, is because in the SaveCustomViewState() function, when I change this line:
to this:
The code works fine. In other words, if I hard-code a "5" for the selected value of the radio button, everything works ok.
Hopefully I explained the issue well enough. If anyone needs more information please let me know and I'll do my best to provide it as clearly as I can. Thanks in advance.
So I have the RadioButtonList populating successfully, but when a customer selects one of the options the selection disappears when the customer clicks on a Payment Method. I noticed that several of the other values in the One Page Checkout are being saved using the LoadCustomViewState() and SaveCustomViewState() functions. So I tried adding my custom field information to the code inside those functions. I am now getting this error: "System.NullReferenceException: Object reference not set to an instance of an object." I have tried a bunch of different combinations without much success. I believe the issue lies with finding the actual control on the page and extracting that value. Here are the two functions listed above as I currently have them:
LoadCustomViewState()
Code: Select all
private void LoadCustomViewState()
{
User user = Token.Instance.User;
_CurrentBasketHash = user.Basket.GetContentHash(OrderItemType.Product);
if (Page.IsPostBack)
{
UrlEncodedDictionary customViewState = new UrlEncodedDictionary(EncryptionHelper.DecryptAES(Request.Form[VS_CustomState.UniqueID]));
_EditBillingAddress = (AlwaysConvert.ToInt(customViewState.TryGetValue("EditBillingAddress")) == 1);
_EditShippingAddress = (AlwaysConvert.ToInt(customViewState.TryGetValue("EditShippingAddress")) == 1);
_ShipToAddressId = AlwaysConvert.ToInt(customViewState.TryGetValue("ShipToAddressId"));
_LastBillToCountry = customViewState.TryGetValue("LastBillToCountry");
_BillToProvinceError = AlwaysConvert.ToBool(customViewState.TryGetValue("BillToProvinceError"), false);
_LastShipToCountry = customViewState.TryGetValue("LastShipToCountry");
_ShipToProvinceError = AlwaysConvert.ToBool(customViewState.TryGetValue("ShipToProvinceError"), false);
_ShowBillToProvinceList = AlwaysConvert.ToBool(customViewState.TryGetValue("ShowBillToProvinceList"), false);
_ShowShipToProvinceList = AlwaysConvert.ToBool(customViewState.TryGetValue("ShowShipToProvinceList"), false);
CharityList.SelectedValue = customViewState.TryGetValue("CharitySelection");
string savedBasketHash = customViewState.TryGetValue("SavedBasketHash");
if (savedBasketHash == _CurrentBasketHash)
{
string savedShipRates = customViewState.TryGetValue("SavedShipRates");
ParseSavedShipRates(savedShipRates);
}
_SavedBasketHash = savedBasketHash;
}
if (string.IsNullOrEmpty(_LastBillToCountry)) _LastBillToCountry = user.PrimaryAddress.CountryCode;
if (string.IsNullOrEmpty(_LastShipToCountry)) _LastShipToCountry = GetDefaultShipToCountry(user);
}
Code: Select all
private void SaveCustomViewState()
{
RadioButtonList selected = (RadioButtonList)PageHelper.RecursiveFindControl(Page, "CharityList");
UrlEncodedDictionary customViewState = new UrlEncodedDictionary();
customViewState.Add("EditBillingAddress", (_EditBillingAddress ? "1" : "0"));
customViewState.Add("EditShippingAddress", (_EditShippingAddress ? "1" : "0"));
customViewState.Add("ShipToAddressId", _ShipToAddressId.ToString());
customViewState.Add("LastBillToCountry", BillToCountry.SelectedValue);
customViewState.Add("BillToProvinceError", _BillToProvinceError.ToString());
customViewState.Add("LastShipToCountry", ShipToCountry.SelectedValue);
customViewState.Add("ShipToProvinceError", _ShipToProvinceError.ToString());
customViewState.Add("SavedBasketHash", _CurrentBasketHash);
customViewState.Add("SavedShipRates", EncodeSavedShipRates());
customViewState.Add("ShowBillToProvinceList", _ShowBillToProvinceList.ToString());
customViewState.Add("ShowShipToProvinceList", _ShowShipToProvinceList.ToString());
customViewState.Add("CharitySelection", selected.SelectedItem.Value);
VS_CustomState.Value = EncryptionHelper.EncryptAES(customViewState.ToString());
}
Code: Select all
customViewState.Add("CharitySelection", selected.SelectedItem.Value);
Code: Select all
customViewState.Add("CharitySelection", "5");
Hopefully I explained the issue well enough. If anyone needs more information please let me know and I'll do my best to provide it as clearly as I can. Thanks in advance.