Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.net dropdown lists

Posted on 2006-11-24
5
Medium Priority
?
202 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

671 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