0

データベース内のデータを選択、削除、更新するためにグリッドビューを使用しています。これらすべての操作を実行するための単一の SP を作成しました。パラメータに基づいて、SP は実行する操作を決定します。

これが私のグリッドのグリッドビュー 画像の画像です http://www.freeimagehosting.net/uploads/0a5de50661.jpg

         <asp:GridView ID="GridView1" runat="server" DataSourceID="dsDomain" 
            AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
            DataKeyNames="DomainId" >
            <Columns>
                <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
                <asp:BoundField DataField="DomainId" HeaderText="DomainId" 
                    InsertVisible="False" ReadOnly="True" SortExpression="DomainId">
                </asp:BoundField>
                <asp:BoundField DataField="Domain" HeaderText="Domain" 
                    SortExpression="Domain">
                </asp:BoundField>
                <asp:BoundField DataField="Description" HeaderText="Description" 
                    SortExpression="Description" >
                </asp:BoundField>                            
                <asp:BoundField DataField="InsertionDate" HeaderText="InsertionDate" 
                    SortExpression="InsertionDate">
                </asp:BoundField>                            
        </asp:GridView> 

使用しているデータソースはこちら

           <asp:SqlDataSource ID="dsDomain" runat="server" 
                ConnectionString="<%$ ConnectionStrings:conLogin %>" 
                SelectCommand="Tags.spOnlineTest_Domain" 
                SelectCommandType="StoredProcedure" CancelSelectOnNullParameter="False"
                        DeleteCommand="Tags.spOnlineTest_Domain" DeleteCommandType="StoredProcedure" OnDeleting="DomainDeleting">

                <SelectParameters>

                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="DomainId" Type="String" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Domain" Type="String" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Description" Type="String" />
                    <asp:Parameter  DefaultValue="1" Name="OperationType" Type="Byte" />
                </SelectParameters>
                <DeleteParameters>
                     <asp:ControlParameter ControlID="GridView1" Name="DomainId" 
                        PropertyName="SelectedValue" Size="4" Type="Int32" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Domain" Type="String" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="" Name="Description" Type="String" />
                    <asp:Parameter ConvertEmptyStringToNull="true" DefaultValue="4" Name="OperationType" Type="Byte" />
                </DeleteParameters>
             </asp:SqlDataSource>

選択操作は正常に機能しています。しかし、削除しようとすると、

プロシージャまたは関数 'spOnlineTest_Domain' には、指定されていないパラメーター '@Domain' が必要
ですが、このパラメーターを指定しています。

私のストアドプロシージャ呼び出しはこのようなものです

EXEC Tags.spOnlineTest_Domain NULL, NULL, NULL, 1 // Select の場合、最後のパラメーターは 1 になります EXEC Tags.spOnlineTest_Domain "SelectedRow's DomainId), NULL, NULL, 4 // Delete の場合、最後のパラメーターは 4 になります

私の手順には4つのパラメーターがあり、最後のパラメーターはプログラマーによって設定され、実行される操作の種類をプログラムに伝えます。Select の場合、最後のパラメーターのみが Not Null である必要があります。削除の場合、最初と最後のパラメーターを NULL にすることはできません。

私の最初の削除パラメーターは、テーブルの主キーです。ユーザーが行を選択して削除を押すと、この値が渡されます。PropertyName="SelectedValue" を使用しても、ID の正しい値を取得できるかどうかわかりません。

http://www.freeimagehosting.net/uploads/0a5de50661.jpg />

4

2 に答える 2

2

ObjectDataSource の OnDeleting イベントを実装していない場合は、以下を試してください。

<asp:ObjectDataSource .... OnDeleting="YourDeletingEvent" ...
</asp:ObjectDataSource>

あなたのコードビハインドで:

private void YourDeletingEvent(object source, ObjectDataSourceMethodEventArgs e)
{
  IDictionary paramsFromPage = e.InputParameters;

  //In this case I assume your stored procedure is taking a DomainId as a parameter
  paramsFromPage.Remove("Domain");
  paramsFromPage.Add("Domain", (int)paramsFromPage["DomainId"]);
  paramsFromPage.Remove("DomainId");
}

詳しくはこちらをご覧ください。

于 2010-03-02T16:03:12.290 に答える
1

次のように記述できます:(gridView タグの間) \< asp:TemplateField ShowHeader="false"> \asp:LinkBut​​ton ID="linkBut​​ton1" runat="server" CausesValidation="false" CommandName="Delete" OnClientClick="return confirm( 'このレコードを削除してもよろしいですか?');" Text="削除" />

     </ItemTemplate>

     </asp:TemplateField>

そして、「GridView1_RowDeleting」メソッドを実装できるようになった後、次のようになります。

        //makeDelete("YourStoredProcedure");
        //mydataBindGV();
    }
于 2011-07-14T08:56:01.197 に答える