1

I'm using Sitecore 7.2 and WFFM 2.4 (141008). Following this article https://divamatrix.wordpress.com/category/wffm/ I crated a custom field type with 2 dropdown lists. First list I'm populating on control load and second using JSON web service depending on 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 Result is selected. Since I'm populating lists in 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 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

  • do you have full code sample on how did you manage to have two dropdowns together and send the value? Thanks – Vik Jan 29 '19 at 14:38
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);
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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