Solved

.net dropdown lists

Posted on 2006-11-24
5
172 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
ID: 18008760
please note that this is in a user control
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18008923
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
ID: 18011306
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
ID: 18011309
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Installing IIS after Visual Studio 2015 Community Edition 5 46
New to SOAP and need some direction 10 45
Problem to error 4 44
Angular JS Route 3 50
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 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

22 Experts available now in Live!

Get 1:1 Help Now