ASP.Net Custom Control

190 views Asked by At

I am developing a custom control that needs to display a dropdownlist as a composite control.

The drop down list gets populated from a Rest web service. The problem I am facing is that the dropdownlist only has DataTextField and DataValueField but I need a way of storing more values in the control i.e. I have a couple of other properties I need to access for the selected item.

What is the best way of going about this?

Here is the code I have so far:

    [ValidationProperty("SelectedValue")]
    public class SelectSurveyControl : Panel
    {
        private DropDownList ddlSurveys;

        public string SelectedSurveyId 
        { 
            get 
            { 
                return SelectedValue;  
            } 
        }

        public string SelectedSurveyJavascriptEmbedCode
        {
            get
            {
                return this.ddlSurveys.SelectedItem.Attributes[""];
            }
        }

        public string SelectedValue
        {
            get
            {
                return ddlSurveys.SelectedValue;
            }
            set
            {
                if (ddlSurveys == null)
                {
                    ddlSurveys = new DropDownList();
                }

                ddlSurveys.SelectedValue = value;
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnInit(e);

            if (ddlSurveys == null)
            {
                ddlSurveys = new DropDownList();
            }

            IList<Survey> surveys = GetSurveys();

            this.ddlSurveys.DataSource = surveys;
            this.ddlSurveys.DataTextField = "title";
            this.ddlSurveys.DataValueField = "id";

            this.ddlSurveys.DataBind();

            ddlSurveys.SelectedValue = this.SelectedValue;
            ddlSurveys.CssClass = "umbEditorTextFieldMultiple charlimit";
            ddlSurveys.Attributes.Add("SurveyId", SelectedSurveyId);
            ddlSurveys.Attributes.Add("JavascriptEmbedingCode", SelectedSurveyId);

            this.Controls.Add(ddlSurveys);            
        }

        public IList<Survey> GetSurveys()
        {
            ...
        }
}
2

There are 2 answers

0
Derrick On

You could use a hidden field and iterate thru a copy of the returned Surveys like this:

foreach(Survey s in Surveys){
string val = s.id + ":" + s.<property1> + ":" + s.<property2>;
hiddenField.Value += val +",";
}

When you need to read from the hidden field, you use String.Split to separate the values into arrays using ',' as the separator and in each array, you split again using ':'.

In the first split Array1[0] who be the survey id and Array1[n!=0] would be the properties of the Survey with the id = Array1[0]. Array[n!=0] would then be split into Array2.

I would suggest handling empty property values with an empty string or something or else you might end up with unequal lengths especially if you specify StringSplitOptions.RemoveEmptyEntries.

Agricfowl

0
Peter On

Try using a string join/split to store and retrieve the various values, then you don't have to customize your dropdown list very much.

For Example:

Text: Some Title

Value: 1|testing test|2/12/2010

This will let you store as many values as you want, so long as you choose an appropriate character to join and split on. I usually use the bar, as in my example above.

Side Note: I was looking at your selected value set handler and it needs some tweaking. You shouldn't check for a null drop down list, instead you should call EnsureChildControls() before each get and set instead. Make sure you override the CreateChildControls() method and create your controls there.