您的位置:首页 > 编程语言 > ASP

小结:关于asp.net内置ajax功能的使用(scriptmanager/updatepanel)

2012-12-10 16:12 766 查看
说明:vs实现无刷新更新repeater里面的数据(点击repeater里面的按钮)

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>


  

==================================和谐的分割线=======================================

说明:如果是在updatepanel以外调用此方法,需要在page_load里载入,后台代码如

protected void Page_Load(object sender, EventArgs e)
{
sc1.RegisterAsyncPostBackControl(btn1);  //注意,这里的sc1是scriptmanager的ID,不是updatepanel的ID。
}

protected void btn1_Click(object sender, EventArgs e)
{
this.lbupdate.Text = "最新时间"+DateTime.Now.ToString();
this.up1.Update();
}


  

前台代码

<form id="form1" runat="server">
<asp:ScriptManager ID="sc1" runat="server"></asp:ScriptManager>

<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lbupdate" Text="更新时间" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>

<asp:Button ID="btn1" runat="server" Text="换时间" onclick="btn1_Click" />
</form>


  

参考:

VS2008 无刷新 Repeater 删除功能 ScriptManager UpdatePanel
/article/5699135.html

Repeater删除功能(CommandArgument,CommandName,ItemCommand)
http://gaoyanaigao.blog.163.com/blog/static/16929303620125410257448/

ScriptManager的使用
http://blog.163.com/zhuyisheng08@126/blog/static/60078777201041493656931/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐