Solved

.net dropdown lists

Posted on 2006-11-24
5
188 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
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
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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