WebForm+一般处理程序+Ajax聊天

#### 很容易理解 插入数据 到数据库 在使用 setInterval() 读取数据 显示在 页面中 好了 不废话 直接上代码 不会的 可以加我 微信 Jth11163
## 效果图片


## WebForm + Jquer + Css 代码
```
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication15.WebForm1" %> <!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>
<script src="jquery-3.3.1.js" ></script>
<style>
*{
list-style:none;
padding:0px;
margin:0px;
}
ul{
line-height:20px;
}
li{
padding-left:20px;
text-align:left;
margin-bottom:10px;
color:blue;
}
#form1{
width:500px;
height:550px;
margin:auto;
border:1px solid black;
}
.Box{
width:500px;
height:500px;
box-sizing:border-box;
border:1px solid black;
overflow:auto;
}
span{
color:#808080;
padding-left:15px;
font-size:13px;
}
p{
color:black; font-size:16px;
}
#Button1{
background-color:blue;
width:80px;
height:45px;
line-height:45px;
color:white;
font-size:15px;
text-align:center;
border:none;
border-radius:5px;
cursor:pointer;
}
#TextBox1{
width:399px;
height:50px;
}
</style>
<script> $(function () {
$('#TextBox1').focus();
//发送
function Send() {
$.ajax({
url: "Handler2.ashx",
data: { Content: $('#TextBox1').val() },
type: "POST",
success: function (data) { }
});
}
$('input').keypress(function (e) {
if (e.which == 13) {
Send();
$('#TextBox1').val("");
$('#TextBox1').focus();
}
});
setInterval(function () {
$.ajax({
url: "Handler1.ashx",
type: "POST",
success: function (data) {
$('.ul').children().remove();
$('.ul').append(data);
}
});
$('.Box').scrollTop($('.ul').outerHeight());
}, 1);
$('#Button1').click(function () {
Send();
$('#TextBox1').val("");
$('#TextBox1').focus();
})
})
</script>
</head>
<body>
<form autopostback="false" id="form1" runat="server">
<div class ="Box">
聊天 在线人数:<span id ="Count" runat="server"></span>
<ul class ="ul">
</ul>
</div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<%--<asp:Button ID="Button1" AutoPostBack="false" runat="server" onclick="Button1_Click" Text="发送" />--%>
<span>
<input type="button" id ="Button1" value ="发送"/>
</span>
</form> </body>
</html>
```
# Ashx(一般处理程序) 代码
### 生成 随机名字
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data; namespace WebApplication15
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{
DBHelper Helper = new DBHelper();
//处理
List<string> ChatContent = new List<string>();
string[] Arr = new string[];
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string J = "";
string sql = "select * from Content order by SendTime Asc";
DataSet Ds = Helper.Data(sql);
if (Ds.Tables[].Rows != null)
{
foreach (DataRow Row in Ds.Tables[].Rows)
{ J += "<li>" + "<p>" + Row["UserName"].ToString() + "<span>" + Row["SendTime"].ToString() + "</span>" + "</P>" + Row["Content"].ToString() + "</li>"; }
}
context.Response.Write(J); Helper.Connection.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
```
### 插入读取数据
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
namespace WebApplication15
{
/// <summary>
/// Handler2 的摘要说明
/// </summary>
public class Handler2 : IHttpHandler
{
DBHelper H = new DBHelper();
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string Cont = context.Request["Content"].ToString();
if (Cont != null)
{
Get(context,Cont);
}
}
public void Get(HttpContext context,string Cont)
{ string sql = "Insert Into Content values(@name,@time,@con)";
SqlParameter[] par = new SqlParameter[] {
new SqlParameter("@con", Cont),
new SqlParameter("@name", Global.Name_),
new SqlParameter("@time", DateTime.Now) };
H.QuerOne(sql, par, CommandType.Text);
} public bool IsReusable
{
get
{
return false;
}
}
}
}
```
原著 希望对你有所帮助 共同学习哈哈哈哈哈哈 啊哈哈哈
qq 2456314589
WebForm+一般处理程序+Ajax聊天的更多相关文章
- PHP+ajax聊天室源码!支持长轮循跟定时请求两种
var lastID = "1";//声明上次取回的消息的ID var isposted = false; var mGetTime;//设置setTimeout的返回值 // ...
- webform 中使用ajax
常用的方式有 js –> WebService , js->*.ashx, js->WebAPI, js->MVC Controller->Action. 前两种就不说 ...
- WebForm页面使用Ajax
AJAX:”Asynchronous JavaScript and XML” 中文意思:异步JavaScript和XML.指一种创建交互式网页应用的网页开发技术.AJAX并非缩写词,而是由Jesse ...
- 在webform中使用ajax
如果你用过Asp.net webform, 说明你也算是.NET 开发的老兵了.WEBform应该是2011-2013左右,当时还用visual studio 2005. visual studio ...
- WebForm下的$.ajax中contentType: “application/json” 的用法
不使用contentType: “application/json”则data可以是对象 $.ajax({ url: actionurl, type: "POST", datTyp ...
- 一般处理程序,ajax
一般处理程序调用session: 在.aspx.cs页中读写Session都是Session["***"]就可以获取或者写入.但是在一般处理程序也就是ashx页面中,再这样写的话, ...
- Ajax聊天
结构: index.html <!DOCTYPE html> <html> <head> <title>index.html</title> ...
- ASP.NET webform基于Jquery,AJAX的三级联动
主要html代码 <select id="province"> <option value="0">--请选择省份--</opti ...
- Ajax PHP JavaScript MySQL实现简易的无刷新在线聊天室
思路 消息显示区 发消息 板块 消息显示 消息发送 优化 显示非重复性的数据 优化显示 加上滚动条 每次都显示最新消息 完整代码 前端代码 数据库表结构 服务器端代码 总结与展望 总结 展望 为更好的 ...
随机推荐
- Java Applet 与Servlet之间的通信
1 Applet对Servlet的访问及参数传递的实现 2.1.1创建URL对象 在JAVA程序中,可以利用如下的形式创建URL对象 URL servletURL = new URL( "h ...
- Windows上安装配置SSH教程(5)——win10下使用Cygwin+Expect自动登陆ssh
1.安装Cygwin,安装上Tcl和Expect两个工具. 可以使用apt-cyg命令安装,也可以在安装Cygwin的时候选中这两个包. 命令安装的话使用下面的两个命令: apt-cyg instal ...
- visual studio2015中开发python
之前下载了visual studio2017但是发现很不好用,不如使用matlab与visual studio混合编程就根本找不到visual studio,只有下了visual studio2015 ...
- MySQL 复制 - 性能与扩展性的基石 3:常见问题及解决方案
主备复制过程中有很大可能会出现各种问题,接下来我们就讨论一些比较普遍的问题,以及当遇到这些问题时,如何解决或者预防问题发生. 1 数据损坏或丢失 问题描述:服务器崩溃.断电.磁盘损坏.内存或网络错误等 ...
- Vue.js-09:第九章 - 组件基础再探(data、props)
一.前言 在上一章的学习中,我们学习了 Vue 中组件的基础知识,知道了什么是组件,以及如何创建一个全局/局部组件.不知道你是否记得,在上一章中,我们提到组件是一个可以复用的 Vue 实例,它与 Vu ...
- Windows下Goland的Terminal设置为Git Bash
Windows下Terminal默认的使用的是系统自带的cmd,功能实在太少,用起来远不如Git Bash来得方利和高效.其实要在Goland的Terminal中使用Bash设置起来也很简单,设置位置 ...
- .NETCore 千星项目模块化开发框架 SimplCommerce 详解
SimplCommerce 是 github 上过千星的.netcore 商城示例项目,本文详解他的模块化框架现实思路,其业务(如产品.订单)不作介绍.因作者文笔水平很差,它又很值得学习和推荐,就算不 ...
- 如何在ASP.NET Core中自定义Azure Storage File Provider
文章标题:如何在ASP.NET Core中自定义Azure Storage File Provider 作者:Lamond Lu 地址:https://www.cnblogs.com/lwqlun/p ...
- docker同时删除多个容器
查询所有容器 sudo docker ps -a 同时删除多个符合筛选条件的容器,例如删除状态为“exited”的容器 docker rm $(docker container ls -f " ...
- js 原型,原型链,原型链继承浅析
对于网上的关于原型,原型链和原型链继承的晦涩语言说明就不累赘了,复制粘贴过来再解释一遍怕自己也整蒙了,本人最怕空气突然安静,四目对视,大眼对小眼,一脸懵逼. 我们先看下面