Solved

.net dropdown lists

Posted on 2006-11-24
5
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

635 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