Eval/Bind的数据绑定问题-C#Webforms asp.net

本文关键字:-C#Webforms asp net 问题 数据绑定 Bind Eval | 更新日期: 2023-09-27 17:59:37

我正在处理一个现有的项目,正在进行一些更新,在"FenDropDownListRoles"控件中设置"FenSelectedValue"的值时遇到了问题。

我一直收到错误:

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

但是Label控件中的eval运行良好。我一直在到处读,我读到一些关于它没有在正确的时间绑定的东西,所以我把控件从"EditItemTemplate"(它最终应该在哪里)移到了"ItemTemplate",以测试它,但仍然没有运气。。

                <ItemTemplate>
                    <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" />
                </ItemTemplate>

Eval/Bind的数据绑定问题-C#Webforms asp.net

以下是我如何学会在网格视图中设置下拉选定项目的。

示例网格:

        <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
                $("[id$='drpLotNumber']").val($("#hidSelectedFormulationLotNo").val());
            });
        </script>
        <asp:hiddenfield runat="server" id="hidSelectedFormulationLotNo" value="-1" />
        <asp:gridview id="dgrStudyFormulations" cssclass="data" runat="server" allowpaging="False" autogeneratecolumns="False"
            datakeynames="Id, FormulationLotNo, FormulationNo">
                <Columns>
                    <asp:BoundField HeaderText="Formulation" ReadOnly="True" DataField="FormulationName" />
                    <asp:TemplateField HeaderText="Lot #">
                        <EditItemTemplate>
                            <asp:dropdownlist ID="drpLotNumber" AddBlank="False" runat="server" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblLotNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FormulationLot.Name")%>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="AI in Formulation" ReadOnly="True" DataField="ActiveIngredientName" />
                    <asp:TemplateField HeaderText="AI Of Interest">
                        <EditItemTemplate>
                            <asp:CheckBox ID="chkOfInterest" Checked='<%# DataBinder.Eval(Container.DataItem, "OfInterest")%>' runat="server" />
                        </EditItemTemplate>
                        <ItemTemplate>
                            <%--<asp:Label ID="lblOfInterest" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OfInterest")%>' />--%>
                            <asp:image runat="server" id="imgOfInterest" Visible="False" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField HeaderText="AI Amount" ReadOnly="True" DataField="AIAmountText" />
                    <asp:CommandField ShowEditButton="True" ShowCancelButton="True" ShowDeleteButton="True"/>
                </Columns>
        </asp:gridview>

然后在网格的row_editing事件中:

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

设置HTML 中的隐藏字段

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

然后jQuery函数调用在网格中新编辑的行中设置正确的选项。

我最终是如何做到的(但把答案留在Rake36的答案上,因为它可能也能工作,并让我朝着我需要的方向前进)由于某种原因我无法让Javascript工作,而且我从混乱中知道我可以在"RowDataBound"中获得标签的值,我将Rake36方法与隐藏字段相结合,并在codeehind中设置值(在RowDataBound中)

在码尾:

    protected void gvwUsers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DropDownList DropDownListRol = (DropDownList)e.Row.FindControl("ddlRolOmschrijving");
        if (e.Row.RowType == DataControlRowType.DataRow && DropDownListRol != null)
        {
            DsFenVlaanderen.tb_rolDataTable dtRole = DsFenVlaanderen.RolTableAdapter.GetData();
            //Fill Dropdownlist
            DropDownListRol.DataSource = dtRole;
            DropDownListRol.DataValueField = dtRole.Rol_IDColumn.ColumnName;
            DropDownListRol.DataTextField = dtRole.Rol_omschrijvingColumn.ColumnName;
            DropDownListRol.DataBind();
            //Set Selected value
            DropDownListRol.Items.FindByValue(hidSelectedRole.Value).Selected = true;
        }
    }
    protected void gvwUsers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        //Set hiddenfield to value of Rol_ID
        hidSelectedRole.Value = gvwUsers.DataKeys[e.NewEditIndex].Values["Rol_ID"].ToString();
    }

这是我的网格:

    <asp:hiddenfield runat="server" id="hidSelectedRole" value="-1" />
    <fen:FenGridViewSelectable ID="gvwUsers" runat="server" Selectable="False"
        DataSourceID="dsUsers" EnableModelValidation="True" SkinID="Blue"
        AllowSorting="True" OnDataBound="gvwUsers_DataBound" OnRowDeleting="gvwUsers_RowDeleting"
        AutoGenerateColumns="False" DataKeyNames="User_ID,Rol_ID" OnRowDataBound="gvwUsers_RowDataBound" OnRowEditing="gvwUsers_RowEditing" OnRowUpdating="gvwUsers_RowUpdating">
        <Columns>
            <asp:BoundField DataField="User_ID" HeaderText="Gebruikersnaam" ReadOnly="True" SortExpression="User_ID" />
            <asp:BoundField DataField="User_ID_EXT" HeaderText="Naam" ReadOnly="true" SortExpression="User_ID_EXT" />
            <%-- <asp:BoundField DataField="Rol_omschrijving" HeaderText="Type bestuurder" SortExpression="Rol_omschrijving" /> --%>
            <asp:TemplateField HeaderText="Type bestuurder" SortExpression="Rol_omschrijving">
                <ItemTemplate>
                    <asp:Label ID="lblRolOmschrijving" Text='<%# Eval("Rol_omschrijving") %>' runat="server"/>
                </ItemTemplate>
                <EditItemTemplate> 
                    <asp:DropDownList ID="ddlRolOmschrijving" runat="server" DataField="Rol_omschrijving"></asp:DropDownList>
                </EditItemTemplate>
            </asp:TemplateField>
            <fen:FenTemplateField HeaderStyle-Width="100px">
                <ItemTemplate>
                    <fen:FenButton ID="btnEdit" runat="server" Function="Edit" />
                    <fen:FenButton ID="btnDelete" runat="server" Function="Delete" />
                </ItemTemplate>
                <EditItemTemplate>
                    <fen:FenButton ID="btnUpdate" runat="server" Function="Update" />
                    <fen:FenButton ID="btnCancel" runat="server" Function="CancelInline" />
                </EditItemTemplate>
            </fen:FenTemplateField>
        </Columns>
    </fen:FenGridViewSelectable>
    <asp:ObjectDataSource ID="dsUsers" runat="server"
        OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
        TypeName="FenVlaanderen.DsFenVlaanderenTableAdapters.vUsersTableAdapter"></asp:ObjectDataSource>
    <asp:Label ID="lblNoResults" runat="server" Visible="false" CssClass="error">Er werden geen gebruikers gevonden.</asp:Label>
    <asp:Label ID="lblDeleteNotAllowed" runat="server" Visible="false" CssClass="error" />
    <fen:AddUser ID="addUser" runat="server" OnFenControlSaved="addUser_FenControlSaved" />
</ContentTemplate>