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

时间:2016-08-01 14:35:21

标签: c# asp.net webforms eval

我正在处理现有项目,进行一些更新,并且设置了" FenSelectedValue"的价值。在" FenDropDownListRoles"控制。

我一直收到错误:

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

但是Label控件中的eval运行正常。我一直在这里读书,我读到的东西没有在正确的时间被束缚,所以我把控件从#34; 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>

2 个答案:

答案 0 :(得分:1)

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

示例网格:

        <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>

然后在grid的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函数调用在网格中新编辑的行中设置正确的选项。

答案 1 :(得分:1)

我是如何最终做到的(但是在Rake36的回答中留下答案,因为它可能也有效并让我朝着我需要的方向前进)因为我无法让Javascript因某些原因而起作用而且我从乱搞中知道我可以在&#34; RowDataBound&#34;中得到标签的价值。我将Rake36的方法与隐藏字段结合起来并在代码隐藏中设置值(在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>