DataGrid with DropDownList blues

I

Igor Stavnitser

PLEASE HELP!This is driving me insane.

I have a datagrid with a templatecolumn.
The template column contains drop-down list. I populate the list with
values using OnDataBinding event of the DropDownList control. In that
same event handler I also set the selected item. I stepped through the
code and ensured that 'ddl.SelectedValue =
dti.DataItem("FieldGroupID")' is correct, meaning that ddl.Items
contains an item that I am setting, and dti.DataItem("FieldGroupID")
returns correct available value!

ASPX code:
<asp:DataGrid id="dtgFieldOrder" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField=FieldName
HeaderText="Field Name"></asp:BoundColumn>

<asp:TemplateColumn><ItemTemplate>
<input type=hidden runat=server

value='<%#DataBinder.Eval(Container.DataItem,"FieldPositionID")%>'>
<asp:DropDownList runat=server
OnDataBinding="PopulateList"
OnSelectedIndexChanged="GroupSelectionChanged"
AutoPostBack=True
ID=GroupSelector>
</asp:DropDownList>
</ItemTemplate></asp:TemplateColumn>
.....

Corresponding Codebehind:
Protected Sub PopulateList(ByVal sender As Object, ByVal e As
EventArgs)
Dim ddl As DropDownList = CType(sender, DropDownList)
ddl.DataSource = dtGroups
ddl.DataTextField = "FieldGroupName"
ddl.DataValueField = "ID"
RemoveHandler ddl.DataBinding, AddressOf PopulateList
ddl.DataBind()
Dim dti As DataGridItem = ddl.NamingContainer

'The following statement executes fine, but does not set selected
value!!!
'I have also tried another way of doing it below no luck with either
ddl.SelectedValue = dti.DataItem("FieldGroupID")

'Another way in which it does not work
'Dim lb As ListItem =
ddl.Items.FindByValue(dti.DataItem("FieldGroupID"))
'lb.Selected = True
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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top