S
sling blade
Hi,
I have a dropdownlist in my DataList control and I am trying to set the
SelectedItem property of the dropdownlist so when the datalist displays
the user will see the text in the dropdownlist set to a predetermined
value.
I can set the SelectedItem property easy enough and get the
dropdownlist to work fine just so long as the dropdownlist is just on
the form itself not in a DataList control.
For some reason when its in a DataList control the dropdownlist will
show the first item in the list and not the one selected.
I am setting the selectedItem property from the datalist's ItemCreated
command.
Here is the code from the item created event:
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
Dim ddlCategory As DropDownList
Try
ddlCategory = CType(e.Item.FindControl("ddlCategory"),
DropDownList)
Dim myDataSet As New DataSet
Dim strSQL As String = "SELECT CategoryID, CategoryName
FROM Category"
Dim myAdapter As New OleDbDataAdapter(strSQL,
c.Connection)
myAdapter.Fill(myDataSet)
ddlCategory.DataSource = myDataSet
ddlCategory.DataTextField = "CategoryName"
ddlCategory.DataValueField = "CategoryID"
ddlCategory.SelectedValue = myvalue
ddlCategory.DataBind()
Catch ex As Exception
Response.Write(ex.Message.ToString)
End Try
End If
Right after this code has ran I have checked the SelectedValue text and
it is set to myvalue eg.
dim strTest as string = ddlCategory.SelectedValue.ToString
strTest does equal myvalue, however when the grid displays it is always
set to the first value generated from the dataset and not the
selectedvalue.
Any ideas on how to set the SelectedValue from in a DataList?
Thanks
I have a dropdownlist in my DataList control and I am trying to set the
SelectedItem property of the dropdownlist so when the datalist displays
the user will see the text in the dropdownlist set to a predetermined
value.
I can set the SelectedItem property easy enough and get the
dropdownlist to work fine just so long as the dropdownlist is just on
the form itself not in a DataList control.
For some reason when its in a DataList control the dropdownlist will
show the first item in the list and not the one selected.
I am setting the selectedItem property from the datalist's ItemCreated
command.
Here is the code from the item created event:
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem Then
Dim ddlCategory As DropDownList
Try
ddlCategory = CType(e.Item.FindControl("ddlCategory"),
DropDownList)
Dim myDataSet As New DataSet
Dim strSQL As String = "SELECT CategoryID, CategoryName
FROM Category"
Dim myAdapter As New OleDbDataAdapter(strSQL,
c.Connection)
myAdapter.Fill(myDataSet)
ddlCategory.DataSource = myDataSet
ddlCategory.DataTextField = "CategoryName"
ddlCategory.DataValueField = "CategoryID"
ddlCategory.SelectedValue = myvalue
ddlCategory.DataBind()
Catch ex As Exception
Response.Write(ex.Message.ToString)
End Try
End If
Right after this code has ran I have checked the SelectedValue text and
it is set to myvalue eg.
dim strTest as string = ddlCategory.SelectedValue.ToString
strTest does equal myvalue, however when the grid displays it is always
set to the first value generated from the dataset and not the
selectedvalue.
Any ideas on how to set the SelectedValue from in a DataList?
Thanks