By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

DropDownList Problem

P: n/a
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
Nov 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
How and when are you populating the items? In Page_Load, protected by a
Page.IsPostBack?
Defined in one the .aspx html page?

bill
"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
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

Nov 19 '05 #2

P: n/a
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #3

P: n/a
Hi Xarky,
As William pointed out, the problem lies in the Page.IsPostBack property..

Need help, post the code...

Happy Coding..
"Xarky" <be*********@yahoo.com> wrote in message
news:bc*************************@posting.google.co m...
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

Nov 19 '05 #4

P: n/a
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

"xarky d_best" <be*********@yahoo.com> wrote in message
news:ee**************@TK2MSFTNGP12.phx.gbl...
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

*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #5

P: n/a
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.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #6

P: n/a
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

"xarky d_best" <be*********@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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.

*** Sent via Developersdex http://www.developersdex.com ***

Nov 19 '05 #7

P: n/a
Thanks, problem solved

*** Sent via Developersdex http://www.developersdex.com ***
Nov 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.