【问题标题】:No Id in ObjectDataSource Update method ParrameterObjectDataSource 更新方法参数中没有 Id
【发布时间】:2011-11-20 17:47:10
【问题描述】:

我有一个带有简单表单视图和对象数据源的 .Net 3.5 应用程序。

<asp:ObjectDataSource ID="odsDevice" runat="server" SelectMethod="GetDeviceByDeviceNumber" EnableViewState="True"
     UpdateMethod="updateDevice" DataObjectTypeName="GPSO.Repository.Device" TypeName="GPSOnline.ATOMWebService">
<SelectParameters>
    <asp:SessionParameter DbType="Guid" Name="deviceid" SessionField="deviceid" />
</SelectParameters>

这是表单视图

<asp:FormView ID="fvDevices" runat="server" DataSourceID="odsDevice">
<EditItemTemplate>
    <table cellpadding="0" cellspacing="0" class="AdminContent">
        <tr>
            <td class="Content">
                <h1>
                    Device Details</h1>
                <hr />
                <table class="AdminDetails" cellpadding="0" cellspacing="0">
                    <tr>
                        <th>
                            Atom Serial #
                        </th>
                        <td class="control">
                            <asp:TextBox ID="txtDeviceNumber" runat="server" Text='<%#Bind("DeviceNumber")%>' />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            &nbsp;
                        </th>
                        <td class="control">
                            <asp:RequiredFieldValidator ID="rfvDeviceNumber" runat="server" ToolTip="Must specify Device Number"
                                Display="Dynamic" ErrorMessage="Device Number is Required" EnableClientScript="false"
                                ControlToValidate="txtDeviceNumber"></asp:RequiredFieldValidator>
                            <asp:RegularExpressionValidator ID="revDeviceAddId" runat="server" ToolTip="Device ID must be an Integer."
                                ValidationExpression="[0-9]{1,6}" Display="Dynamic" ErrorMessage="Device ID must be an Number, less then 1000000."
                                EnableClientScript="false" ControlToValidate="txtDeviceNumber"></asp:RegularExpressionValidator>
                            <asp:CustomValidator ID="cvDeviceAddId" runat="server" ErrorMessage="Device ID already Exists."
                                Display="Dynamic" ToolTip="Device ID already Exists." ControlToValidate="txtDeviceNumber"
                                OnServerValidate="CheckDeviceDoesNotExist"></asp:CustomValidator>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            &nbsp;
                        </th>
                        <td class="control">
                            <asp:Label runat="server" ForeColor="Red" ID="lblFeedback" Text="" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            &nbsp;
                        </th>
                        <td class="control">
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <th>
                            &nbsp;
                        </th>
                        <td class="control">
                            <asp:Button runat="server" ID="btnUpdate" Text="Update" CausesValidation="true" CommandName="Update"
                                Width="160px" />
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</EditItemTemplate>

这里是对象数据源方法的代码。

    [OperationContract]
    [WebGet]
    public  void updateDevice(Device device)
    {
        DeviceRep.updateDevice(device);

    }

    [OperationContract]
    [WebGet]
    public Device GetDeviceByDeviceNumber(Guid deviceid)
    {
        return DeviceRep.GetDeviceByDeviceNumber(deviceid);}

现在业务对象包含我对更新不感兴趣的几个字段,我只想更新设备编号。

但是我遇到的问题是,我作为“public void updateDevice(Device device)”方法的参数获得的设备对象没有正在更新的对象的 ID。这使得无法知道要更新存储库中的哪条记录。

有没有办法保留设备的 ID,以便在更新时它是对象的一部分?

【问题讨论】:

    标签: asp.net objectdatasource


    【解决方案1】:

    不确定,但您必须将FormViewDataKeyNames 属性设置为,

     <asp:FormView ID="fvDevices" runat="server" 
       DataSourceID="odsDevice" DataKeyNames="deviceid" ..
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      相关资源
      最近更新 更多