Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2005-04-12
Medium Priority
Last Modified: 2010-04-07
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()
            Dim DS As DataSet                  
                  DS = New DataSet()
                  lblStatus.Text = strSearch

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

            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...
Question by:Nugs
  • 3
  • 3
  • 2
LVL 18

Accepted Solution

tusharashah earned 2000 total points
ID: 13766209
Where are you binding your DropDownList..??

Make sure it's inside IsPostback False of Page_Load..

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
 If Not IsPostBack Then
   ' bind your DropDownList here
 End If
End Sub

LVL 13

Expert Comment

ID: 13766298
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"


Author Comment

ID: 13766852
Thanks tusharashah that worked... What does this actualy do i ahve seen it being used allot?!
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 13

Expert Comment

ID: 13766894
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.
LVL 18

Expert Comment

ID: 13766944
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..


Author Comment

ID: 13767073
so evenb if that page reloads from a post back those bindings are still there?
LVL 18

Expert Comment

ID: 13767096
Yes, unless you have ViewState setup to False..

Author Comment

ID: 13767150
Cool, thanks, that makes a little more sense now...

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question