“数据绑定方法,如Eval(),XPath()和Bind()只能在数据绑定控件的上下文中使用”

时间:2018-01-11 16:46:27

标签: c# asp.net webforms

很确定我知道为什么我会收到错误,只是不确定如何解决它。在FormView ItemTemplate中,我有一个Label控件,用于绑定到FormView的SQLDatasource的Project ID#。我还有一个DropDownList,其SQLDatasource我已经将Label.Text绑定为ControlParameter。我猜测,因为Label控件还没有绑定,这导致我的DDL发出错误。

ASPX

<asp:FormView ID="FvChangeOrder" runat="server" DataKeyNames="ProjectChangeOrderID"
    DataSourceID="FvChangeOrderSQL" OnItemCommand="FvChangeOrder_OnItemCommand"
    OnDataBound="FvChangeOrder_OnDataBound">
    <ItemTemplate>
        <div>Project #: </div>
        <div>
            <asp:Label ID="LblProjectID" runat="server" Text='<%# Bind("ProjectID") %>' /></div>
        <div>Shipment #: </div>
        <div>
            <asp:DropDownList runat="server" ID="DdlShipment" DataSourceID="DdlShipmentSQL"
                SelectedValue='<%# Bind("ProjectShipmentID") %>' DataValueField="ProjectShipmentID"
                DataTextField="ShipmentNo" Enabled="False" />
        </div>
        ...
    </ItemTemplate>
</asp:FormView>

<asp:SqlDataSource ID="FvChangeOrderSQL" runat="server" 
ConnectionString="<%$ ConnectionStrings:ProjectLogicTestConnectionString %>" 
SelectCommand="SELECT [ProjectID], [ProjectChangeOrderID], [ProjectShipmentID], [SeqNo], 
    [Date], [EnteredBy_UserID], [Source], [Initiator], [Reason], [ReasonNotes], 
    [ApprovalCode], [Description], [NumPanels], [Amount], [IsCommissionable], [DateDue], 
    [DateRecd], [Status]
FROM [tblProjectChangeOrder] WHERE ([ProjectChangeOrderID] = @PCOID)">
<SelectParameters>
    <asp:QueryStringParameter Name="PCOID" QueryStringField="PCOID" Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>

<asp:SqlDataSource ID="DdlShipmentSQL" runat="server" 
ConnectionString="<%$ ConnectionStrings:ProjectLogicTestConnectionString %>"
SelectCommand="SELECT pco.ProjectShipmentID, ps.ShipmentNo FROM tblProjectChangeOrder pco
LEFT JOIN tblProjectShipment ps ON pco.ProjectShipmentID = ps.ProjectShipmentID 
WHERE pco.ProjectID = @ProjectID">
    <SelectParameters>
        <asp:ControlParameter ControlID="FvChangeOrder$LblProjectID" 
        Name="ProjectID" PropertyName="Text"/>
    </SelectParameters>
</asp:SqlDataSource>

我最好不要在DDL SQLDatasource上省略WHERE和ControlParameter,并在FormView的OnDataBound方法的代码隐藏中更改DDL的SQL Source吗?

1 个答案:

答案 0 :(得分:0)

想想我已经解决了。我将ControlParameter改为参数类型=&#34;字符串&#34;并在Form_Load上将Label设置为FvChangeOrder.FindControl(&#34; LblProjectID&#34;),将LblProject.Text分配给字符串,然后将其作为Select Parameter的DefaultValue传递。

在一个半相关的问题上,我必须删除标记端的SelectedValue,并在Form_Load中为DDL SelectedValue处理SQLCommand。可能是一个糟糕的数据集,但我一直收到错误,该字段中的值不在列表中或类似的东西。对于给定项目,有多个变更单具有ShippingID的NULL值。

Anywho,所有人都在使用ItemTemplate方面。现在看看我是否可以保持EditItemTemplate上的所有工作。 :P