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.SelectedV alue
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(Syst em.Configu ration.Con figuration Settings.A ppSettings ("MM_CONNE CTION_STRI NG_Conn_In tranet"))
' 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:DropDo wnList>
<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...
strString = dropdownmenuname.SelectedV
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(Syst
' 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: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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
so evenb if that page reloads from a post back those bindings are still there?
Yes, unless you have ViewState setup to False..
ASKER
Cool, thanks, that makes a little more sense now...
strString = dropdownmenuname.selectedi
Otherwise, bind it like this:
dropdownmenuname.DataSourc
dropdownmenuname.DataMembe
dropdownmenuname.DataTextF
dropdownmenuname.DataValue
dropdownmenuname.DataBind(