DropDownList: Select item based on record? (newbie)

G

Guest

Hi,

I am sure this is a common question, but I could not find an answer...

I have a DataList tag in which the EditItemTemplate has an item that should
only set the database field to one of a few options.

Seemingly a Drop Down List is the way to go.

However, when the user selects a record to edit, how can I "pre select" the
item in the dropDownList to reflect what is currently in the database?

See code below.

Any help or sample code in VB would be great.

Thanks,

David

--------------------------------
My code is in VB.net and looks like this:

<asp:DataList>

<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "datafield") %>
</ItemTemplate>

<EditItemTemplate>
<asp:DropDownList>
<asp:ListItem value="opt1">opt1</asp:ListItem>
<asp:ListItem value="opt2">opt2</asp:ListItem>
<asp:ListItem value="opt3">opt3</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>

</asp:DataList>
 
G

Guest

David,

To programmatically select a listitem in a dropdownlist, do something like
the following:

dropDownList1.Items.FindByValue("somevalue").Selected = true;

You can also substitute FindByValue for FindByText.

Hope this helps!

Thanks,
Ian Suttle
http://www.IanSuttle.com
 
G

Guest

Hi,

Thanks for the help!

Where would be the correct spot to put this code?

Thanks again,
David
 
G

Guest

Hi,
Where would be the correct spot to put this code?

I tried this, but it did not work:

Sub dropDown1_onLoad(sender As Object, e As EventArgs)

sender.Items.FindByValue(DataBinder.Eval(Container.DataItem,
"datafield")).Selected = true

End Sub

Where "dropDown1_onLoad" is the onLoad handler for the DropDownList in
question.

The errror was "BC30451: Name 'Container' is not declared."

Any ideas?

Thanks,
David
 
G

Guest

Ok, I solved it one way.

I will post it here for other who may find this thread.

Here is what I did:

1) Add ItemDataBound handler for the overall DataList. This will fire for
each row it creates.

2) In this handler, check if the current row is the one being edited.

3) If so, find the DropDownList we want to work with.

4) Then, set the "Selected" prop to true for the item in the DropDownList we
want pre-selected.

See code below.

-- David


---------------------------------

<asp:DataList id="DataList1" onItemDataBound="DataList1_ItemDataBound">

<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "datafield") %>
</ItemTemplate>

<EditItemTemplate>
<asp:DropDownList id="DropDown1">
<asp:ListItem value="opt1">opt1</asp:ListItem>
<asp:ListItem value="opt2">opt2</asp:ListItem>
<asp:ListItem value="opt3">opt3</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>

</asp:DataList>

-----------------------------------

Sub DataList1_ItemDataBound(sender As Object, e As DataListItemEventArgs)

If (e.Item.ItemType = ListItemType.EditItem) Then

Dim ddl1 = CType(e.Item.FindControl ("DropDown1"), DropDownList)

Dim myCurrentValue = getCurrentValueFromDataBase()

Dim itemInDDL1 = ddl1.Items.FindByValue(myCurrentValue )

If TypeOf itemInDDL1 is ListItem Then

itemInDDL1.Selected = true

End If

End If

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top