ASP.NET 实现站内信功能(点对点发送,管理员群发)
正好这段时间在研究这个功能,还是得感谢这位大神,没有他的引路,我就不可能把站内信做出来。
http://www.cnblogs.com/grenet/archive/2010/03/08/1680655.html
哈哈,再次感谢。
我们先来说说单点传送站内信,所谓的单点传送,就是用户与用户之间的短信发送,这里的用户可以是一个人,也可以是多个人,
上面的文章大家如果看了的话,想必有一个疑问,就是用户的阅读状态怎么定义?
我这里给大家一种解决方案,再建立一张表,也就是说,建立一张用户阅读状态表,因为每个用户的阅读状态都是不同的,如果照上面的方法,
肯定是不行的,一个人改变了状态,另外的人也改变了。
上面的是我建立的3张表,具体的SQL如下,包含注释
--短信表 create table cms_message ( id varchar(50) primary key, senduserid varchar(50), --发送用户的ID recuserid varchar(50), --接收用户的ID messageid varchar(50), --短信ID Msgstatus varchar(10) --查看状态(是否被查询) ) --短信内容表 create table cms_messagetext ( idc varchar(50) primary key, messagetext text, --站内信内容 pdate varchar(50) --站内信发送时间 ) --阅读状态表 create table cms_message_read ( uniqueid varchar(50) primary key, idm varchar(50) ,--和MESSAGEID相同 userid varchar(50), --用户ID flag varchar(5) --阅读状态:0未读,1:已读,2:删除 )
大家也许会问一个问题,你已经在阅读状态表里定义了阅读状态,为什么还要短信表里再定义一次呢?这个其实是有用的,尤其是在发件箱功能里要标记自己的信件的时候,稍后会提到哦。
先让大家来看看主界面。
其中有发件箱,未读信息和已读消息。
发件箱的功能就是查看自己已经发送的信件,未读和已读信息就是表示自己是否已经点击过短信。
下面给大家来看一下发送短信的界面。
下面是普通的点对点发送。
下面是短信群发的界面,我这里的群发是选择发送给某个用户组。
我先把代码都放出来,然后我们再来慢慢分析。
其中有用到公司的框架,大家知道意思就行,没必要过分纠结。
这是发送短信的窗口的代码
<%@ Page Language="C#" MasterPageFile="~/Core/Public/WebControls/Main.master" AutoEventWireup="true" CodeFile="AdminSend.aspx.cs" Inherits="MsgSend_AdminSend" %> <asp:Content ID="cssContent" ContentPlaceHolderID="cssContent" runat="server"> <style type="text/css"> .Row { width:100%; border-bottom:1px solid #BBE9FF; font-size:12px; font-family:宋体; height:30px; } .Row .Caption { float:left; width:80px; text-align:left; padding-left:20px; padding-top:8px; } .Row .Data { float:left; text-align:left; padding-top:2px; width:317px; } .Row .Validate { float:left; color:red; padding-top:8px; } .Row .Data .Field { width:256px; float:left; } .Row .Data .Select { float:left; padding-left:1px; padding-top:1px; } .Bottom { width:100%; height:40px; border-bottom:1px solid #BBE9FF; font-size:12px; font-family:宋体; } .Bottom .Button { margin-top:5px; float:right; text-align:left; padding-right:10px; } </style> </asp:Content> <asp:Content ID="bodyContent" ContentPlaceHolderID="bodyContent" runat="server"> <div class="Row"> <div class="Caption">收件人 :</div> <div class="Data"> <asp:TextBox runat="server" ID="recieveid" style="width:311px;height:17px;"></asp:TextBox> <%--下拉列表,仅仅用户管理员可用--%> <asp:DropDownList runat="server" ID="ddlUserGroup"></asp:DropDownList> </div> <div class="Validate"> *</div> </div> <div class="Row" id="divMemo"> <div class="Caption">内容 :</div> <div class="Data"> <asp:TextBox TextMode="MultiLine" runat="server" ID="msgText" style="width:311px;"></asp:TextBox></div> <div class="Validate"> *</div> </div> <div class="Bottom"> <div class="Button"> <asp:Button ID="sendMsg" runat="server" Text="发送" OnClick="sendMsg_Click" CssClass="buttonblue_100_27" OnClientClick="return ValidateEmpty();" /> <asp:Button ID="cancel" runat="server" Text="取消" CssClass="buttonblue_100_27" OnClientClick="return winClose()" /> </div> </div> </asp:Content> <asp:Content runat="server" ID="jsContent" ContentPlaceHolderID="jsContent"> <script type="text/javascript"> $( document ).load( function() { var bottom = $( "#divMemo" ); bottom.height( $( document.body ).height() - 120 - 15 ); bottom.children().eq( 0 ).css( "margin-top" , 70 ); bottom.children().eq( 1 ).height( bottom.height() - 15 ); bottom.children().eq( 1 ).children().eq( 0 ).height( bottom.children().eq( 1 ).height() ); } ); //内容判断不能为空 function ValidateEmpty() { if($("#<%=this.msgText.ClientID%>").val()=="") { alert("请输入短信内容"); return false; } else { return true; } } </script> </asp:Content>
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; public partial class MsgSend_AdminSend :cs.Common.Web.BasePage { protected void Page_Load(object sender, EventArgs e) { //如果是查询信件的话 if (Request["id"] != null && Request["id"] != "") { //recieveid.Visible = false; //如果是单纯的查询信件的话,隐去接收人 recieveid.Enabled = false; DataSet setRequest = this.DataManager.Fill("select * from cms_message a,cms_messagetext b where a.messageid=b.idc and a.id='" +Request["id"]+ "'"); foreach(DataRow row in setRequest.Tables[0].Rows) { msgText.Text= row["messagetext"].ToString(); //短信内容 sendMsg.Text = row["recuserid"].ToString(); //接收人 } msgText.Enabled = false; sendMsg.Enabled = false; ddlUserGroup.Visible = false; } ddlUserGroup.Visible = false; //群发短信 if (Request["group"] == "yes"&&!IsPostBack) { recieveid.Visible = false; ddlUserGroup.Visible = true; //显示下拉 //绑定数据 //不为顶级栏目 DataSet set = this.DataManager.Fill("select * from t_sys_org where orgid!='Org_05d1a0697fbf4559afcf4914f9'"); ddlUserGroup.DataSource = set; //数据源 ddlUserGroup.DataTextField = "name"; ddlUserGroup.DataValueField = "orgid"; ddlUserGroup.DataBind(); } } //发送短信 //分为群发和点对点发送,如果下拉列里面的值为NULL,就是点对点发送,否则为短信群发 protected void sendMsg_Click(object sender, EventArgs e) { //点对点发送 if (ddlUserGroup.SelectedItem == null) { bool suc_Insert = false; bool suc_InsertText = false; string id = Guid.NewGuid().ToString(); //新产生的ID string currentId = cms.Web.AppContext.CuurentUser.ID; //当前用户的ID string recieveId = this.recieveid.Text; //收信人ID string messageId = Guid.NewGuid().ToString(); //信件ID string messageStatus = "0"; //0:未读,1:已读,2:删除 string pdate = DateTime.Now.ToString(); //站内信发送时间 string messagetext = this.msgText.Text; //信件内容 //发送短信的SQL其实是在数据库里插入3条记录而已,删除是假删除。 //插入cms_message的SQL string sqlInsert = "insert into cms_message values('" + id + "','" + currentId + "','" + recieveId + "','" + messageId + "','" + messageStatus + "')"; //插入cms_messagetext表的SQL string sqlInsertText = "insert into cms_messagetext values ('" + messageId + "','" + messagetext + "','" + pdate + "')"; if (!userIsExists(recieveId)) { cs.Common.Web.AjaxServer.Msg("用户名不存在,请检查用户名!如有多个用户名,请用英文逗号分隔!", ""); } else { //插入2张表 suc_Insert = this.DataManager.Execute(sqlInsert); suc_InsertText = this.DataManager.Execute(sqlInsertText); //插入cms_message_read 阅读状态表的记录,因为这里要遍历插入的人,所以要用循环 string[] recIds = recieveId.Split(','); for (int i = 0; i < recIds.Length; i++) { string uniqueId = Guid.NewGuid().ToString(); //新建一个不重复的ID string sqlInsertStatus = "insert into cms_message_read values('" + uniqueId + "','" + messageId + "','" + recIds[i] + "','" + messageStatus + "')"; this.DataManager.Execute(sqlInsertStatus); } //yangliweng,baojunyi,cgb,clj if (suc_Insert && suc_InsertText) { cs.Common.Web.AjaxServer.Msg("发送成功!", "return winClose();"); } else { cs.Common.Web.AjaxServer.Msg("发送失败!", "return winClose()"); } } } //群发 else { bool suc_Insert = false; bool suc_InsertText = false; string id = Guid.NewGuid().ToString(); //新产生的ID string currentId = cms.Web.AppContext.CuurentUser.ID; //当前用户的ID string messageId = Guid.NewGuid().ToString(); //信件ID string messageStatus = "0"; //0:未读,1:已读,2:删除 string pdate = DateTime.Now.ToString(); //站内信发送时间 string messagetext = this.msgText.Text; //信件内容 string recieveId = "";//收信人ID string orgId = ddlUserGroup.SelectedValue; //选中的部门的ID DataSet setUserByOrgId = this.DataManager.Fill("select * from t_sys_user where orgid='"+orgId+"'"); foreach (DataRow Row in setUserByOrgId.Tables[0].Rows) { recieveId+=","+ Row["id"].ToString(); } recieveId = recieveId.Substring(1); //截取字符串 //Response.Write(recieveId); //发送短信的SQL其实是在数据库里插入3条记录而已,删除是假删除。 //插入cms_message的SQL string sqlInsert = "insert into cms_message values('" + id + "','" + currentId + "','" + recieveId + "','" + messageId + "','" + messageStatus + "')"; //插入cms_messagetext表的SQL string sqlInsertText = "insert into cms_messagetext values ('" + messageId + "','" + messagetext + "','" + pdate + "')"; if (!userIsExists(recieveId)) { cs.Common.Web.AjaxServer.Msg("用户名不存在,请检查用户名!如有多个用户名,请用英文逗号分隔!", ""); } else { //插入2张表 suc_Insert = this.DataManager.Execute(sqlInsert); suc_InsertText = this.DataManager.Execute(sqlInsertText); //插入cms_message_read 阅读状态表的记录,因为这里要遍历插入的人,所以要用循环 string[] recIds = recieveId.Split(','); for (int i = 0; i < recIds.Length; i++) { string uniqueId = Guid.NewGuid().ToString(); //新建一个不重复的ID string sqlInsertStatus = "insert into cms_message_read values('" + uniqueId + "','" + messageId + "','" + recIds[i] + "','" + messageStatus + "')"; this.DataManager.Execute(sqlInsertStatus); } //yangliweng,baojunyi,cgb,clj if (suc_Insert && suc_InsertText) { cs.Common.Web.AjaxServer.Msg("发送成功!", "return winClose()"); } else { cs.Common.Web.AjaxServer.Msg("发送失败!", "return winClose()"); } } } } //判断用户是否存在 public bool userIsExists(String ids) { string[] idSplit = ids.Split(','); int len = idSplit.Length; string newStr=""; //新的存放IDS的地方 for (int i = 0; i < idSplit.Length; i++) { newStr += ",'" + idSplit[i] + "'"; } DataSet setUsers = this.DataManager.Fill("select * from T_SYS_USER where id in(" + newStr.Substring(1) + ")"); int setLen = setUsers.Tables[0].Rows.Count; if (setLen < len) { return false; } else { return true; } } }
这是显示短信的代码
<%@ Page Language="C#" MasterPageFile="~/Core/Public/WebControls/Main.master" AutoEventWireup="true" CodeFile="Send.aspx.cs" Inherits="MsgSend_Send" %> <%@ Register Src="~/Core/Public/WebControls/GridTable.ascx" TagName="GridTable" TagPrefix="cms"%> <%@ Register Src="~/Core/public/WebControls/TabControl.ascx" TagName="TabControl" TagPrefix="u1" %> <asp:Content ID="bodyContent" ContentPlaceHolderID="bodyContent" runat="server"> <div style="margin:10px;"> <%-- <div id="div1" style="width:200px;float:left;"> <u2:ChannelTree ID="TopicTree1" runat="server" /> </div>--%> <div style="margin-left:10px;" id="divTab"> <u1:TabControl ID="TabControl1" runat="server"/> </div> </div> <%--发件箱--%> <div style="display:none;padding-left:10px;padding-top:10px;" id="Content1"> <%--树和TAB--%> <div style="height:30px;width:100%;margin-top:5px"> <asp:Button runat="server" ID="addMsg" CssClass="buttonwhite_100_27" Text="新增短信" OnClientClick="WinOpen_()" /> <asp:Button runat="server" ID="delMsg_Send" CssClass="buttonwhite_100_27" Text="批量删除" OnClick="delMsg_Send_Method" OnClientClick=" return DelSendBox()" /> <asp:Button runat="server" ID="groupSend" CssClass="buttonwhite_100_27" Text="短信群发" OnClientClick="WinOpen_Group()" /> </div> <div style="width:100%"> <cms:GridTable id="gridReceive" runat="server"> </cms:GridTable> </div> </div> <%--收件箱--未读信息--%> <div style="display:none;padding-left:10px;padding-top:10px;" id="Content3"> <div style="height:30px;width:100%;margin-top:5px"> <asp:Button runat="server" ID="Button2" CssClass="buttonwhite_100_27" Text="批量删除" OnClick="delMsg_UnRead" OnClientClick="return DelUnread()" /> </div> <div style="width:100%"> <cms:GridTable id="GridTableUnread" runat="server"> </cms:GridTable> </div> </div> <%--收件箱--已读信息--%> <div style="display:none;padding-left:10px;padding-top:10px;" id="Content2"> <div style="height:30px;width:100%;margin-top:5px"> <asp:Button runat="server" ID="Button1" CssClass="buttonwhite_100_27" Text="批量删除" OnClick="delMsg_Read" OnClientClick="return DelRead()" /> </div> <div style="width:100%"> <cms:GridTable id="gridProcess" runat="server"> </cms:GridTable> </div> </div> <asp:HiddenField runat="server" ID="ids" /> <asp:HiddenField runat="server" ID="idsUser" /> <asp:HiddenField runat="server" ID="txtIDS" /> </asp:Content> <asp:Content ID="jsContent" ContentPlaceHolderID="jsContent" runat="server"> <script type="text/javascript"> //打开新增短信的窗口 function WinOpen_() { return winOpen( 450 , 370 , "新增短信" , "MsgSend/AdminSend.aspx" ); } //打开新增短信的窗口 --带参数,里面写代码是把状态由未读改为已读 function winOpens(id) { //return winOpen( 450 , 370 , "新增短信" , "MsgSend/AdminSend.aspx?id="+id ); <% //更新状态表 bool flag = this.DataManager.Execute("update cms_message_read set flag=1 where idm='" + ids.Value + "' and userid='" + idsUser.Value + "'"); // Response.Write(flag); %> window.showModalDialog( "AdminSend.aspx?id="+id ,"查询短信","dialogWidth=450px;dialogHeight=370px"); } //删除:发件箱 function DelSendBox() { var s = doSelect( "<%=this.gridReceive.ClientID%>" ); if( s == "" ) { alert( "请选择要删除的信息!" ); return false; } //alert(s); $( "#<%=this.txtIDS.ClientID%>" ).val( s ); return confirm( "你确定要删除这些信息?" ); } //删除:未读信息 function DelUnread() { var s = doSelect( "<%=this.GridTableUnread.ClientID%>" ); if( s == "" ) { alert( "请选择要删除的信息!" ); return false; } //alert(s); $( "#<%=this.txtIDS.ClientID%>" ).val( s ); return confirm( "你确定要删除这些信息?" ); } //删除:已读信息 function DelRead() { var s = doSelect( "<%=this.gridProcess.ClientID%>" ); if( s == "" ) { alert( "请选择要删除的信息!" ); return false; } //alert(s); $( "#<%=this.txtIDS.ClientID%>" ).val( s ); return confirm( "你确定要删除这些信息?" ); } //打开短信群发窗口 function WinOpen_Group() { return winOpen( 450 , 370 , "短信群发" , "MsgSend/AdminSend.aspx?group=yes" ); } $( document ).load( function() { var rowCount = 0; var rowHeight = 0; var subHeight = 15; var other = 0; var bottom = $( "#divTab" ); bottom.height( $( document.body ).height() - subHeight - rowHeight * rowCount - other ); <%=this.TabControl1.ClientID%>.MarginLeft = 20; <%=this.TabControl1.ClientID%>.MarginRight = 20; <%=this.TabControl1.ClientID%>.OnSize( bottom.height() - 5 ); $( "#<%=this.TabControl1.ClientID%>" ).height( bottom.height() ); } ); </script> </asp:Content>
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Powerise.EIP.Intercourse; public partial class MsgSend_Send : cs.Common.Web.BasePage { protected void Page_Load(object sender, EventArgs e) { cs.Common.Web.TabPage tp1 = new cs.Common.Web.TabPage(); tp1.Title = "发件箱"; tp1.ContentID = "Content1"; cs.Common.Web.TabPage tp2 = new cs.Common.Web.TabPage(); cs.Common.Web.TabPage tp3 = new cs.Common.Web.TabPage(); tp3.Title = "未读信息"; tp3.ContentID = "Content3"; tp2.Title = "已读信息"; tp2.ContentID = "Content2"; this.TabControl1.TabPages.Add(tp1); this.TabControl1.TabPages.Add(tp3); this.TabControl1.TabPages.Add(tp2); this.bindScreen(); //绑定未读信息和已读新 if (!IsPostBack) { //this.addMsg.Attributes.Add("onclick", "return winOpen_()"); if (cms.Web.AppContext.CuurentUser.ID != "administrator") { this.groupSend.Visible = false; //管理员可见群发短信按钮 } this.LoadData(); //载入数据 } else { //this.LoadData(); } } //发件箱 private void BuildSendBox() { GridTableStyle gts = new GridTableStyle(); //id字段 GridTableColumn id = new GridTableColumn(); id.DataField = "id"; gts.KeyColumns.Add(id); //发送人ID GridTableColumn sendUserid = new GridTableColumn(); sendUserid.DataField = "senduserid"; sendUserid.HeaderText = "发送人ID"; gts.Columns.Add(sendUserid); // sendUserid.Visible = false; //接收人ID GridTableColumn recuserid = new GridTableColumn(); recuserid.DataField = "recuserid"; recuserid.HeaderText = "接收人ID"; recuserid.ItemHorizontalAlign = HorizontalAlign.Left; // recuserid.Width = Unit.Parse("50%"); gts.Columns.Add(recuserid); //信件ID GridTableColumn messageid = new GridTableColumn(); messageid.DataField = "messageid"; messageid.HeaderText = "信件ID"; //messageid.Width = Unit.Parse("20%"); messageid.ItemHorizontalAlign = HorizontalAlign.Right; gts.Columns.Add(messageid); messageid.Visible = false; //是否未读 GridTableColumn msgstatus = new GridTableColumn(); msgstatus.DataField = "msgstatus"; msgstatus.HeaderText = "是否未读"; msgstatus.ItemHorizontalAlign = HorizontalAlign.Center; gts.Columns.Add(msgstatus); msgstatus.Visible = false; //短消息内容 GridTableColumn messagetext = new GridTableColumn(); messagetext.DataField = "messagetext"; messagetext.HeaderText = "短信内容"; messagetext.ItemHorizontalAlign = HorizontalAlign.Center; gts.Columns.Add(messagetext); this.gridReceive.GridTableStyle = gts; this.gridReceive.GridTableStyle.IsShowCheckBox = false; this.gridReceive.GridView.RowDataBound += new GridViewRowEventHandler(GridView_RowDataBound); //绑定GRIDVIEW } protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string id = DataBinder.Eval(e.Row.DataItem, "id").ToString(); e.Row.Cells[2].Text = "<a href=\"javascript:winOpens('" + id + "');\">" + "<font color=blue>" + e.Row.Cells[2].Text + "</font></a>"; // e.Row.Cells[2].Text = "<a href=\"javascript:winOpens('" + id + "');\" >" + e.Row.Cells[2].Text + "</a>"; } } //已读信件 private void BuildRead() { GridTableStyle gts = new GridTableStyle(); //id字段 GridTableColumn id = new GridTableColumn(); id.DataField = "id"; gts.KeyColumns.Add(id); //发送人ID GridTableColumn sendUserid = new GridTableColumn(); sendUserid.DataField = "senduserid"; sendUserid.HeaderText = "发送人ID"; gts.Columns.Add(sendUserid); // sendUserid.Visible = false; //接收人ID GridTableColumn recuserid = new GridTableColumn(); recuserid.DataField = "recuserid"; recuserid.HeaderText = "接收人ID"; recuserid.ItemHorizontalAlign = HorizontalAlign.Left; // recuserid.Width = Unit.Parse("50%"); gts.Columns.Add(recuserid); recuserid.Visible = false; //信件ID GridTableColumn messageid = new GridTableColumn(); messageid.DataField = "messageid"; messageid.HeaderText = "信件ID"; //messageid.Width = Unit.Parse("20%"); messageid.ItemHorizontalAlign = HorizontalAlign.Right; gts.Columns.Add(messageid); //是否未读 GridTableColumn msgstatus = new GridTableColumn(); msgstatus.DataField = "msgstatus"; msgstatus.HeaderText = "是否未读"; msgstatus.ItemHorizontalAlign = HorizontalAlign.Center; gts.Columns.Add(msgstatus); //短消息内容 GridTableColumn messagetext = new GridTableColumn(); messagetext.DataField = "messagetext"; messagetext.HeaderText = "短信内容"; messagetext.ItemHorizontalAlign = HorizontalAlign.Center; gts.Columns.Add(messagetext); this.gridProcess.GridTableStyle = gts; this.gridProcess.GridTableStyle.IsShowCheckBox = false; this.gridProcess.GridView.RowDataBound += new GridViewRowEventHandler(ProcessGridView_RowDataBound); //绑定GRIDVIEW } protected void ProcessGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //string mailId = DataBinder.Eval(e.Row.DataItem, "emailId").ToString(); //e.Row.Cells[3].Text = "<a href=\"javascript:mailLook('" + mailId + "');\" >" + e.Row.Cells[3].Text + "</a>"; } } //未读信息 private void BuildUnRead() { GridTableStyle gts = new GridTableStyle(); //id字段 GridTableColumn id = new GridTableColumn(); id.DataField = "id"; gts.KeyColumns.Add(id); //发送人ID GridTableColumn sendUserid = new GridTableColumn(); sendUserid.DataField = "senduserid"; sendUserid.HeaderText = "发送人ID"; gts.Columns.Add(sendUserid); // sendUserid.Visible = false; //接收人ID GridTableColumn recuserid = new GridTableColumn(); recuserid.DataField = "recuserid"; recuserid.HeaderText = "接收人ID"; recuserid.ItemHorizontalAlign = HorizontalAlign.Left; // recuserid.Width = Unit.Parse("50%"); gts.Columns.Add(recuserid); recuserid.Visible = false; //信件ID GridTableColumn messageid = new GridTableColumn(); messageid.DataField = "messageid"; messageid.HeaderText = "信件ID"; //messageid.Width = Unit.Parse("20%"); messageid.ItemHorizontalAlign = HorizontalAlign.Right; gts.Columns.Add(messageid); messageid.Visible = false; //是否未读 GridTableColumn msgstatus = new GridTableColumn(); msgstatus.DataField = "msgstatus"; msgstatus.HeaderText = "是否未读"; msgstatus.ItemHorizontalAlign = HorizontalAlign.Center; gts.Columns.Add(msgstatus); //短消息内容 GridTableColumn messagetext = new GridTableColumn(); messagetext.DataField = "messagetext"; messagetext.HeaderText = "短信内容"; messagetext.ItemHorizontalAlign = HorizontalAlign.Center; gts.Columns.Add(messagetext); this.GridTableUnread.GridTableStyle = gts; this.GridTableUnread.GridTableStyle.IsShowCheckBox = false; this.GridTableUnread.GridView.RowDataBound += new GridViewRowEventHandler(Unread_RowDataBound); //绑定GRIDVIEW } protected void Unread_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string id = DataBinder.Eval(e.Row.DataItem, "messageid").ToString(); //信件ID ids.Value = id; //把messageid抛到隐藏域里面去 idsUser.Value = cms.Web.AppContext.CuurentUser.ID; //获取当前用户的ID,然后进行判断这条短信是不是属于这个用户 e.Row.Cells[2].Text = "<a href='#' onclick=\"winOpens('" + id + "');\" >"+"<font color=blue>" + e.Row.Cells[2].Text + "</font></a>"; } } //绑定GRIDVIEW的界面 public void bindScreen() { this.BuildSendBox(); //发件箱 this.BuildUnRead();//未读信息 this.BuildRead(); //已读信息 } //载入数据 public void LoadData() { //发件箱 DataSet set = this.DataManager.Fill("select * from cms_message a,cms_messagetext b where a.messageid=b.idc and a.msgstatus!=2 and a.senduserid='" + cms.Web.AppContext.CuurentUser.ID + "'"); if (set!=null) { this.gridReceive.DataSource = set; this.gridReceive.DataBind(); } //未读 DataSet setUnread = this.DataManager.Fill("select * from cms_message a,cms_messagetext b,cms_message_read c where a.messageid=b.idc and c.idm=b.idc and c.flag=0 and c.userid='"+cms.Web.AppContext.CuurentUser.ID+"'"); if (setUnread != null&&setUnread.Tables.Count>0) { //这种方法也可行,暂时注释掉 foreach (DataRow row in setUnread.Tables[0].Rows) { string recUserid = row["recuserid"].ToString(); //接收用户ID string userId = cms.Web.AppContext.CuurentUser.ID; //Response.Write(recUserid.Length); //Response.Write("baojunyi".IndexOf(userId)); //Response.Write(userId.Length); //Response.Write(recUserid.Equals(userId)); if (recUserid.IndexOf(cms.Web.AppContext.CuurentUser.ID)>=0) //如果本用户ID在RECUSERID里面的话,就绑定 { this.GridTableUnread.DataSource = setUnread; this.GridTableUnread.DataBind(); } } //this.GridTableUnread.DataSource = setUnread; //this.GridTableUnread.DataBind(); } //已处理信件 DataSet set2 = this.DataManager.Fill("select * from cms_message a,cms_messagetext b,cms_message_read c where a.messageid=b.idc and c.idm=b.idc and c.flag=1 and c.userid='" + cms.Web.AppContext.CuurentUser.ID + "'"); if (set2!=null) { //这种方法也可行,暂时注释掉 foreach (DataRow row in set2.Tables[0].Rows) { string recUserid = row["recuserid"].ToString(); //接收用户ID string userId = cms.Web.AppContext.CuurentUser.ID; //Response.Write(recUserid.Length); //Response.Write("baojunyi".IndexOf(userId)); //Response.Write(userId.Length); //Response.Write(recUserid.Equals(userId)); if (recUserid.IndexOf(cms.Web.AppContext.CuurentUser.ID) >= 0) //如果本用户ID在RECUSERID里面的话,就绑定 { this.gridProcess.DataSource = set2; this.gridProcess.DataBind(); } } //this.gridProcess.DataSource = set2; //this.gridProcess.DataBind(); } } //发件箱删除方法(假删) protected void delMsg_Send_Method(object sender,EventArgs e) { //删除语句 //string sqlDel="update" string[] ids = this.txtIDS.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); for(int i =0;i<ids.Length;i++) { string delSql = "update cms_message set msgstatus=2 where id='"+ ids[i]+"'"; this.DataManager.Execute(delSql); } } //未读信息删除方法(假删) protected void delMsg_UnRead(object sender,EventArgs e) { string[] ids = this.txtIDS.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); //方法:先是找到对应的的一张表的ID,然后根据用户名找到第三张表对应的UniqueID,根据UNIQUEID进行UPDATE操作 //第一张表:cms_message ,第三张表:cms_message_read string ids_Package = ""; for (int i = 0; i < ids.Length; i++) { ids_Package += ",'" + ids[i] + "'"; } DataSet setUnread_Dels = this.DataManager.Fill("select c.uniqueid from cms_message a,cms_messagetext b,cms_message_read c where a.messageid=b.idc and c.idm=b.idc and c.flag=0 and c.userid='" + cms.Web.AppContext.CuurentUser.ID + "' and a.id in("+ids_Package.Substring(1)+")"); foreach (DataRow Row in setUnread_Dels.Tables[0].Rows) { //更新数据库 string update_Sql = "update cms_message_read set flag=2 where uniqueid='" + Row["uniqueid"].ToString() + "'"; this.DataManager.Execute(update_Sql); } } protected void delMsg_Read(object sender, EventArgs e) { string[] ids = this.txtIDS.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); //方法:先是找到对应的的一张表的ID,然后根据用户名找到第三张表对应的UniqueID,根据UNIQUEID进行UPDATE操作 //第一张表:cms_message ,第三张表:cms_message_read string ids_Package = ""; for (int i = 0; i < ids.Length; i++) { ids_Package += ",'" + ids[i] + "'"; } DataSet setUnread_Dels = this.DataManager.Fill("select c.uniqueid from cms_message a,cms_messagetext b,cms_message_read c where a.messageid=b.idc and c.idm=b.idc and c.flag=1 and c.userid='" + cms.Web.AppContext.CuurentUser.ID + "' and a.id in(" + ids_Package.Substring(1) + ")"); foreach (DataRow Row in setUnread_Dels.Tables[0].Rows) { //更新数据库 string update_Sql = "update cms_message_read set flag=2 where uniqueid='" + Row["uniqueid"].ToString() + "'"; this.DataManager.Execute(update_Sql); } } }
下面我们就来详细解释一下代码,首先我们要知道一件事情,就是:我给很多人发了同一条信息,然后有的人看过了,有的人没有看,所以就有不同的状态,
而这种不同的状态,是以一个用户名+短信ID来确定的一个唯一的值。
明白这点就好办了。而删除也不是真正的删除,而是改变阅读的状态,让它查询不到。
还有一点大家要注意,就是怎么实现:点一下短信,让短信变成已读,这个问题困扰了我很久,因为这个链接是在GRIDVIEW里面。
我们其实只要用这个链接的ONCLICK事件,然后跳到前台,从前台去改变数据库里的值就行了。
如下:
function winOpens(id) { //return winOpen( 450 , 370 , "新增短信" , "MsgSend/AdminSend.aspx?id="+id ); <% //更新状态表 bool flag = this.DataManager.Execute("update cms_message_read set flag=1 where idm='" + ids.Value + "' and userid='" + idsUser.Value + "'"); // Response.Write(flag); %> window.showModalDialog( "AdminSend.aspx?id="+id ,"查询短信","dialogWidth=450px;dialogHeight=370px"); }
这样就OK了。
还有一点大家要注意一下,就是发件箱的删除功能和未读已读是2个不同的概念,因为发件箱里查询到的是自己的信息,
而未读,已读,是在状态表里存放的状态,这就是为什么发件箱里面也要有一个阅读状态的列,其作用就是:判断是否删除(假删)。
而要改变未读,已读里的状态,先要找到状态表里的ID,ID怎么找,找唯一项,唯一项,就是短信ID+用户名的组合。
如下。
//未读信息删除方法(假删) protected void delMsg_UnRead(object sender,EventArgs e) { string[] ids = this.txtIDS.Value.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); //方法:先是找到对应的的一张表的ID,然后根据用户名找到第三张表对应的UniqueID,根据UNIQUEID进行UPDATE操作 //第一张表:cms_message ,第三张表:cms_message_read string ids_Package = ""; for (int i = 0; i < ids.Length; i++) { ids_Package += ",'" + ids[i] + "'"; } DataSet setUnread_Dels = this.DataManager.Fill("select c.uniqueid from cms_message a,cms_messagetext b,cms_message_read c where a.messageid=b.idc and c.idm=b.idc and c.flag=0 and c.userid='" + cms.Web.AppContext.CuurentUser.ID + "' and a.id in("+ids_Package.Substring(1)+")"); foreach (DataRow Row in setUnread_Dels.Tables[0].Rows) { //更新数据库 string update_Sql = "update cms_message_read set flag=2 where uniqueid='" + Row["uniqueid"].ToString() + "'"; this.DataManager.Execute(update_Sql); } }
站内信关键是思路上的问题,其实并不是很难。
出处:http://www.cnblogs.com/kmsfan
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
欢迎大家加入KMSFan之家,以及访问我的优酷空间!