DropDownList in Repeater


S

Shaun Camilleri

Hi all,
I am creating a DropDownList in a RepeaterControl. After the Repeater is
DataBound in the ItemCreated event (of the Repeater) I bind the DropDownList
to a Table and then try to select one of its value. Here is the code:

If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType =
ListItem.AlternatingItem Then
Dim ddl as DropDownList = Ctype(e.Item.FindControl("ddl"), DropDownList)

With ddl
.DataSource = ds.Tables("tbl")
.DataValueField = "Code"
.DataTextField = "Name"
.DataBind

.SelectedValue = Ctype(e.Item.DataItem, DataRow)("Code").ToString
End With
End If

If I check the DropDownList.SelectedValue while I'm still in the above code
the SelectedValue is correctly set, but when i finally view the resulting
page all the DropDownLists controls that the Repeater has the first Item
selected... Apart from DropDownList.SelectedValue i tried to use other
methods (i.e. .SelectedIndex) but the result is the same, the first value of
the DropDown is selected...

Thanks for you help
Shaun
 
Ad

Advertisements

Y

Yunus Emre ALPÖZEN

You don't need to use ItemCreated event. Simply use dropdownlist properties.
Forexample,

<asp:DropDownList id=ddl DataSource="<%# SourceTable %>" runat="server"
DataTextField="Name" DataValueField="Code" SelectedIndex='<%#
ListIndex((DropDownList)target,DataBinder.Eval( Container.DataItem,"Code"))
%>'></asp:DropDownList>

SourceTable should be protected property in your page and its type is
DataTable.
ListIndex is a helper method traverse DropDownList items and compares each
item's value with second parameter of this function. And returns item's
index.

Also SelectedIndex must be given after DataSource Property. Other wise it
fails. I submit this problem as a bug. But i couldn't express myself :)))

I hope, this helps you...
 

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

Top