用了我一下午的時間終于寫完并整理好了利用.net來發(fā)送即時消息的材料(當然了,還有上午的數(shù)據(jù)庫設計:)
數(shù)據(jù)庫設計:info表:id fromstu_id tostu_id content term
其中id是主鍵,fromstu_id是發(fā)送信息的用戶的學號(這是和我做的學友錄連在一起的),tostu_id是接受信息的用戶的學號,content是消息的內(nèi)容,term是判斷是否為新消息。
下面的代碼家在校友錄中的if not ispostback中
'/////////////////////判斷是否有新留言,將自動彈出頁面
這里還要將頁面的刷新時間設置一下,以便可以循環(huán)的讀取信息。
dim mysql as string = "select * from info where [email protected] 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()
下面的代碼是用來發(fā)送即時消息的頁面,其中里面分了兩個部分,一個是用來回復的,一個是用來專門發(fā)送的,兩個的頁面稍有區(qū)別,仔細看一下就會明白的:)
下面是所有的代碼: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
'////////////////////當發(fā)送留言時
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
'/////////////////////////當發(fā)送留言時
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('發(fā)送成功!')</script>")
end sub
'////////////////////返回繼續(xù)發(fā)送
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">發(fā)往:</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">內(nèi)容:</asp:label>
<asp:textbox id="textbox2" style="z-index: 104; left: 73px; position: absolute; top: 154px" runat="server" textmode="multiline" width="449px" height="74px" bordercolor="navy" borderwidth="1px" maxlength="200"></asp:textbox>
<asp:button id="button1" style="z-index: 105; left: 357px; position: absolute; top: 252px" runat="server" width="50px" height="20px" text="發(fā)送" bordercolor="navy" borderwidth="1px" backcolor="#ffe0c0"></asp:button>
<asp:button id="button2" style="z-index: 106; left: 176px; position: absolute; top: 253px" runat="server" width="87px" height="20px" text="繼續(xù)發(fā)送…" bordercolor="navy" borderwidth="1px" backcolor="#ffe0c0"></asp:button>
<asp:label id="label3" style="z-index: 107; left: 75px; position: absolute; top: 10px" runat="server" width="135px" height="6px" font-size="small">label</asp:label>
<asp:label id="label4" style="z-index: 108; left: 300px; position: absolute; top: 9px" runat="server" width="219px" height="13px" font-size="small">label</asp:label>
<asp:label id="label5" style="z-index: 109; left: 73px; position: absolute; top: 40px" runat="server" width="447px" height="71px" font-size="x-small" bordercolor="slategray" borderwidth="1px">label</asp:label>
<asp:label id="label6" style="z-index: 110; left: 26px; position: absolute; top: 12px" runat="server" font-size="x-small" forecolor="red" font-bold="true">來自:</asp:label>
<asp:textbox id="textbox3" style="z-index: 112; left: 247px; position: absolute; top: 122px" runat="server" visible="false"></asp:textbox>
<asp:label id="label8" style="z-index: 113; left: 116px; position: absolute; top: 55px" runat="server" height="33px" width="327px" font-bold="true" forecolor="navy" font-size="large" font-names="方正姚體" font-underline="true">直接寫入內(nèi)容點擊發(fā)送即可!</asp:label>
<asp:label id="label7" style="z-index: 114; left: 225px; position: absolute; top: 12px" runat="server" height="15px" width="71px" font-bold="true" forecolor="red" font-size="x-small">發(fā)信日期:</asp:label>
<asp:label id="label9" style="z-index: 115; left: 25px; position: absolute; top: 41px" runat="server" font-bold="true" forecolor="red" font-size="x-small">內(nèi)容:</asp:label>
</font>
</form>
</body>
</html>
以上代碼在bata2環(huán)境下調(diào)試成功.
特別感謝:cheery_ke提供思路!
朋友一生一起走,多一個朋友就多一分收獲!