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

Eval/Bind-C#Webforms asp.net的数据绑定问题,c#,asp.net,webforms,eval,C#,Asp.net,Webforms,Eval,我正在处理一个现有项目,正在进行一些更新,在“FendropDownstroles”控件中设置“FenSelectedValue”的值时遇到问题 我不断得到错误: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control in repeater control 但是标签控件中的eval工作正常。我到处都在读,我读到一些关于它没有在正

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

我不断得到错误:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control in repeater control
但是标签控件中的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>

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

示例网格:

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

下面是我如何在网格视图中设置下拉选择项的学习

示例网格:

        <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>
设置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”中获得标签的值,因此我将的方法与隐藏字段相结合,并在codebehind(在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();

    }
这是我的网格:


呃,我是格布吕克·格沃登。
我最终是如何做到的(但将答案留在Rake36的答案上,因为它可能也能起作用,并让我朝着我需要的方向前进),因为出于某种原因,我无法让Javascript正常工作,而且我知道我可以在“RowDataBound”中获得标签的值我将的方法与隐藏字段相结合,并在codebehind(在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();

    }
这是我的网格:


呃,我是格布吕克·格沃登。


如果您使用内置的
下拉列表,它能工作吗?
?还没有尝试过,会尝试,但有点重构。自定义的一个(已经存在)由它自己填充,它应该告诉您从哪里开始查找,如果更改后没有失败,那么问题就出在
fendropDownstroles
的某个地方。我可能遗漏了一些内容,但我找不到在dropdownlist控件中设置所选值的方法。(可能是我的同事决定编写一个自定义的)在您的
Repeater.OnItemDataBound
事件中执行此操作。如果您使用内置的
DropDownList
,它能工作吗?还没有尝试过,会尝试,但它有点重构。自定义的一个(已经存在)由它自己填充,它应该告诉您从哪里开始查找,如果更改后没有失败,那么问题就出在
fendropDownstroles
的某个地方。我可能遗漏了一些内容,但我找不到在dropdownlist控件中设置所选值的方法。(可能是我的同事决定编写一个自定义的)在您的
Repeater.OnItemDataBound
事件中执行此操作。在JS中设置它是否会将整个网格的值限制为一个值?我需要它是编辑时的旧值..Nvm,发现它发生在单击行上的编辑时。我使用了hiddenfield,但在“RowDataBound”事件中复制了该值,因为Javascript出现了一些奇怪的错误,我更喜欢codebehind。谢谢@Lonefish:酷。您是说您可以在RowDataBound事件中设置所选的值?我永远也不能让它“粘住”。我可以将值列表绑定到下拉列表,但从不设置所选值。你能分享你的工作RowDataBound代码吗?我把它作为一个答案添加了进来,因为它比Comments中的要好一点。你不能在JS中设置它,将整个网格的值限制为一个值吗?我需要它是编辑时的旧值..Nvm,发现它发生在单击行上的编辑时。我使用了hiddenfield,但在“RowDataBound”事件中复制了该值,因为Javascript出现了一些奇怪的错误,我更喜欢codebehind。谢谢@Lonefish:酷。您是说您可以在RowDataBound事件中设置所选的值?我永远也不能让它“粘住”。我可以将值列表绑定到下拉列表,但从不设置所选值。你能分享你的工作RowDataBound代码吗?我把它作为一个答案添加了进来,因为它比注释中的要好一点