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

说明: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="换时间" οnclick="btn1_Click" />
    </form>

  

 

参考:

VS2008 无刷新 Repeater 删除功能 ScriptManager UpdatePanel
http://www.cnblogs.com/pwm_1987/archive/2010/05/03/1726402.html

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

ScriptManager的使用
http://blog.163.com/zhuyisheng08@126/blog/static/60078777201041493656931/

 

 

转载于:https://www.cnblogs.com/ishibin/archive/2012/12/10/2811562.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值