hi ,
need your help please
I have got a dropdownlist which is getting it's items and values from the table with in SQL server , everything works fine.
I just want to be able to select default item , so when the page is loaded the dropdown list default will be that selected item.
as an exmple if the drop downlist items and values are as follow
item --> value
-------
city --> 1
tokyo --> 2
london -->3
these info is imported from the database
and I want by default tokyo to be selected
many thanks
M
------------
here is my code:
Dim sqlConnection As SqlConnection
Dim sqlDataAdapter As SqlDataAdapter
Dim sqlCommand As SqlCommand
Dim dataSet As DataSet
Dim dataTable As DataTable sqlConnection = New SqlConnection("user id=sa;password=testfirm;database=ken_live;server=csfirm03")
'pass the stored proc name and SqlConnection
sqlCommand = New SqlCommand("Select * From _aci",sqlConnection)
'instantiate SqlAdapter and DataSet
sqlDataAdapter = New SqlDataAdapter(sqlCommand)
dataSet = New DataSet()
'populate the DataSet
sqlDataAdapter.Fill(dataSet, "AA")
'apply sort to the DefaultView to sort by CompanyName
dataSet.Tables(0).DefaultView.Sort = "bsheet"
city.DataSource = dataSet.Tables("AA").DefaultView
city.DataTextField ="bsheet" ' what to display
city.DataValueField ="bsheet" ' what to set as value
city.DataBind()
Dim li as ListItemFOR EACH li in city.Items
IF li.value = 2 THEN
li.selected = true
END IF
NEXT
|||I would also advise using a DataReader object rather than a DataSet and DataAdapter. The DataReader is much faster and does not use nearly as much resources. Also be sure to clean up after you use the objects, for example:
city.DataBind()sqlCommand.Dispose
sqlCommand = Nohting
sqlDataAdapter.Dispose
sqlDataAdapter = Nothing
dataSet.Dispose
dataSet = Nothing
sqlConnection.Close
sqlConnection = Nothing
HTH|||------ Edited by Colt ------
<code/> tag was added
------ Edited by Colt ------
thanks for the reply
I've changed the code, ( no error messages ) but no effect almost
Dim sqlConnection As SqlConnection
Dim sqlDataAdapter As SqlDataAdapter
Dim sqlCommand As SqlCommand
Dim dataSet As DataSet
Dim dataTable As DataTable
Dim li as listitem sqlConnection = New SqlConnection("user id=sa;password=testfirm;database=ken_live;server=csfirm03")
'pass the stored proc name and SqlConnection
sqlCommand = New SqlCommand("Select * From _aci",sqlConnection)
'instantiate SqlAdapter and DataSet
sqlDataAdapter = New SqlDataAdapter(sqlCommand)
dataSet = New DataSet()
'populate the DataSet
sqlDataAdapter.Fill(dataSet, "AA")
'apply sort to the DefaultView to sort by CompanyName
dataSet.Tables(0).DefaultView.Sort = "bsheet"
city.DataSource = dataSet.Tables("AA").DefaultView
city.DataTextField ="bsheet" ' what to display
city.DataValueField ="bsheet" ' what to set as value
for each li in city.items
if li.value="CITY" then
li.selected=true
end if
next
city.DataBind()
|||The DataBind() should preceed the For Loop that is selecting the default value.
city.DataBind()for each li in city.items
if li.value="CITY" then
li.selected=true
end if
next
|||thanks again for following the case
but still the porblem does exist ( no error messages but no effect as well )
code:
session("A")="0"
'object vars
Dim sqlConnection As SqlConnection
Dim sqlDataAdapter As SqlDataAdapter
Dim sqlCommand As SqlCommand
Dim dataSet As DataSet
Dim dataTable As DataTable
Dim li as listitem
sqlConnection = New SqlConnection("user id=sa;password=testfirm;database=ken_live;server=csfirm03")
'pass the stored proc name and SqlConnection
sqlCommand = New SqlCommand("Select * From _aci",sqlConnection)
'instantiate SqlAdapter and DataSet
sqlDataAdapter = New SqlDataAdapter(sqlCommand)
dataSet = New DataSet()
'populate the DataSet
sqlDataAdapter.Fill(dataSet, "AA")
'apply sort to the DefaultView to sort by CompanyName
'dataSet.Tables(0).DefaultView.Sort = "bsheet"
city.DataSource = dataSet.Tables("AA").DefaultView
city.DataTextField ="bsheet" ' what to display
city.DataValueField ="bsheet" ' what to set as value
city.DataBind()
for each li in city.items
if li.value="CITY" then
li.selected=true
end if
next|||As you had presented earlier, the Value property of the DDL was populated with a numeric value and the Text property was propulated with the character description (i.e., CITY).
li.value = 2
or you can use
li.text = "CITY"
HTH|||hi again :(
it didn't work.
always it is coming up wit it's own default one ( which is the fist row of data in database)
please , please , any alternative way
again I'm no getting any error messages
many thanks|||Please post your current code.|||
public Sub page_load(sender As Object, e As EventArgs) If Not Page.IsPostback Then
session("A")="0"
'object vars
Dim sqlConnection As SqlConnection
Dim sqlDataAdapter As SqlDataAdapter
Dim sqlCommand As SqlCommand
Dim dataSet As DataSet
Dim dataTable As DataTable
sqlConnection = New SqlConnection("user id=sa;password=testfirm;database=ken_live;server=csfirm03")
'pass the stored proc name and SqlConnection
sqlCommand = New SqlCommand("Select * From _aci",sqlConnection)
'instantiate SqlAdapter and DataSet
sqlDataAdapter = New SqlDataAdapter(sqlCommand)
dataSet = New DataSet()
'populate the DataSet
sqlDataAdapter.Fill(dataSet, "AA")
'apply sort to the DefaultView to sort by CompanyName
'dataSet.Tables(0).DefaultView.Sort = "bsheet"
city.DataSource = dataSet.Tables("AA").DefaultView
city.DataTextField ="bsheet" ' what to display
city.DataValueField ="bsheet" ' what to set as value
city.DataBind()
sqlCommand = New SqlCommand("select distinct datepart(yy,dateincur) as tempdate from ken_non_matter where nonmat in (935,1035,1040) order by datepart(yy,dateincur) desc",sqlConnection)
sqlDataAdapter = New SqlDataAdapter(sqlCommand)
dataSet = New DataSet()
'populate the DataSet
sqlDataAdapter.Fill(dataSet, "tempdate")
'apply sort to the DefaultView to sort by CompanyName
' dataSet.Tables(0).DefaultView.Sort = "tempdate"
year.DataSource = dataSet.Tables("tempdate").DefaultView
year.DataTextField ="tempdate" ' what to display
year.DataValueField ="tempdate" ' what to set as value
year.DataBind()
End if
for each li in city.items
if li.value="CITY" then
li.selected=true
end if
next
End Sub
|||As you had presented earlier, the Value property of the DDL was populated with a numeric value and the Text property was propulated with the character description (i.e., CITY).
li.value = 2
or you can use
li.text = "CITY"
You are using li.value="CITY" (this is wrong according to your previous posts)
USE li.value = 2
HTH|||You shouldn't need to loop through the liost collection, either.
After you databind (and you should always bind the list before you attempt to set a default value), simply do the following:
city.Items.FindByValue("YOUR VALUE OR FIELDNAME GOES HERE").selected = true;
You can also use FindByText, if you prefer (city.Items.FindByText("text").selected = true)
If the ddl might have already contained a selected item, be sure to use ClearSelection first, or you'll throw an error...|||this is going arround
I manage to find where I'm going wrong but don't know how to fix it
for each li in city.items
If (li.text = "CITY ") then
li.selected=true
response.write(li.value) ' just Entered it as a test ! which doesn't print at all
End if
next
It runs the FOR loop fine but doesn't enter the IF statment.
just for test if I add
for each li in city.items
response.write("-"+li.value+"-")
if (li.text = "CITY ") then
li.selected=true
response.write(li.value) ' just Entered it as a test ! which doesn't print at all
End if
next
it will print
-AFSC --CITY --HKONG --INDIA --NZ -
whitch there is a space after the string therefore , I add a space after the 'city ' in my code
for each li in city.items
response.write("-"+li.value+"-")
if (li.text = "CITY ") then
li.selected=true
response.write(li.value) ' just Entered it as a test ! which doesn't print at all
End if
next
but still it is not entering the IF section
any ideas|||You're writing too much code. Try the method I posted above...|||it is small code but I've explained it,
it doesn't run