| 用了我一下午的时间终于写完并整理好了利用.net来发送即时消息的材料(当然了,还有上午的数据库设计:) 数据库设计:info表:id fromstu_id tostu_id content term 其中id是主键,fromstu_id是发送信息的用户的学号(这是和我做的学友录连在一起的),tostu_id是接受信息的用户的学号,content是消息的内容,term是判断是否为新消息。 下面的代码家在校友录中的if not ispostback中 '/////////////////////判断是否有新留言,将自动弹出页面 这里还要将页面的刷新时间设置一下,以便可以循环的读取信息。 dim mysql as string = "select * from info where tostu_id=@myid and term=1" dim comm as sqlcommand = new sqlcommand(mysql, conn) comm.parameters.add(new sqlparameter("@myid", sqldbtype.int, 4)) comm.parameters("@myid").value = session("stu_id") dim dr as sqldatareader conn.open() dr = comm.executereader if dr.read then response.write("<script language=javascript>window.open('info.aspx',','height=330,width=560,status=no,location=no,toolbar=no,directories=no,menubar=no')</script>") end if dr.close() comm.cancel() 下面的代码是用来发送即时消息的页面,其中里面分了两个部分,一个是用来回复的,一个是用来专门发送的,两个的页面稍有区别,仔细看一下就会明白的:) 下面是所有的代码:codebehind部分 public sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load if not ispostback then dim tostu_id as string = request.querystring("tostu_id") if tostu_id = "" then '//////////////////当回复留言时 dim sql as string = "select a.*,b.nick from info a,pwd b where a.fromstu_id=b.stu_id and a.tostu_id='" & session("stu_id") & "' and a.term=1" dim comm as sqlcommand = new sqlcommand(sql, conn) dim dr as sqldatareader conn.open() dr = comm.executereader while dr.read label3.text = dr.item("nick") label4.text = dr.item("tim") label5.text = dr.item("content") textbox1.text = dr.item("nick") textbox3.text = dr.item("fromstu_id") textbox1.enabled = false label8.visible = false end while dr.close() comm.cancel() '//////////////////////更新留言使留言属性为已阅读过 dim sql_1 as string = "update info set term=0 where tostu_id='" & session("stu_id") & "' and term=1 and tim='" & label4.text & "'" comm = new sqlcommand(sql_1, conn) comm.executenonquery() else '////////////////////当发送留言时 dim mysql as string = "select nick from pwd where stu_id='" & tostu_id & "'" dim comm as sqlcommand = new sqlcommand(mysql, conn) dim dr as sqldatareader conn.open() dr = comm.executereader while dr.read textbox1.text = dr.item("nick") end while textbox1.enabled = false label3.text = "" label4.text = "" label5.visible = false label8.visible = true label6.visible = false label7.visible = false label9.visible = false dr.close() end if end if end sub '/////////////////书写提交消息事件 public sub button1_click(byval sender as system.object, byval e as system.eventargs) handles button1.click dim tostu_id as string = request.querystring("tostu_id") if tostu_id = "" then '/////////////////////////当回复留言时 conn.open() dim sql as string = "insert into info(fromstu_id,tostu_id,content,term,tim) values(@fromstu_id,@tostu_id,@content,@term,@tim)" dim comm as sqlcommand = new sqlcommand(sql, conn) comm.parameters.add(new sqlparameter("@fromstu_id", sqldbtype.int, 4)) comm.parameters("@fromstu_id").value = session("stu_id") comm.parameters.add(new sqlparameter("@tostu_id", sqldbtype.int, 4)) comm.parameters("@tostu_id").value = textbox3.text comm.parameters.add(new sqlparameter("@content", sqldbtype.varchar, 200)) comm.parameters("@content").value = textbox2.text comm.parameters.add(new sqlparameter("@term", sqldbtype.int, 4)) comm.parameters("@term").value = "1" comm.parameters.add(new sqlparameter("@tim", sqldbtype.char, 20)) comm.parameters("@tim").value = date.now comm.executenonquery() textbox2.text = "" else '/////////////////////////当发送留言时 conn.open() dim sql as string = "insert into info(fromstu_id,tostu_id,content,term,tim) values(@fromstu_id,@tostu_id,@content,@term,@tim)" dim comm as sqlcommand = new sqlcommand(sql, conn) comm.parameters.add(new sqlparameter("@fromstu_id", sqldbtype.int, 4)) comm.parameters("@fromstu_id").value = session("stu_id") comm.parameters.add(new sqlparameter("@tostu_id", sqldbtype.int, 4)) comm.parameters("@tostu_id").value = tostu_id comm.parameters.add(new sqlparameter("@content", sqldbtype.varchar, 200)) comm.parameters("@content").value = textbox2.text comm.parameters.add(new sqlparameter("@term", sqldbtype.int, 4)) comm.parameters("@term").value = "1" comm.parameters.add(new sqlparameter("@tim", sqldbtype.char, 20)) comm.parameters("@tim").value = date.now comm.executenonquery() textbox2.text = "" end if response.write("<script language=javascript>alert('发送成功!')</script>") end sub '////////////////////返回继续发送 private sub button2_click(byval sender as system.object, byval e as system.eventargs) handles button2.click response.redirect("boaman.aspx") end sub end class 页面部分: <%@ page language="vb" autoeventwireup="false" codebehind="info.aspx.vb" inherits="_99re1.info"%> <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title></title> <meta content="microsoft visual studio.net 7.0" name="generator"> <meta content="visual basic 7.0" name="code_language"> <meta content="javascript" name="vs_defaultclientscript"> <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetschema"> </head> <body background="image/bg.gif" ms_positioning="gridlayout"> <form id="form1" method="post" runat="server"> <font face="宋体"> <asp:image id="image3" style="z-index: 111; left: 141px; position: absolute; top: 312px" runat="server" width="221px" height="98px" imageurl="image/99re1-1.gif"></asp:image> <asp:textbox id="textbox1" style="z-index: 101; left: 73px; position: absolute; top: 123px" runat="server" bordercolor="navy" borderwidth="1px"></asp:textbox> <asp:label id="label1" style="z-index: 102; left: 26px; position: absolute; top: 127px" runat="server" width="42px" height="18px" font-size="x-small" forecolor="navy" font-bold="true">发往:</asp:label> <asp:label id="label2" style="z-index: 103; left: 26px; position: absolute; top: 156px" runat="server" font-size="x-small" forecolor="navy" font-bold="true">内容:</asp:label> <asp:textbox id="textbox2" style="z-index: 104; left: 73px; position: absolut
本文关键:.net中即时消息发送的实现……
相关方案
|