Lonefish Lonefish - 2 months ago 46x
ASP.NET (C#) Question

Databinding issue with Eval/Bind - C# Webforms asp.net

I'm working on an existing project, doing some updates and have troubles setting the value of "FenSelectedValue" in the "FenDropDownListRoles" Control.

I keep getting the error:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control in repeater control

But the eval in the Label control works fine. I've been reading here and there, and I read things about it not being bound at the right time so I moved the control from "EditItemTemplate" where it eventually should be to "ItemTemplate", to test it, but still no luck..

<asp:Label ID="lblRolOmschrijving" Text='<%# Eval("Rol_omschrijving") %>' runat="server" />
<fen:FenDropDownListRoles ID="ddlRoles" FenSelectedValue='<%# Eval("Rol_omschrijving") %>' runat="server" Watermark="AdministratorType" Required="true" ValidationGroup="grpAddUser" />


Here's how I've learned to set drop down selected items in a grid view.

Example grid:

        <div id="gridContainerFormulations">
        <script type="text/javascript">
            $(document).ready(function () {
                //This is done here, instead of codebehind, because the SelectedValue property of the drop down list 
                //simply does not work when databinding. I set the two 'hid' values via the RowEditing event
        <asp:hiddenfield runat="server" id="hidSelectedFormulationLotNo" value="-1" />
        <asp:gridview id="dgrStudyFormulations" cssclass="data" runat="server" allowpaging="False" autogeneratecolumns="False"
            datakeynames="Id, FormulationLotNo, FormulationNo">
                    <asp:BoundField HeaderText="Formulation" ReadOnly="True" DataField="FormulationName" />
                    <asp:TemplateField HeaderText="Lot #">
                            <asp:dropdownlist ID="drpLotNumber" AddBlank="False" runat="server" />
                            <asp:Label ID="lblLotNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FormulationLot.Name")%>' />
                    <asp:BoundField HeaderText="AI in Formulation" ReadOnly="True" DataField="ActiveIngredientName" />
                    <asp:TemplateField HeaderText="AI Of Interest">
                            <asp:CheckBox ID="chkOfInterest" Checked='<%# DataBinder.Eval(Container.DataItem, "OfInterest")%>' runat="server" />
                            <%--<asp:Label ID="lblOfInterest" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OfInterest")%>' />--%>
                            <asp:image runat="server" id="imgOfInterest" Visible="False" />
                    <asp:BoundField HeaderText="AI Amount" ReadOnly="True" DataField="AIAmountText" />
                    <asp:CommandField ShowEditButton="True" ShowCancelButton="True" ShowDeleteButton="True"/>

Then in row_editing event of grid:

SelectedFormulationLotNo = CType(dgrStudyFormulations.DataKeys(e.NewEditIndex)("FormulationLotNo"), String)

Which sets the hidden field in the HTML

 Property SelectedFormulationLotNo() As String
        Return hidSelectedFormulationLotNo.Value.Trim()
    End Get
    Set(value As String)
        If String.IsNullOrEmpty(value) Then
            hidSelectedFormulationLotNo.Value = String.Empty
            hidSelectedFormulationLotNo.Value = value.Trim()
        End If
    End Set
End Property

And then the jQuery function call sets the correct option in the newly editable row in the grid.