469,648 Members | 1,410 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,648 developers. It's quick & easy.

dropdownlist.selectedItem not working from in a datalist

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

Nov 19 '05 #1
3 2539
Check page load event.
If Page.IsPostBack property is true, then you shouldn't re-bind and
initialize your database stuff.

Burak SARICA

"sling blade" <co*************@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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

Nov 19 '05 #2
I have a "If Not IsPostBack is True Then BindGrid" routine in my Page
Load event for the DataList.

Once the DataList binds then the ItemCreated event fires and I create a
oledbcommand object and use it to bind stuff to the dropdownlist.

Does this sound like the right procedure?

Nov 19 '05 #3
Not just wrong. But if your dropdownlists doesn't change row by row, it
creates useless traffic and cpu time. So fill a global dataTable for the
dropdowns, and only bind them in itemcreate event.

"sling blade" <co*************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
I have a "If Not IsPostBack is True Then BindGrid" routine in my Page
Load event for the DataList.

Once the DataList binds then the ItemCreated event fires and I create a
oledbcommand object and use it to bind stuff to the dropdownlist.

Does this sound like the right procedure?

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Ivan | last post: by
reply views Thread by Ashish Sharma | last post: by
2 posts views Thread by Shiju Poyilil | last post: by
1 post views Thread by B. Sockel | last post: by
2 posts views Thread by HH | last post: by
2 posts views Thread by jeffmagill | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.