.net中即时消息发送的实现……
点击次数:18 次 发布日期:2008-11-26 13:52:40 作者:源代码网
|
源代码网推荐 数据库设计: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: 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="发送" 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="继续发送…" 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">直接写入内容点击发送即可!</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">发信日期:</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">内容:</asp:Label> 源代码网推荐 </FONT> 源代码网推荐 </form> 源代码网推荐 </body> 源代码网推荐 </HTML> 源代码网推荐 以上代码在bata2环境下调试成功. 源代码网推荐 源代码网推荐 特别感谢:cheery_ke提供思路! 源代码网推荐 朋友一生一起走,多一个朋友就多一分收获! 源代码网推荐 源代码网推荐 做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。 源代码网推荐 源代码网供稿. |
