DropDownList Problem

Discussion in 'ASP .Net' started by Xarky, Apr 5, 2005.

  1. Xarky

    Xarky Guest

    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
    Xarky, Apr 5, 2005
    #1
    1. Advertising

  2. 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" <> wrote in message
    news:...
    > 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
    William F. Robertson, Jr., Apr 5, 2005
    #2
    1. Advertising

  3. Xarky

    xarky d_best Guest

    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 ***
    xarky d_best, Apr 5, 2005
    #3
  4. Hi Xarky,
    As William pointed out, the problem lies in the Page.IsPostBack property..

    Need help, post the code...

    Happy Coding..


    "Xarky" <> wrote in message
    news:...
    > 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
    Vishnu-Chivukula, Apr 5, 2005
    #4
  5. 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" <> wrote in message
    news:...
    > 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 ***
    William F. Robertson, Jr., Apr 5, 2005
    #5
  6. Xarky

    xarky d_best Guest

    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 ***
    xarky d_best, Apr 5, 2005
    #6
  7. 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" <> wrote in message
    news:%...
    > 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 ***
    William F. Robertson, Jr., Apr 5, 2005
    #7
  8. Xarky

    xarky d_best Guest

    xarky d_best, Apr 5, 2005
    #8
  9. Xarky

    Byock

    Joined:
    Aug 30, 2006
    Messages:
    7
    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
    Byock, Aug 30, 2006
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mr2_93
    Replies:
    1
    Views:
    3,593
    mr2_93
    Oct 2, 2005
  2. tshad
    Replies:
    8
    Views:
    2,221
    tshad
    Oct 19, 2005
  3. =?Utf-8?B?SnVhbmpv?=

    GridView: Filtr DropDownList from another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 23, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,860
    =?Utf-8?B?SnVhbmpv?=
    Dec 23, 2005
  4. =?Utf-8?B?SnVhbmpv?=

    GridView: Filter DropDownList for another DropDownList

    =?Utf-8?B?SnVhbmpv?=, Dec 29, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    1,293
    =?Utf-8?B?SnVhbmpv?=
    Dec 29, 2005
  5. keithb
    Replies:
    1
    Views:
    11,950
    shauns1
    Nov 1, 2006
Loading...

Share This Page