目录
新闻表
设计页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="IndexTwo.aspx.cs" Inherits="HowToShow.IndexTwo" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Repeater ID="repArticles" runat="server" OnItemCommand="repArticles_ItemCommand" OnItemDataBound="repArticles_ItemDataBound">
<ItemTemplate>
<div>
<span><%# Eval("ATitle") %></span>
<span><%# Eval("AAuthor") %></span>
<span><%# Eval("ATime") %></span>
<span><%# Eval("AContent") %></span>
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="delete" CommandArgument='<%# Eval("AId") %>'>删除</asp:LinkButton>
</div>
</ItemTemplate>
<AlternatingItemTemplate>
<div style="background-color:aqua">
<span><%# Eval("ATitle") %></span>
<span><%# Eval("AAuthor") %></span>
<span><%# Eval("ATime") %></span>
<span><%# Eval("AContent") %></span>
<asp:LinkButton ID="lbtnDelete" runat="server" CommandName="delete" CommandArgument='<%# Eval("AId") %>'>删除</asp:LinkButton>
</div>
</AlternatingItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>
code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace HowToShow
{
public partial class IndexTwo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
private void Bind()
{
string sql = "select * from T_Article";
DataSet ds = SqlHelper.returnDataSet(sql, CommandType.Text, null);
repArticles.DataSource = ds.Tables[0];
repArticles.DataBind();
}
protected void repArticles_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Response.Write(int.Parse(e.CommandArgument.ToString()));
int AId = int.Parse(e.CommandArgument.ToString());
string sql = "delete from T_Article where AId=" + AId;
SqlHelper.ExecuteNonQuery(sql, CommandType.Text, null);
ClientScript.RegisterStartupScript(this.GetType(), "Alert",
"<script>alert('删除成功')</script>");
Bind();
}
}
protected void repArticles_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item|| e.Item.ItemType == ListItemType.AlternatingItem)//点击删除事件——跳出弹窗('真的要删除吗?')
{
((LinkButton)e.Item.FindControl("lbtnDelete")).Attributes.Add("onclick",
"javascript:return confirm('真的要删除吗?')");
}
}
}
}
自动生成的方法
右键repeater,点属性
点小闪电,双击事件名右边的空白。