从Repeater中删除行

时间:2012-08-29 14:13:05

标签: asp.net .net sqldatasource

我的页面上有以下转发器:

<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" 
        onitemcommand="Repeater1_ItemCommand">
        <ItemTemplate>
            <div id="fullcommentheader">
                <span class="fullname">
                    <asp:Literal ID="Literal3" runat="server" Text='<%# Eval("Name") %>'></asp:Literal></span>
                <br />
                <span class="fullbodytext">
                    <asp:Button ID="Button2" runat="server" CommandName="Delete" CommandArgument='<%# Eval("Id") %>' Text="Delete" />
                    <asp:Literal ID="LitBody2" Text='<%# Eval("Message")%>' runat="server"></asp:Literal></span>
                <span class="dateTime">
                    <asp:Literal ID="Literal4" runat="server" Text='<%# Eval("CreateDateTime") %>'></asp:Literal></span>
            </div>
            <br />
        </ItemTemplate>
    </asp:Repeater>

我有一个 Button2 ,我想用它来删除转发器条目,如何查询数据库来实现这一目标?我习惯在gridview上默认使用这些命令,所以我不确定如何手动执行此操作。

我的活动:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete" && e.CommandArgument.ToString() != "")
        {

        }
    }

这是我的SqlDataSource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:orangefreshConnectionString1 %>"
        DeleteCommand="DELETE FROM [Comment] WHERE [Id] = @Id" 
        InsertCommand="INSERT INTO [Comment] ([Name], [Email], [Website], [Message], [PostId]) VALUES (@Name, @Email, @Website, @Message, @PostId)"
        SelectCommand="SELECT [Name], [Email], [Website], [Message], [PostId], [Id], [CreateDateTime] FROM [Comment] WHERE ([PostId] = @PostId)"
        UpdateCommand="UPDATE [Comment] SET [Name] = @Name, [Email] = @Email, [Website] = @Website, [Message] = @Message, [PostId] = @PostId, [CreateDateTime] = @CreateDateTime WHERE [Id] = @Id">
        <DeleteParameters>
            <asp:Parameter Name="Id" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Website" Type="String" />
            <asp:Parameter Name="Message" Type="String" />
            <asp:QueryStringParameter Name="PostId" QueryStringField="Id" Type="Int32" />
            <asp:Parameter Name="CreateDateTime" Type="DateTime" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="PostId" QueryStringField="Id" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="Website" Type="String" />
            <asp:Parameter Name="Message" Type="String" />
            <asp:Parameter Name="PostId" Type="Int32" />
            <asp:Parameter Name="CreateDateTime" Type="DateTime" />
            <asp:Parameter Name="Id" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>

2 个答案:

答案 0 :(得分:1)

首先需要声明一个私有的int _id全局变量。

然后,在sqldatasource删除事件中动态添加参数:

然后在你的ItemCommand'delete'中,将global_id设置为CommandArgument,因为你传入了它。然后执行一个SqlDataSource1.Delete()

_id = Convert.ToInt32(e.CommandArgument);
SqlDataSource1.Delete();
_id = 0;
//need to rebind your repeater here or you won't see the changes


protected void SqlDataSource1_Deleting(object sender, SqlDataSourceCommandEventArgs e)
{
  //add the parameter
  if (_id != 0)
    e.Command.Parameters["@Id"].Value = _id;
}   

答案 1 :(得分:0)

对转发器使用ItemCommand事件。这将在单击按钮时触发,并允许您访问命令名称和命令参数。然后,您可以使用命令参数从数据库中删除记录。

删除记录,您可以使用SqlDataSource.Delete()方法,但在调用该方法或处理Deleting事件并在那里填充参数之前,您需要填充delete参数。