无法在gridview上调用更新功能

时间:2014-03-08 16:35:41

标签: c# asp.net gridview objectdatasource aspxgridview

每次调用我的更新功能时,我都会遇到问题, 我相信问题的根源是因为这个函数得到了对象参数, 并且从一个不同的源头得到一个参数,我现在不是这样做的。

这是错误: http://prntscr.com/2z0cd6

GridView代码:

<asp:GridView ID="gvAnimals" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="ObjectDataSourceAnimals" ForeColor="#333333" GridLines="None" DataKeyNames="animalId">
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <Columns>
                    <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                    <asp:BoundField DataField="animalId" HeaderText="animalId" SortExpression="animalId" />
                    <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                    <asp:CheckBoxField DataField="vertebrates" HeaderText="vertebrates" SortExpression="vertebrates" />
                    <asp:CheckBoxField DataField="vegetarian" HeaderText="vegetarian" SortExpression="vegetarian" />
                    <asp:CheckBoxField DataField="terrestrial" HeaderText="terrestrial" SortExpression="terrestrial" />
                    <asp:BoundField DataField="kind" HeaderText="kind" SortExpression="kind" />
                    <asp:BoundField DataField="avgWeight" HeaderText="avgWeight" SortExpression="avgWeight" />
                    <asp:BoundField DataField="avgHeight" HeaderText="avgHeight" SortExpression="avgHeight" />
                    <asp:BoundField DataField="infoAdress" HeaderText="infoAdress" SortExpression="infoAdress" />
                    <asp:BoundField DataField="imageAdress" HeaderText="imageAdress" SortExpression="imageAdress" />
                </Columns>
                <EditRowStyle BackColor="#999999" />
                <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#E9E7E2" />
                <SortedAscendingHeaderStyle BackColor="#506C8C" />
                <SortedDescendingCellStyle BackColor="#FFFDF8" />
                <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
            </asp:GridView>
            <asp:ObjectDataSource ID="ObjectDataSourceAnimals" runat="server" DeleteMethod="DeleteAnimal" OldValuesParameterFormatString="original_{0}" SelectMethod="GetAllAnimals" TypeName="BLProject.Animal" UpdateMethod="UpdateAnimal" DataObjectTypeName="BLProject.Animal">
                <DeleteParameters>
                    <asp:Parameter Name="a" Type="Object" />
                    <asp:SessionParameter Name="newsAdress" SessionField="Adress" Type="String" />
                </DeleteParameters>
                <SelectParameters>
                    <asp:ControlParameter ControlID="textSearch" Name="Search" PropertyName="Text" Type="String" />
                    <asp:ControlParameter ControlID="dropdownlistAnimalToSearch" Name="type" PropertyName="SelectedValue" Type="String" />
                </SelectParameters>
                <UpdateParameters>
                    <asp:Parameter Name="a" Type="Object" />
                    <asp:SessionParameter DefaultValue="D:\\project\\Ilan Project 29-11\\Web\\NewsInfo" Name="newsAdress" SessionField="Adress" Type="String" />
                </UpdateParameters>
            </asp:ObjectDataSource>

*我相信错误的来源是粗线

更新功能:

[DataObjectMethod(DataObjectMethodType.Update)]
    public static int UpdateAnimal(Animal a, string newsAdress)

Animal a是网格视图使用select函数获取的对象参数, 但是地址是会话源的另一个参数,功能也是如此。 但是,由于我添加了这个参数,因此我遇到了这个新错误。

感谢。

1 个答案:

答案 0 :(得分:1)

你可以这样做:

使用此方法更改您的方法

public void UpdateAnimal(int animalId, string name, bool vertebrates, bool vegetarian, string newsAdress)
    {

    }

和你的ObjectDataSource

<asp:ObjectDataSource ID="ObjectDataSourceAnimals" runat="server" DeleteMethod="DeleteAnimal" SelectMethod="GetAllAnimals" TypeName="BLProject.Animal" UpdateMethod="UpdateAnimal">
            <DeleteParameters>
                <asp:Parameter Name="a" Type="Object" />
                <asp:SessionParameter Name="newsAdress" SessionField="Adress" Type="String" />
            </DeleteParameters>
            <SelectParameters>
                <asp:ControlParameter ControlID="textSearch" Name="Search" PropertyName="Text" Type="String" />
                <asp:ControlParameter ControlID="dropdownlistAnimalToSearch" Name="type" PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="animalId" Type="Int32"/>
                .......the other fields from Animal that you want to update
                <asp:Parameter Name="name" Type="String"/>
                <asp:Parameter Name="vegetarian" Type="Boolean"/>
                <asp:Parameter Name="vertebrates" Type="Boolean"/>
                <asp:SessionParameter DefaultValue="D:\\project\\Ilan Project 29-11\\Web\\NewsInfo" Name="newsAdress" SessionField="Adress" Type="String" />
            </UpdateParameters>
        </asp:ObjectDataSource>

似乎在使用DataObjectTypeName时无法添加参数。解决方案是发送作为参数编辑的Animal行的部分或全部属性以及其他参数。

如果您想使用OldValuesParameterFormatString="original_{0}",请将此int original_animalid添加到方法输入参数中。