ObjectDataSource Update方法参数中没有Id

时间:2011-09-20 03:25:55

标签: asp.net objectdatasource

我有一个.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,以便在更新时将其对象的一部分保留下来?

1 个答案:

答案 0 :(得分:2)

不确定,但您必须将DataKeyNames的{​​{1}}属性设置为

FormView