Repeater 中的删除按钮在当前上下文中不存在
Delete Button within Repeater does not exist within current context
===================
更新:29/06/2017
我正在尝试让我的转发器控件中的删除按钮按预期运行。目的是获取 "fire" 我的 MSSQL 数据库中存储过程的按钮。
感谢 Win 的深入回复,尽管我仍在努力解决问题。我承认我一开始可能无法正确表达我的问题。因此,我编辑了 post 以显示我现在拥有的代码。我相信我即将解决这个问题,并真诚地感谢任何帮助。
我的 *.*aspx 页面中的代码:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM
[Comments] WHERE ([Ad_ID] = @Ad_ID) ORDER BY [CommentCreationDateTime] ASC">
再往下 *.*aspx 页面:
<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>
最后,我的代码背后的魔法应该发生但没有发生:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
DeleteCommentById(Convert.ToInt32(e.CommandArgument))
}
}
private void DeleteCommentById(int Comment_ID)
{
SqlConnection conn;
SqlCommand deleteCommentById;
string connectionString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
deleteCommentById = new SqlCommand("usp_deleteCommentById", conn);
deleteCommentById.CommandType = System.Data.CommandType.StoredProcedure;
deleteCommentById.Parameters.Add("@Comment_ID", System.Data.SqlDbType.Int);
deleteCommentById.Parameters["@Comment_ID"].Value = Comment_ID;
conn.Open();
deleteCommentById.ExecuteNonQuery();
conn.Close();
}
也许值得一提的是,如果我 "hard code" 我试图删除的行,那么它会起作用。例如,如果我在删除按钮中使用以下内容:
CommandArgument='44'
然后存储过程将按预期触发并影响第 44 行。
您在什么情况下尝试访问转发器按钮?
您将需要尝试在转发器项目中找到控件。
例如:
按钮 btn1 = (按钮)rptItem.FindControl("btn1");
最简单的方法是使用 ItemCommand 事件。
<%@ 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();
}
}
}
}
一切正常,但由于我没有指定仅 return 未被软删除的结果,所以一切都得到了 returned。小白失误,为以后学点东西!
===================
更新:29/06/2017
我正在尝试让我的转发器控件中的删除按钮按预期运行。目的是获取 "fire" 我的 MSSQL 数据库中存储过程的按钮。
感谢 Win 的深入回复,尽管我仍在努力解决问题。我承认我一开始可能无法正确表达我的问题。因此,我编辑了 post 以显示我现在拥有的代码。我相信我即将解决这个问题,并真诚地感谢任何帮助。
我的 *.*aspx 页面中的代码:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$
ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * FROM
[Comments] WHERE ([Ad_ID] = @Ad_ID) ORDER BY [CommentCreationDateTime] ASC">
再往下 *.*aspx 页面:
<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>
最后,我的代码背后的魔法应该发生但没有发生:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
DeleteCommentById(Convert.ToInt32(e.CommandArgument))
}
}
private void DeleteCommentById(int Comment_ID)
{
SqlConnection conn;
SqlCommand deleteCommentById;
string connectionString =
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
deleteCommentById = new SqlCommand("usp_deleteCommentById", conn);
deleteCommentById.CommandType = System.Data.CommandType.StoredProcedure;
deleteCommentById.Parameters.Add("@Comment_ID", System.Data.SqlDbType.Int);
deleteCommentById.Parameters["@Comment_ID"].Value = Comment_ID;
conn.Open();
deleteCommentById.ExecuteNonQuery();
conn.Close();
}
也许值得一提的是,如果我 "hard code" 我试图删除的行,那么它会起作用。例如,如果我在删除按钮中使用以下内容:
CommandArgument='44'
然后存储过程将按预期触发并影响第 44 行。
您在什么情况下尝试访问转发器按钮?
您将需要尝试在转发器项目中找到控件。 例如: 按钮 btn1 = (按钮)rptItem.FindControl("btn1");
最简单的方法是使用 ItemCommand 事件。
<%@ 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();
}
}
}
}
一切正常,但由于我没有指定仅 return 未被软删除的结果,所以一切都得到了 returned。小白失误,为以后学点东西!