Click here to Skip to main content
15,897,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a default page and a class.

I want to get the value of the dropdownlist from default page to my class.
how can I do this,
I'm new at OO programming
Posted
Comments
Sergey Alexandrovich Kryukov 13-Mar-14 2:04am    
Not clear. What page, what application type? what's the problem?
What have you tried so far?
—SA
JB0301 13-Mar-14 2:07am    
Nothing yet, I have a class I need to get the value of the dropdown list.
Sergey Alexandrovich Kryukov 13-Mar-14 2:14am    
And..?

1 solution

I supposed that this is about an ASP.NET application, so if is so the solution is the next one:
In your Page_Init() event you have to init your drop down list like in the next example:
C#
protected void Page_Init(object sender, EventArgs e)
        {
            if (CurrentUser.UserRole == UserRoles.Administrator)
                _editPopupControl.ShowCloseButton = true;
            //
            if (!Page.IsPostBack)
            {
                try
                {
                    //
                    // Init the center drop down list used as filter control
                    //
                    List<StoragePlan> itemsList = DataContext.StoragePlans.ToList<StoragePlan>();
                    Dictionary<string, int> dictionary = new Dictionary<string, int>();
                    foreach (StoragePlan storagePlan in itemsList)
                    {
                        if (!dictionary.ContainsValue(storagePlan.ID))
                            dictionary.Add(BaseListPage.GetEnumTextForValue(storagePlan.Type, typeof(StoragePlanTypes)), storagePlan.ID);
                    }
                    //
                    _storagePlanDropDownList.DataSource = dictionary;
                    _storagePlanDropDownList.DataValueField = "value";
                    _storagePlanDropDownList.DataTextField = "key";
                    _storagePlanDropDownList.DataBind();
                    //
                    _storagePlanDropDownList.Items.Insert(0, new ListItem(Resources.Resource.UserListPageNoStoragePlan, "-1"));
                    _storagePlanDropDownList.Items.Insert(0, new ListItem(Resources.Resource.UserListPageAllStoragePlan, "0"));
                    //
                    _storagePlanDropDownList.SelectedIndex = 0;
                }
                catch (Exception ex)
                {
                    BosEventLog.LogException(ex);
                    this.ErrorMessage = Resources.Resource.EntityLoadError;
                }
            }
        }


Then in could add a property in your class that return the selected value:
C#
public int StoragePlanID
        {
            get
            {
                int entityID = 0;
                if(_storagePlanDropDownList.SelectedItem != null)
                    int.TryParse(_storagePlanDropDownList.SelectedItem.Value, out entityID);
                //
                return entityID;
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900