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:
<aspataGrid 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")%>'>
<aspropDownList runat=server
OnDataBinding="PopulateList"
OnSelectedIndexChanged="GroupSelectionChanged"
AutoPostBack=True
ID=GroupSelector>
</aspropDownList>
</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
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:
<aspataGrid 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")%>'>
<aspropDownList runat=server
OnDataBinding="PopulateList"
OnSelectedIndexChanged="GroupSelectionChanged"
AutoPostBack=True
ID=GroupSelector>
</aspropDownList>
</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