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:%;
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:%;
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() - - );
bottom.children().eq( ).css( "margin-top" , );
bottom.children().eq( ).height( bottom.height() - );
bottom.children().eq( ).children().eq( ).height( bottom.children().eq( ).height() );
}
);
//内容判断不能为空
function ValidateEmpty()
{
if($("#<%=this.msgText.ClientID%>").val()=="")
{
alert("请输入短信内容");
return false;
} else {
return true;
} } </script>
</asp:Content>
AdminSend.aspx(发送短信前台)
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[].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:未读,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 = ; 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:未读,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[].Rows)
{
recieveId+=","+ Row["id"].ToString(); }
recieveId = recieveId.Substring(); //截取字符串 //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 = ; 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 = ; i < idSplit.Length; i++)
{
newStr += ",'" + idSplit[i] + "'"; } DataSet setUsers = this.DataManager.Fill("select * from T_SYS_USER where id in(" + newStr.Substring() + ")"); int setLen = setUsers.Tables[].Rows.Count; if (setLen < len)
{
return false;
} else
{
return true;
} }
}
AdminSend.aspx.cs(后台)
这是显示短信的代码
<%@ 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( , , "新增短信" , "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( , , "短信群发" , "MsgSend/AdminSend.aspx?group=yes" ); } $( document ).load( function()
{
var rowCount = ;
var rowHeight = ;
var subHeight = ;
var other = ;
var bottom = $( "#divTab" );
bottom.height( $( document.body ).height() - subHeight - rowHeight * rowCount - other );
<%=this.TabControl1.ClientID%>.MarginLeft = ;
<%=this.TabControl1.ClientID%>.MarginRight = ;
<%=this.TabControl1.ClientID%>.OnSize( bottom.height() - ); $( "#<%=this.TabControl1.ClientID%>" ).height( bottom.height() );
}
); </script> </asp:Content>
Send.aspx(前台)
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[].Text = "<a href=\"javascript:winOpens('" + id + "');\">" + "<font color=blue>" + e.Row.Cells[].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[].Text = "<a href='#' onclick=\"winOpens('" + id + "');\" >"+"<font color=blue>" + e.Row.Cells[].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>)
{ //这种方法也可行,暂时注释掉
foreach (DataRow row in setUnread.Tables[].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)>=) //如果本用户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[].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) >= ) //如果本用户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 =;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 = ; 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()+")"); foreach (DataRow Row in setUnread_Dels.Tables[].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 = ; 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() + ")"); foreach (DataRow Row in setUnread_Dels.Tables[].Rows)
{
//更新数据库
string update_Sql = "update cms_message_read set flag=2 where uniqueid='" + Row["uniqueid"].ToString() + "'";
this.DataManager.Execute(update_Sql); } } }
Send.aspx.cs
下面我们就来详细解释一下代码,首先我们要知道一件事情,就是:我给很多人发了同一条信息,然后有的人看过了,有的人没有看,所以就有不同的状态,
而这种不同的状态,是以一个用户名+短信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); }
}
站内信关键是思路上的问题,其实并不是很难。
ASP.NET 实现站内信功能(点对点发送,管理员群发)的更多相关文章
- ThinkPHP---thinkphp完善站内信功能
[一]收件箱 分析 控制器:EmailController.class.php 方法:recBox(全称receive box收件箱) 模板文件:recBox.html 分步操作: 第一步:创建方法r ...
- SharePoint 2010 类似人人网站内信功能实施
简介:用SharePoint代码加实施的方式,完成类似人人网站内信功能,当然,实现的比较简单,样式也比较难看,只为给大家一个实施的简单思路,如有谬误,还请见谅.当然,还有就是截图比较长,当然为了让大家 ...
- 开源 免费 java CMS - FreeCMS2.1 会员站内信
项目地址:http://www.freeteam.cn/ 站内信 1.1.1 写信 从左側管理菜单点击写信进入. 输入收信人.标题.内容后点击发送button. 1.1.2 收件箱 从左側管理菜单点击 ...
- 2. SharePoint Online 开发,请联系qq512800530。加好备注。(不要发站内信。。。)
///(不要发站内信...) <meta name="keywords" content="SharePoint Online, SP Online, SPO, S ...
- 站内信,群发与全部发送。Gson解析result
/** * 发送站内信 */@Permission(Module.TZGL)@RequestMapping(value = "/sendznx", method = Request ...
- 站内信DB设计实现
两年前,万仓一黍在博客园发了两篇关于站内信的设计实现博文,<群发"站内信"的实现>.<群发"站内信"的实现(续)>,其中阐述了他关于站内 ...
- Hexo next主题添加站内搜索功能
根据关键字搜索博文,站内搜索的功能很实用.hexo开启站内搜索很方便,已经有现成的插件可以使用,也是为了方便自己 安装插件 npm install hexo-generator-search --sa ...
- PHPCMS站内搜索功能实现方法汇总,一文解决PHPCMS站内搜索问题
1,https://blog.csdn.net/hzw19920329/article/details/80110673 点评:phpcms搜索功能实现方法,作者基于PHPCMS做个门户网站实现站内搜 ...
- asp.net 实现搜索站内搜索功能
首先有index和search 两个页面 index页面中有textbox1和button1两个控件 双击button1控件添加代码: protected void Button1_Click(obj ...
随机推荐
- 浅谈数据库优化方案--表和SQL
1.数据类型的选择 1.字段最好设置为非空.若字段为char(8),即便是NULL也会现有8个字符的空间. 2.尽量使用数字型字段,若只含数值信息的字段尽量不要设计为字符型,这会降低查询和连接的性能, ...
- IOS第一天-新浪微博 - 框架的搭建
*************HWAppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWith ...
- ie8 jquery parents() 获取多个的问题
今天开发的时候碰到了一个奇怪的问题 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3 ...
- 有了lisk,为什么我们还要做一个Asch?
0 前言 首先要声明一点,我们和我们的一些朋友都是lisk的投资人和支持者,我们也相信lisk会成功. 事实上,lisk已经成功了一半,目前在区块链领域融资金额排行第二,仅次于以太坊. 那为什么我们还 ...
- TLogger一个D7可用的轻量级日志
今天调程序,要用到日志.XE7有Qlog,D7用什么 从网上找到了Logger,下载的原文是不支持D7的,不过也只是很少的地方不同,自己修改了下就可以用了 感谢原作者和红鱼的分享 unit Logge ...
- 自定义cell右侧 多按钮
#import "ViewController.h" @interface ViewController () <UITableViewDataSource, UITable ...
- PHP Strict Standards:问题解决
异常信息: ( ! ) Strict standards: Declaration of SugarEmailAddress::save() should be compatible with tha ...
- 使用Word2013发布博客
步骤一.新建博客文章 打开Word软件,新建->博客文章(第一次在模板下面可能找不到,可以在搜索栏中搜索"博客",下次在首页就能直接找到). 步骤二.编辑博客文章 1.输入文 ...
- 飞流直下的精彩 -- 淘宝UWP中瀑布流列表的实现
在淘宝UWP中,搜索结果列表是用户了解宝贝的重要一环,其中的图片效果对吸引用户点击搜索结果,查看宝贝详情有比较大的影响.为此手机淘宝特意在搜索结果列表上采用了2种表现方式:一种就是普通的列表模式,而另 ...
- python自动化测试(3)- 自动化框架及工具
python自动化测试(3) 自动化框架及工具 1 概述 手续的关于测试的方法论,都是建立在之前的文章里面提到的观点: 功能测试不建议做自动化 接口测试性价比最高 接口测试可以做自动化 后面所谈到 ...