如何使用向导从使用数据源和SP的gridview中删除数据

发布于 2024-08-23 07:15:05 字数 3731 浏览 9 评论 0原文

我正在使用 gridview 来选择、删除和更新数据库中的数据。我已经编写了一个 SP 来完成所有这些操作。 SP 根据参数决定执行哪个操作。

这是我的 gridview 的图像 我的网格图像 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" />
 
                    
                    
                
             

选择操作工作正常。 但当我尝试删除时,它说

过程或函数“spOnlineTest_Domain”需要参数“@Domain”,但未提供该参数
但我提供这个参数,如

我的存储过程调用是这样的

EXEC Tags.spOnlineTest_Domain NULL, NULL, NULL, 1 // For Select 最后一个参数将为 1 EXEC Tags.spOnlineTest_Domain "SelectedRow's DomainId), NULL, NULL, 4 // 对于删除,最后一个参数将为 4

我的程序有 4 个参数,其中最后一个参数将由程序员设置,这将告诉程序要执行哪种操作。 对于 Select,只有最后一个参数必须为 Not Null。 对于删除,第一个和最后一个参数不能为 NULL。

我的第一个删除参数是表的主键。当用户选择一行并点击删除时,我将传递此值。我不确定使用 PropertyName="SelectedValue" 能否获得正确的 ID 值。

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

I am using a gridview to select, delete and update data in database. I have written a single SP for doing all these operation. Based on a parameter SP decides which operation to perform.

Here is the image of my gridview
image of my grid 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> 

Data Source that I am using is here

           <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>

Select operation is working fine.
But when I tried to delete, it says

Procedure or Function 'spOnlineTest_Domain' expects parameter '@Domain', which was not supplied
But I am supplying this parameter, as

My Stored procedure calling is like this

EXEC Tags.spOnlineTest_Domain NULL, NULL, NULL, 1 // For Select last parameter will be 1
EXEC Tags.spOnlineTest_Domain "SelectedRow's DomainId), NULL, NULL, 4 // For Delete last parameter will be 4

My procedure has 4 parameters where last parameter will be set by programmer which will tell the program for what kind of operation to be performed.
For Select only last parameter has to be Not Null.
For Delete first and last parameter cannot be NULL.

My first Delete parameter is Primary key of the table. I am passing this value, when a user selects a row and hit delete. I am not sure by using PropertyName="SelectedValue", will I get the right value of the ID.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-08-30 07:15:05

如果您尚未实现 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");
}

请查看 此处了解更多详细信息。

If you have not implemented OnDeleting event of the ObjectDataSource, try the below

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

In your code behind:

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");
}

Please look here for more details.

じ违心 2024-08-30 07:15:05

你可以写:(在gridView标签之间)
\< asp:TemplateField ShowHeader="false">

\asp:LinkBut​​ton ID="linkBut​​ton1" runat="服务器" CausesValidation="false" CommandName="删除"
OnClientClick =“返回确认('您确定要删除此记录吗?');”文本=“删除”/>

     </ItemTemplate>

     </asp:TemplateField>

在你可以实现“GridView1_RowDeleting”方法之后,如下所示:
protected void GridView1_RowDeleting(对象发送者,GridViewDeleteEventArgs e)
{

        //makeDelete("YourStoredProcedure");
        //mydataBindGV();
    }

u can write:(between gridView tags)
\< asp:TemplateField ShowHeader="false">

\asp:LinkButton ID="linkButton1" runat="server" CausesValidation="false" CommandName="Delete"
OnClientClick="return confirm('Are you sure you want to delete this record?');" Text="Delete" />

     </ItemTemplate>

     </asp:TemplateField>

and after u can implement the "GridView1_RowDeleting" methode,like that:
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

        //makeDelete("YourStoredProcedure");
        //mydataBindGV();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文