2

I'm using Sitecore 7.2 and WFFM 2.4 (141008). Following this article https://divamatrix.wordpress.com/category/wffm/

I created a custom field type with 2 dropdown lists. The first list I'm populating on control load and the second using JSON web service depending on the first selection. Form is working very nice on load.

I have a problem in submit custom action related to this form. Problem is that when I submit a form first custom field (user control) on load event is fired and then the Result is selected. Since I'm populating lists in the load event result is always empty and no list item is selected. If I add !IsPOstBack list will be empty. Looks like controls view state is not enabled and controls are always empty. Other fields in the form are OK. Maybe somebody can show me what I did wrong:

/// <summary>
/// Custom WFFM field contains 2 linked lists: countries and dealers for selected country.  
/// </summary>
public partial class DealerInCountrySelector : ValidateUserControl, IHasTitle
{
    /// <summary>
    /// Collect dealer name and country, comma separated
    /// </summary>
    public override ControlResult Result
    {
        get
        {
            var countryName = ddlCountries.SelectedIndex > -1 ? ddlCountries.SelectedItem.Text : "";
            var dealerName = ddlDealers.SelectedIndex > -1 ? (ddlDealers.SelectedValue.Equals("-1") ? txtOther.Text : ddlDealers.SelectedItem.Text) : "";
            return new ControlResult(this.ControlName, string.Format("{0},{1}", countryName , dealerName), null);
        }
    }
    public string Title
    {
        get
        {
            return lbCountry.Text;
        }

        set
        {
            lbCountry.Text = value;
        }
    }
    protected override void OnInit(EventArgs e)
    {
        try
        {
            //Clear country selector list
            ddlCountries.Items.Clear();
            //Add default select item
            AddDefaultItem();
            //Populate country selector list
            foreach (var country in GlobalData.Dealers.DealersList.Keys.OrderBy(item => item.Name))
            {
                var storesList = GlobalData.Dealers.DealersList[country];
                if (storesList.Any())
                {
                    var item = new ListItem(country.Name, country.Code);
                    ddlCountries.Items.Add(item);
                }
            }
        }
        catch (Exception ex)
        {
            Log.Error("DealerInCountrySelector ERROR:" + ex.Message, ex, this);
        }
    }        
    /// <summary>
    /// Add Please select as default item
    /// </summary>
    void AddDefaultItem()
    {
        var defaultItem = new ListItem();
        defaultItem.Text = Translate.Text("Please select a country");
        defaultItem.Value = "";
        defaultItem.Selected = true;
        ddlCountries.Items.Add(defaultItem);
    }
    protected override Control ValidatorContainer
    {
        get
        {
            return this;
        }
    }
    protected override Control InnerValidatorContainer
    {
        get { return this.pSelector; }
    }        
}

enter image description here

1
  • do you have full code sample on how did you manage to have two dropdowns together and send the value? Thanks
    – Vik
    Commented Jan 29, 2019 at 14:38

1 Answer 1

1

Solution is to add hidden field which will be populated with second dropdown list selected value before submitting using js. In this case hidden field will keep selected value after form is reloaded and code for result is

public override ControlResult Result
{
    get
    {
        var countryName = ddlCountries.SelectedIndex > -1 ? ddlCountries.SelectedItem.Text : "";
        var dealerName = hfDealers.value; //hidden field to store selected dealer
        return new ControlResult(this.ControlName, string.Format("{0},{1}", countryName , dealerName), null);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.