DropDownList Problem

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,
I am using a DropDownList and populating with items. One of the items
is set as blank("").

I need to check that the items selected from this DropDownList is not
that value but another one.

I am using the following code:
string result =DropDownList.SelectedItem.Text;

The value of the result is always being an empty string(""), even when
I select an item that has text in it.

Can someone help me out.
Thanks in Advance
 
How and when are you populating the items? In Page_Load, protected by a
Page.IsPostBack?
Defined in one the .aspx html page?

bill
 
I'm populating it during the Page_Load method.

Items I add are being all shown ind dropdownList, but as reported an
empty string is being reported in SelectedItem.Text
 
Hi Xarky,
As William pointed out, the problem lies in the Page.IsPostBack property..

Need help, post the code...

Happy Coding..
 
Does your page load look something like this.

private Page_Load( ... )
{
if ( Page.IsPostBack == false )
LoadDDLItems();
}

or
private Page_Load( ... )
{
LoadDDLItems();
}

If your looks like the second approach, every postback you are dumping what
the user selected and recreated all the items. During your LoadDDLItems
method, you probably also are adding the blank record at position 1, and
might even be setting the index.

Make sure you are only binding the data to your DropDownList when the
IsPostBack property of the Page is false.

Post your code if you need more help.

bill
 
Hi,
What code exactly do I need to post. Is this enough?

<asp:DropDownList id="dropDownList" style="Z-INDEX: 109; LEFT: 168px;
POSITION: absolute; TOP: 112px" tabIndex="4" runat="server"
Height="24px" Width="184px" Font-Size="Medium"></asp:DropDownList>

*** in Page_Load() ****
dropDownList.Items.Clear();
dropDownList.Items.Add("");
foreach(string type in schoolTypes)
{
dropDownList.Items.Add(type);
}

schoolTypes is an arrayList, where its data is being retrieved from a
database.
 
You need to place you .Clear(), .Add() code inside a if statement

page_load

if ( Page.IsPostBack == false )
{
dropDownList.Items.Clear();
dropDownList.Items.Add("");
foreach(string type in schoolTypes)
{
dropDownList.Items.Add(type);
}
}

HTH,

bill
 
Back
Top