Solved

.net dropdown lists

Posted on 2006-11-24
5
162 Views
Last Modified: 2010-04-16
Hi

I am having a problem with dropdownlists in .net 2.0. I bind the dropdownlist like this

    protected void Page_Load(object sender, EventArgs e)
    {
            this.LoadDivisions();
    }

    private void LoadDivisions()
    {
        Valiant.API.Interfaces.IDivision Division = new Valiant.API.Elements.Division();
        DataSet objDataSet = Division.GetDivisions();
        foreach (DataRow objRow in objDataSet.Tables[0].Rows)
        {
            ListItem objItem = new ListItem(objRow["DivisionName"].ToString(), objRow["DivisionID"].ToString());
            this.DropDownList1.Items.Add(objItem);
        }
    }


now on the button submit I would want to get the value.

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
          int.Parse(this.DropDownList1.SelectedItem.Value);
   }

the selected item, is always null
0
Comment
Question by:Caroline_Swart
  • 2
5 Comments
 

Author Comment

by:Caroline_Swart
Comment Utility
please note that this is in a user control
0
 
LVL 44

Expert Comment

by:Arthur_Wood
Comment Utility
why don't you DataBind to the control, like this:

    private void LoadDivisions()
    {
        Valiant.API.Interfaces.IDivision Division = new Valiant.API.Elements.Division();
        DataSet objDataSet = Division.GetDivisions();
        this.DropDownList1.DataSource = objDataSet;
        this.DropDownList1.DataBind();
    }
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 500 total points
Comment Utility
it is because when you click the button you post back and then refill the drop down list thus erasing the selected item. to correct simply have this

    protected void Page_Load(object sender, EventArgs e)
    {
if(!IsPostBack)
{
            this.LoadDivisions();
}
    }
0
 
LVL 20

Expert Comment

by:REA_ANDREW
Comment Utility
this way the selected item is persisted through post back and there fore unless null to begin with will have a value for the selectedItem

:-)

Andrew
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now