③ 聊天信息拖动停止聊天信息功能
相对于前面两个功能的实现,这个功能多了点步骤,但原理还是一致的。由于系统中并没有控制Timer控件的启用与关闭的控件,因此我们需要先在Max2区域中添加两个按钮Button1(关闭Timer)与Button2(启用Timer),然后设置其style=”display:none”,并为两个按钮添加如下代码:
protected void Button1_Click(object sender, EventArgs e)
{
this.Timer2.Enabled = true; //启用
}
protected void Button2_Click(object sender, EventArgs e)
{
this.Timer2.Enabled = false; //停止
}
现在我们来具体的看一下如何在拖动文本框时调用Button2按钮,在客户端并没有拖动文本框的事件,那我们不得不求助于onmousemove事件,当鼠标在上面移动时触发,如果我们将调用服务器的代码直接置入事件中,我们就会因移动不断的去调用服务器,这样就会加重服务器的负载,因此我们必须在JS中通过一定的Flag值来控制是否得得调用服务器程序。代码如下:
首先设定Flag: var Timer2Flag=1;
再建立两个JS函数:
{
{
__doPostBack("AnchorCall1$Button2","");
Timer2Flag=0;
}
}
{
__doPostBack("AnchorCall1$Button1","");
Timer2Flag=1;
}
④ 保存聊天记录
系统的聊天功能是通过参数传递给一个新的页面并打开新页面,新页面根据URL参数从数据库中读出相关的聊天信息。为了打开新的页面,我们首先给“保存记录”添加客户端事件,在添加的过程中我们将此访问者的呢称、登陆时间、当前URL传递过去,如下:
this.ImageButton2.Attributes.Add("onclick", "window.open('Anchor_ChatRecord.aspx?url=" + Request.Url.ToString() + "&begindate=" + this.LoginTime.Text + "&rndname="+this.RndName.Text+"')");
这样我们就可以很方便的打开新页面,而且能够将关键值传递给新页面,然后我们在新的页面中输入如下代码,以方便调用数据库信息,首先在新页面(Anchor_ChatRecord.aspx)添加一个Label控件,用于显示聊天信息。然后在后台Page_load中输入如下代码:
if (Request["url"] != null && Request["begindate"] != null && Request["rndname"] != null)
{
string TempStr = "";
string url = Request["url"].ToString();
string begindate = Request["begindate"].ToString();
string rndname = Request["rndname"].ToString();
string str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["AnchorCallDateBase"]);
OleDbConnection conn = new OleDbConnection(str);
conn.Open();
string sql = "select * from chatroom where frompage='"+url+"' and datediff('s','" + begindate + "',dateandtime)>0 order by id desc";
OleDbCommand cmd = new OleDbCommand(sql, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
].ToString() + "<font color='#888888'>[" + DateTime.Parse(ds.Tables[0].Rows[i]["dateandtime"].ToString()).ToShortTimeString() + "]</font><br/>";
this.Label1.Text += TempStr;
}
}
cmd.Dispose();
conn.Close();
conn.Dispose();
}
else
{
}
五、Visual Studio 2005开发步骤