C# 当前上下文中不存在中继器中的删除按钮

C# 当前上下文中不存在中继器中的删除按钮,c#,asp.net,repeater,C#,Asp.net,Repeater,=================== 更新:29/06/2017 我正在尝试使中继器控件中的删除按钮按预期运行。目的是让按钮“激发”我的MSSQL数据库中的存储过程 我要感谢温的深入回应,尽管我仍在努力解决这个问题。我承认,一开始我可能无法正确地表达我的问题。因此,我编辑了我的帖子,以显示我现在拥有的代码。我相信我即将解决这个问题,并衷心感谢任何帮助 我的*.aspx页面中的代码: <asp:SqlDataSource ID="SqlDataSource2" runat="server"

===================

更新:29/06/2017 我正在尝试使中继器控件中的删除按钮按预期运行。目的是让按钮“激发”我的MSSQL数据库中的存储过程

我要感谢温的深入回应,尽管我仍在努力解决这个问题。我承认,一开始我可能无法正确地表达我的问题。因此,我编辑了我的帖子,以显示我现在拥有的代码。我相信我即将解决这个问题,并衷心感谢任何帮助

我的*.aspx页面中的代码:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ 
ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM 
[Comments] WHERE ([Ad_ID] = @Ad_ID) ORDER BY [CommentCreationDateTime] ASC">
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2" 
Visible="True" OnItemCommand="Repeater1_ItemCommand">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<table id="displayCommentsTable" class="displayCommentsTable">
<tr class="displayCommentsTable"><td class="displayCommentsTable">
<asp:ImageButton ID="deleteCommentImageButtonReal" runat="server" 
class="rightCross" ImageUrl="images/Red-Cross-Mark-PNG.png" 
OnClientClick="return confirm('Are you sure you wish to delete this 
comment?');" Height="11" Width="11" CommandName="Delete" 
CommandArgument='<%# Eval("Comment_ID") %>' /><%# Eval("CommenterName") %> 
commented on <%# Eval("CommentCreationDateTime", "{0:d/M/yyyy <i> hh:mm:ss 
tt}") %>
</td></tr>
<tr class="displayCommentsTable"><td class="displayCommentsTable"><%# 
Eval("CommentText") %><br /></td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
也许值得一提的是,如果我“硬编码”我试图删除的行,那么它就可以工作。例如,如果我在“删除”按钮中使用了以下选项:

CommandArgument='44'

然后,存储过程将按预期触发并影响第44行

在什么情况下,您试图访问中继器按钮

您需要尝试在repeater项中查找控件。 例如: 按钮btn1=(按钮)rptItem.FindControl(“btn1”)

最简单的方法是使用事件



代码隐藏
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
命名空间DemoWebForm
{
公共部分类RepeaterDemo:System.Web.UI.Page
{
公开课评论
{
公共int Id{get;set;}
公共字符串名称{get;set;}
}
公共静态IList注释=新列表
{
新注释{Id=1,Name=“One”},
新注释{Id=2,Name=“Two”},
新注释{Id=3,Name=“Three”}
};
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(!IsPostBack)
{
Repeater1.DataSource=注释;
Repeater1.DataBind();
}
}
受保护的void Repeater1\u ItemCommand(对象源,RepeaterCommandEventArgs e)
{
如果(如CommandName==“删除”)
{
int id=Convert.ToInt32(e.CommandArgument);
var comment=Comments.FirstOrDefault(c=>c.Id==Id);
注释。删除(注释);
Repeater1.DataSource=注释;
Repeater1.DataBind();
}
}
}
}

一切正常,但由于我没有指定只返回未被软删除的结果,所以返回了所有内容。不要犯错误,为未来学点东西

非常感谢您的解决方案。我已经更新了我的原始问题,因为我遇到了一些问题。您需要为CommandArgument分配唯一的id
CommandArgument=''
。连接字符串是什么意思?连接字符串用于定义如何连接到数据库,而不是确定返回的结果。为我的错误术语道歉。我是一个新手开发人员,我仍然在学习ASP。我想说的是,在我定义从数据库中带回什么的地方,我带回了所有东西,并且没有忽略(软)删除的评论。我的代码很好,因为它(软)删除了注释,我只是犯了一个noob错误,忽略了我的明显错误。这与连接字符串无关。请更正你的答案。
CommandArgument='44'
<%@ Page Language="C#" AutoEventWireup="true" 
     CodeBehind="RepeaterDemo.aspx.cs" Inherits="DemoWebForm.RepeaterDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Repeater ID="Repeater1" OnItemCommand="Repeater1_ItemCommand" runat="server">
            <ItemTemplate>
                <p>
                    <%#Eval("Name") %>
                    <asp:ImageButton CommandArgument='<%# Eval("Id") %>' runat="server"
                        ImageUrl="~/Images/Delete.png" CommandName="Delete" />
                </p>
            </ItemTemplate>
        </asp:Repeater>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DemoWebForm
{
    public partial class RepeaterDemo : System.Web.UI.Page
    {
        public class Comment
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        public static IList<Comment> Comments = new List<Comment>
        {
            new Comment {Id = 1, Name = "One"},
            new Comment {Id = 2, Name = "Two"},
            new Comment {Id = 3, Name = "Three"}
        };

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Repeater1.DataSource = Comments;
                Repeater1.DataBind();
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                var comment = Comments.FirstOrDefault(c => c.Id == id);
                Comments.Remove(comment);

                Repeater1.DataSource = Comments;
                Repeater1.DataBind();
            }
        }
    }
}