Link to home
Start Free TrialLog in
Avatar of Nugs
Nugs

asked on

Value of drop down to string... Dropdownmenu selecting fist item in list no matter what!

i am filling a dropdownmenu from a database... I'm not using this dropdown to create a string in my code and and i can't seem to get the value of the selected item...

strString = dropdownmenuname.SelectedValue

I ran a test and printed the valkue of the string on the screan and the above seems to produce the value that i am looking for BUT it is selecting the first item in the DD no matter which on i actualy select... This may have something to do witht he page reloading but i neeed some help here...

This is my Sub:
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
      Sub btnSearch_Cat(sender as Object, e as EventArgs)
            Dim strSearch     As String
            Dim strSQLQuery   As String

            ' Get Search
            strSearch = ddCatagories.SelectedValue
            
            ' If there's nothing to search for then don't search
            ' o/w build our SQL Query and execute it.
            If Len(Trim(strSearch)) > 0 Then
                  ' Set up our connection.
                  Dim oleConn As New OleDb.OleDbConnection(System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_Conn_Intranet"))

                  ' Set up our SQL query text.
                  strSQLQuery = "SELECT * " _
                        & "FROM TBL_Records " _
                        & "WHERE Fld_Catagory LIKE '%" & Replace(strSearch, "'", "''") & "%' " _                        
                        & "ORDER BY Fld_Timestamp DESC;"
                        


                  ' Create new command object passing it our SQL query
                  ' and telling it which connection to use.
                  Dim Comm As OleDbCommand
                  Comm = New OleDbCommand(strSQLQuery, oleConn)

                  ' Get a DataSet to bind the DataGrid to
            Dim DA As New OleDbDataAdapter()
                  DA.selectCommand=Comm
                  
            Dim DS As DataSet                  
                  DS = New DataSet()
                  DA.Fill(DS)
                  lblStatus.Text = strSearch

                  lblDoSearch.visible = False
                  
                  ' DataBind DG to DS
                  Datalist1.DataSource = DS
                  Datalist1.DataBind()



                  OleConn.Close()
            Else
            End If
      End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------------

this is my menu:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
<asp:DropDownList CssClass="forms" ID="ddCatagories" runat="server" DataTextField="Fld_Items" DataValueField="Fld_id"></asp:DropDownList>

<asp:Button CssClass="forms" id="btnSearchCat" runat="server" Text    = "  Search Catagory  " OnClick = "btnSearch_Cat" AccessKey="1" ToolTip="Search Records"/>
----------------------------------------------------------------------------------------------------------------------------------------------------------------

I'm not sure what is going on here...
ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Assuming you already have your data bound use this:

strString = dropdownmenuname.selecteditem.value

Otherwise, bind it like this:

        dropdownmenuname.DataSource = dataset_name
        dropdownmenuname.DataMember = "table_name"
        dropdownmenuname.DataTextField = "item_text_column_name"
        dropdownmenuname.DataValueField = "item_value_column_name"

        dropdownmenuname.DataBind()
Avatar of Nugs
Nugs

ASKER

Thanks tusharashah that worked... What does this actualy do i ahve seen it being used allot?!
A postback is when the page gets refreshed.  So, if you have "if isnotpostback then ..." the code only executes the first time the page loads.
When ever You click any button/LinkButton on your Page this is how it actually get called on code:

Page_Load event ---> Then your Button_Click Event

So, whenever you click onany Button if your Binding code is not inside IsPostBack False then it will be called everytime.. so the values will be re-initialized to its original states before your Button_Click event..

Having your Binding code inside IsPostBack False will ensure that it get called only when the first time page Loads..

-tushar
Avatar of Nugs

ASKER

so evenb if that page reloads from a post back those bindings are still there?
Yes, unless you have ViewState setup to False..
Avatar of Nugs

ASKER

Cool, thanks, that makes a little more sense now...