DropDownList Problem

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
 
W

William F. Robertson, Jr.

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

bill
 
X

xarky d_best

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
 
V

Vishnu-Chivukula

Hi Xarky,
As William pointed out, the problem lies in the Page.IsPostBack property..

Need help, post the code...

Happy Coding..
 
W

William F. Robertson, Jr.

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
 
X

xarky d_best

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.
 
W

William F. Robertson, Jr.

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
 
Joined
Aug 30, 2006
Messages
7
Reaction score
0
I have a problem with dropdownlist that is SOMEWHAT similar with the above issue....

Here's my case.....

---I have a dropdownlist

<asp:dropdownlist id="ddlMediaFormat" runat="server" AutoPostBack="True"></asp:dropdownlist>

---I need to be able to select a mediaformattype in this dropdownlist...to populate my datagrid....this is my page_load


Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then
PopupateDropdownList()
End If


If ddlMediaFormat.Items.FindByText("MP3").Selected = True Then
PopulateMP3Datagrid()
ElseIf ddlMediaFormat.Items.FindByText("WMA").Selected = True Then
PopulateWMADatagrid()
End If

End Sub


-- And this is my PopupateDropdownList() code

Public Sub PopupateDropdownList()
'**** begin: populate the dropdownlist for media format *****
Dim ddDR As SqlDataReader = Nothing
Dim ddSqlConnection As SqlConnection = New SqlConnection("server=localhost;database=media;user id=uid;password=password;")
Dim ddSqlCommand As SqlCommand = New SqlCommand("SELECT MediaFormatID, MediaFormat FROM tblMediaFormat order by MediaFormatID ", ddSqlConnection)
ddSqlConnection.Open()
ddDR = ddSqlCommand.ExecuteReader(CommandBehavior.CloseConnection)

ddlMediaFormat.DataSource = ddDR
ddlMediaFormat.DataTextField = "MediaFormat"
ddlMediaFormat.DataValueField = "MediaFormatID"
ddlMediaFormat.DataBind()
ddlMediaFormat.Items.Insert(0, "Select")
'**** end: populate the dropdownlist for media format *****
End Sub


-- And my codes for PopulateMP3Datagrid() and PopulateWMADatagrid() is similar except that teh where clause is compared to 'WMA' for PopulateWMADatagrid() while 'MP3' for PopulateMP3Datagrid()


Public Sub PopulateMP3Datagrid()
Dim STR As String
STR = "Select MediaFormat, Media " & _
" from tblMedia " & _
" where MediaFormat = 'MP3'"

Dim cn As SqlConnection = New SqlConnection("server=localhost;database=media;user id=uid;password=password;")
Dim da As SqlDataAdapter = New SqlDataAdapter(STR, cn)

Dim ds As DataSet = New DataSet

da.Fill(ds, "tblMedia ")

dgDownload.DataSource = ds.Tables(0)
dgDownload.DataBind()
End Sub


-- I need to check if the selected text or value is equal to MP3 to invoke PopulateMP3Datagrid() or if it is WMA to invoke PopulateWMADatagrid()...This will determine the items i need to display in the datagrid...



----NOW, my problem is that everytime i run it...i get the value or the text 'SELECT' instead of MP3 or WMA...even if I had already selected either MP3 or WMA....

So what's wrong with my codes? Can anybody help me?

Thank you in advance
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top