将数据库行的更新和删除功能添加到中继器

时间:2016-11-16 14:51:06

标签: c# html css asp.net sql-server

我正在使用转发器来显示SQL Server数据库中的数据,并为数据库中的每一行添加了按钮。

以下是填写转发器的代码:

 SqlConnection connR;
    string connectionStringR = ConfigurationManager.ConnectionStrings[
        "BallinoraDBConnectionString1"].ConnectionString;
    connR = new SqlConnection(connectionStringR);
    SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Events", connR);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    Repeater1.DataSource = dt;
    Repeater1.DataBind();

这是Repeater代码:

 <asp:Repeater ID="Repeater1" runat="server">
                        <ItemTemplate>
                            <div>
                                <table>
                                    <tr><th><%#Eval("Event_Title")%></th><td><button>Edit</button></td><td><button>Delete</button></td></tr>
                                    <tr><td>Event Group ID</td><td><%#Eval("Event_Group_Id") %></td></tr>
                                    <tr><td>Event Type</td><td><%#Eval("Event_Type")  %></td></tr>
                                    <tr><td>Event ID</td><td><%#Eval("Event_Id")  %></td></tr>
                                    <br />
                                </table>
                            </div>
                        </ItemTemplate>
                    </asp:Repeater>

Here是用户界面 我不知道应该在哪里添加每行的编辑和删除代码,因为显示的事件数量基于数据库中的行数。

2 个答案:

答案 0 :(得分:0)

首先,您可能应该稍微改进一下您的问题,并按照评论中的提示进行操作,但您可能希望开始使用的是:

<button onclick="delFunc('bowlingId');">Delete Bowling</button>

这将为您提供按钮上的文本,并添加onclick事件,因为您必须编写脚本函数。这意味着您必须从查询中获取一些Id以标识数据库中的每个唯一事件。如果你有这个,你可以编写一些代码来删除一个唯一的行,并从页面上的脚本调用该函数,如何做到这一点取决于你正在使用什么样的后端,但一般的想法可能是这样的这样:

<head>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
 <script>
  function delFunc(id) {
   $.ajax({
     method: "POST",
     url: "deleteEvent.php",
     data: id
   })
   alert('deleted '+ id +'!');
  }
 </script>
</head>

您需要使用jQuery for the $.ajax来电。

嗯,这有望让你至少开始:)

答案 1 :(得分:0)

您必须在 gridview 中执行此操作。

如果您仍然希望在asp:label中完成此操作,那么我建议您做一件事。使用锚点代替按钮,并使用下面的querystring链接到同一页面

lblGroupOneEvents.Text += "<div> <table> <tr><td>" + reader["Event_Title"] + "</td>" + "</tr><tr><td><a href='samepage.aspx?remove=" + reader["id"] + "'>Remove</a></td></tr></table> </div>";
页面加载中的

处理删除功能

if(Request.QueryString["remove"]!=null)
{
   //your delete code here
}