简体   繁体   English

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

[英]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. 我正在处理一个现有项目,正在进行一些更新,并且在“ FenDropDownListRoles”控件中设置“ FenSelectedValue”的值时遇到麻烦。

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. 但是Label控件中的eval可以正常工作。 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.. 我一直在这里和那里阅读,我读到有关它没有在正确的时间绑定的信息,因此我将控件从“ 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>

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
                $("[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>

Then in row_editing event of grid: 然后在grid的row_editing事件中:

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

Which sets the hidden field in the HTML 在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

And then the jQuery function call sets the correct option in the newly editable row in the grid. 然后jQuery函数调用在网格中新可编辑的行中设置正确的选项。

How I finally did it(but leaving the answer on Rake36's answer, since it probably works too and got me in the direction I needed) Since I couldn't get the Javascript to work for some reason and I knew from messing around that I could get the value of labels in "RowDataBound" I combined the method of Rake36 with the hidden field and set the value in the codebehind (in RowDataBound) 我最终是如何做到的(但是将答案留在Rake36的答案上,因为它可能也可以工作,并且使我朝着需要的方向前进)因为我由于某种原因无法使Javascript工作,并且从搞乱中我知道我可以在“ RowDataBound”中获取标签的值,我将Rake36的方法与隐藏字段结合在一起,并在代码隐藏区(在RowDataBound中)中设置了值

In the codebehind: 在后面的代码中:

    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();

    }

This is my grid: 这是我的网格:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM