VS2008 无刷新 Repeater 删除功能 ScriptManager UpdatePanel
前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RepeaterDelete.aspx.cs" Inherits="RepeaterDelete" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Repeater无刷新的删除</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div>
<table>
<tr>
<td align="right" colspan="4">
<asp:Button ID="btnDel" runat="server" Text="删除" OnClick="btnDel_Click" />
</td>
</tr>
<asp:Repeater ID="rtStudent" runat="server">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID="cbDel" runat="server" />
<!--主键-->
<asp:HiddenField ID="hfsID" runat="server" Value='<%#Eval("sID")%>' />
</td>
<td>
<!--姓名-->
<%#Eval("sName")%>
</td>
<td>
<!--年龄-->
<%#Eval("sAge")%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
后台代码:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
//导入
using System.Data.SqlClient;
public partial class RepeaterDelete : System.Web.UI.Page
{
/// <summary>
/// 页面加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
//是否为第一次加载
if (!IsPostBack)
{
Bind();
}
}
/// <summary>
/// 数据绑定,读数据库
/// </summary>
private void Bind()
{
SqlConnection con = new SqlConnection("server=.;database=AJAXWebSite;uid=sa;pwd=;");
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "SELECT * FROM Student";
com.CommandType = CommandType.Text;
con.Open();
SqlDataAdapter sda = new SqlDataAdapter();
DataSet ds = new DataSet();
sda.SelectCommand = com;
sda.Fill(ds);
rtStudent.DataSource = ds.Tables[0].DefaultView;
rtStudent.DataBind();
con.Close();
}
/// <summary>
/// 单击删除按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDel_Click(object sender, EventArgs e)
{
//初始化需要删除的ID集合
string ID = "";
//循环
for (int i = 0; i < rtStudent.Items.Count; i++)
{
//查找单选框按钮
CheckBox cb = (CheckBox)rtStudent.Items[i].FindControl("cbDel");
//隐藏控件,值为表的主键
HiddenField hf = (HiddenField)rtStudent.Items[i].FindControl("hfsID");
//判断单选框是否被选择
if (cb.Checked)
{
//主键之间用逗号隔开
ID = ID + hf.Value + ",";
}
}
//调用删除函数
Delete(ID);
//数据重新绑定
Bind();
}
/// <summary>
/// 删除表中的记录
/// </summary>
/// <param name="ID">需要删除的ID集合</param>
private void Delete(string ID)
{
ID = ID.Substring(0, ID.Length - 1);
SqlConnection con = new SqlConnection("server=.;database=AJAXWebSite;uid=sa;pwd=;");
con.Open();
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandText = "DELETE FROM Student WHERE sID in (" + ID + ")";
com.CommandType = CommandType.Text;
com.ExecuteNonQuery();
con.Close();
}
}
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
</ContentTemplate>
</asp:UpdatePanel>
</form>
这是实现无刷新的代码,VS2008自带了AJAX。
附上SQL语句:
CREATE DATABASE AJAXWebSite
USE AJAXWebSite
CREATE TABLE Student
(
sID int identity(1,1) primary key,/*主键,从1开始自增*/
sName varchar(20),/*姓名*/
sAge int/*年龄*/
)
INSERT Student(sName,sAge) VALUES('张三',22)
INSERT Student(sName,sAge) VALUES('李四',23)
INSERT Student(sName,sAge) VALUES('王五',22)
INSERT Student(sName,sAge) VALUES('赵六',24)
感慨:一开始写代码的时候忘记判断是否是第一次加载,别的代码都是对的,可就是不知道哪里出错了。后来加了判断,结果就正确了。
IsPostBack相当的重要。
文章中的代码比较乱,只是想实现删除功能,没考虑代码的合理性。