0

我的页面上有以下中继器:

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

2 回答 2

1

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

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

然后在您的 ItemCommand 'delete' 中,将全局 _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;
}   
于 2012-08-29T14:45:53.143 回答
0

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

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

于 2012-08-29T14:16:40.210 回答