Hello,
I'm implementing a messaging system in my program. It functions just like e-mail except that it is entirely contained within the system.
I was wondering if anyone has seen an e-mail control template that I could modify. It would have stuff like an inbox, message viewing and message sending parts.
I would just rewire the mail sending & receiving parts to my hook up to my tables.
Thanks!
Edit:
Ok I didn't realize I wrote so much. Make sure you credit Biff_MaGriff if you use this. :D Tech is .net 3.5 with SubSonic 3.0
The rest is in my answer.
Should look like this in the end. (Minus all my cut outs)
web.config
<pages>
<controls>
...
<add tagPrefix="cc" tagName="MessageFolder" src="~/UserControls/Messages/MessageFolder/MessageFolder.ascx"/>
<add tagPrefix="cc" tagName="MessageView" src="~/UserControls/Messages/MessageView/MessageView.ascx"/>
<add tagPrefix="cc" tagName="MessageCompose" src="~/UserControls/Messages/MessageCompose/MessageCompose.ascx"/>
<add tagPrefix="cc" tagName="MessageBox" src="~/UserControls/Messages/MessageBox/MessageBox.ascx"/>
...
</controls>
<pages>
App_Code/IMessageFolder.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for IMessageFolder
/// </summary>
public interface IMessageFolder
{
int? MessageID { get; }
}
App_Code/CustomDataViews.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for CustomDataViews
/// </summary>
public class CustomDataViews
{
//Subsonic db
static VAC.Data.VACDB db = new VAC.Data.VACDB();
public class MessageQuery
{
public class Message
{
public int MessageID { get; set; }
public string FromUser { get; set; }
public string ToUser { get; set; }
public DateTime SentDate { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
public bool IsDeletedSender { get; set; }
public bool IsDeletedRecipient { get; set; }
public bool IsRead { get; set; }
}
public enum MailType { ReceivedMail, SentMail };
public Message[] GetMessages(MailType mt, bool showDeleted)
{
List<Message> myMessages = new List<Message>();
foreach (var v in (from m in db.Messages
where (mt == MailType.ReceivedMail ? m.ToUserID : m.FromUserID) == Common.CurrentUserID
&& (showDeleted || !(mt == MailType.ReceivedMail ? m.IsDeletedRecipient : m.IsDeletedSender))
orderby m.SentDate descending
select m))
{
Message mess = new Message();
mess.MessageID = v.MessageID;
mess.FromUser = (from u in db.Users where u.UserID == v.FromUserID select u.UserName).First();
mess.ToUser = (from u in db.Users where u.UserID == v.ToUserID select u.UserName).First();
mess.SentDate = v.SentDate;
mess.Subject = v.Subject;
mess.Body = v.Body;
mess.IsDeletedSender = v.IsDeletedSender;
mess.IsDeletedRecipient = v.IsDeletedRecipient;
mess.IsRead = v.IsRead;
myMessages.Add(mess);
}
return myMessages.ToArray();
}
public Message GetMessage(int MessageID)
{
var myMessage = (from m in db.Messages where m.MessageID == MessageID select m);
if (myMessage.Count() == 1)
{
var v = myMessage.First();
Message mess = new Message();
mess.MessageID = v.MessageID;
mess.FromUser = (from u in db.Users where u.UserID == v.FromUserID select u.UserName).First();
mess.ToUser = (from u in db.Users where u.UserID == v.ToUserID select u.UserName).First();
mess.SentDate = v.SentDate;
mess.Subject = v.Subject;
mess.Body = v.Body;
return mess;
}
else
return null;
}
}
}
App_Code/Common.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;
using System.Web.Configuration;
using System.Data;
using System.Text;
using System.Collections;
using System.Globalization;
using System.Linq;
/// <summary>
/// Summary description for Common
/// </summary>
public class Common
{
/// <summary>
/// Retrieves the index of the column from the gridview. Returns -1 if not found.
/// </summary>
/// <param name="gv"></param>
/// <param name="columnName"></param>
/// <returns></returns>
static public int GetColumnIndex(GridView gv, string columnName)
{
int returnMe = -1;
for (int i = 0; i < gv.Columns.Count; i++)
{
if (gv.Columns[i].HeaderText == columnName)
{
returnMe = i;
break;
}
}
return returnMe;
}
//Colours for gridviews
static public string BGColourRed = "#FF8888";
static public string BGColourRedAlternate = "#FF0000";
static public string BGColourBlue = "#AAAAFF";
static public string BGColourBlueAlternate = "#4444FF";
static public string BGColourGreen = "#2bf053";
static public string BGColourGreenAlternate = "#27c319";
static public string BGColourOrange = "#FFA365";
static public string BGColourOrangeAlternate = "#FF6600";
static public string NormalBGColour(object sender)
{
return "#" +
((GridView)sender).RowStyle.BackColor.R.ToString("X") +
((GridView)sender).RowStyle.BackColor.G.ToString("X") +
((GridView)sender).RowStyle.BackColor.B.ToString("X");
}
static public string AlternateBGColour(object sender)
{
return "#" +
((GridView)sender).AlternatingRowStyle.BackColor.R.ToString("X") +
((GridView)sender).AlternatingRowStyle.BackColor.G.ToString("X") +
((GridView)sender).AlternatingRowStyle.BackColor.B.ToString("X");
}
static public string SelectedBGColour(object sender)
{
string selectedBGColour = "#165EA9";
if (!((GridView)sender).SelectedRowStyle.BackColor.IsEmpty)
{
selectedBGColour = "#" +
((GridView)sender).SelectedRowStyle.BackColor.R.ToString("X") +
((GridView)sender).SelectedRowStyle.BackColor.G.ToString("X") +
((GridView)sender).SelectedRowStyle.BackColor.B.ToString("X");
}
return selectedBGColour;
}
/// <summary>
/// Gridview RowDataBound extension.
/// Allows row selection by clicking on a row and highlights the row in yellow on the mouse hover.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static public void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItemIndex == -1)
return;
//e.Row.Attributes.Add("onclick", ((GridView)sender).Page.ClientScript.GetPostBackEventReference((GridView)sender, "Select$" + e.Row.RowIndex.ToString(), false));
string selectedBGColour = SelectedBGColour(sender);
string normalBGColour = NormalBGColour(sender);
string alternateBGColour = AlternateBGColour(sender);
ApplyStylingToRow(e.Row, selectedBGColour, normalBGColour, alternateBGColour);
}
static public void ApplyStylingToRow(GridViewRow row, string selectedBGColour, string normalBGColour, string alternateBGColour)
{
AppendAttribute(row, "onMouseOver",
"this.style.cursor='pointer'; this.style.background='yellow';");
AppendAttribute(row, "onMouseOut",
"this.style.background='" + ((row.RowState & DataControlRowState.Selected) == DataControlRowState.Selected ? selectedBGColour :
(row.RowState & DataControlRowState.Alternate) == DataControlRowState.Alternate ? alternateBGColour : normalBGColour) + "';");
}
static public void GridViewAddRowClick(GridView gv, GridViewRow row, bool isRender)
{
AppendAttribute(row, "onclick", gv.Page.ClientScript.GetPostBackEventReference(gv, "Select$" + row.RowIndex.ToString(), isRender) + ";");
/*
for (int i = 0; i < gv.Columns.Count; i++)
if (!string.IsNullOrEmpty(gv.Columns[i].HeaderText) && row.Cells.Count > 1)
AppendAttribute(row.Cells[i], "onclick", gv.Page.ClientScript.GetPostBackEventReference(gv, "Select$" + row.RowIndex.ToString(), isRender) + ";");
* */
}
static public void GridViewRowClick_Render(GridView gv)
{
foreach (GridViewRow row in gv.Rows)
if (row.RowType == DataControlRowType.DataRow)
GridViewAddRowClick(gv, row, true);
gv.Page.ClientScript.RegisterForEventValidation(gv.UniqueID);
}
/// <summary>
/// Hides a column on a gridview.
/// </summary>
/// <param name="gv"></param>
/// <param name="columnIndex"></param>
static public void HideColumn(GridView gv, int columnIndex)
{
if (gv.HeaderRow != null)
gv.HeaderRow.Cells[columnIndex].Style.Add("display", "none");
foreach (GridViewRow row in gv.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
row.Cells[columnIndex].Style.Add("display", "none");
}
}
/// <summary>
/// Registers javascript to be run.
/// </summary>
/// <param name="con"></param>
/// <param name="script"></param>
static public void RegisterStartupScript(object con, string script)
{
RegisterStartupScript((Control)con, script);
}
/// <summary>
/// Capitalizes the beginning of strings
/// </summary>
/// <param name="strText"></param>
public static string InitCap(string strText)
{
return new CultureInfo("en").TextInfo.ToTitleCase(strText.ToLower());
}
/// <summary>
/// Registers javascript to be run.
/// </summary>
/// <param name="con"></param>
/// <param name="script"></param>
static public void RegisterStartupScript(Control con, string script)
{
ScriptManager sm = ScriptManager.GetCurrent(con.Page);
if (sm != null)
ScriptManager.RegisterStartupScript(con, con.GetType(), Guid.NewGuid().ToString(), script, true);
else
con.Page.ClientScript.RegisterStartupScript(typeof(Page), Guid.NewGuid().ToString(), script, true);
}
/// <summary>
/// Registers a javascript file to be included in the page.
/// </summary>
/// <param name="con"></param>
/// <param name="url"></param>
static public void RegisterClientScriptInclude(Control con, string url)
{
ScriptManager sm = ScriptManager.GetCurrent(con.Page);
if (sm != null)
ScriptManager.RegisterClientScriptInclude(con, con.GetType(), Guid.NewGuid().ToString(), System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + url);
else
con.Page.ClientScript.RegisterClientScriptInclude(typeof(Page), Guid.NewGuid().ToString(), System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + url);
}
public static int CurrentUserID { get { return (from u in new VAC.Data.VACDB().Users where u.UserName == HttpContext.Current.User.Identity.Name select u.UserID).First(); } }
public class MessageComposeEventArgs : EventArgs
{
public int? SendToUserID { get; set; }
public string Subject { get; set; }
public MessageComposeEventArgs()
{
}
public MessageComposeEventArgs(int SendToUserID)
{
this.SendToUserID = SendToUserID;
}
public MessageComposeEventArgs(int SendToUserID, string Subject)
{
this.SendToUserID = SendToUserID;
this.Subject = Subject;
}
}
}
App_Code/ExtendedCommandField.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
/// <summary>
/// An extended <see cref="CommandField"/> that allows deletions
/// to be confirmed by the user.
/// </summary>
public class ExtendedCommandField : CommandField
{
/// <summary>
/// Initialize the cell.
/// </summary>
public override void InitializeCell(DataControlFieldCell cell,
DataControlCellType cellType, DataControlRowState rowState, int rowIndex)
{
base.InitializeCell(cell, cellType, rowState, rowIndex);
if (!string.IsNullOrEmpty(this.DeleteConfirmationText) && this.ShowDeleteButton)
{
foreach (Control control in cell.Controls)
{
IButtonControl button = control as IButtonControl;
if (button != null && button.CommandName == "Delete")
// Add delete confirmation
((WebControl)control).Attributes.Add("onclick", string.Format
("if (!confirm('{0}')) return false;", this.DeleteConfirmationText));
}
}
}
#region DeleteConfirmationText
/// <summary>
/// Delete confirmation text.
/// </summary>
[Category("Behavior")]
[Description("The text shown to the user to confirm the deletion.")]
public string DeleteConfirmationText
{
get { return this.ViewState["DeleteConfirmationText"] as string; }
set { this.ViewState["DeleteConfirmationText"] = value; }
}
#endregion
}
~/UserControls/Messages/MessageFolder/MessageFolder.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MessageFolder.ascx.cs"
Inherits="UserControls_Messages_MessageFolder_MessageFolder" %>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:HiddenField ID="hfMessageID" runat="server" />
<asp:Button ID="butRefresh" runat="server" Text="Check for new Messages" /><asp:Button
ID="butCompose" runat="server" Text="Compose New Message" OnClick="butCompose_Click" /><asp:CheckBox
AutoPostBack="true" ID="cbShowDeleted" runat="server" Text="Show Deleted Messages"
OnCheckedChanged="cbShowDeleted_CheckChanged" />
<asp:GridView ID="gvMessages" runat="server" DataKeyNames="MessageID" AutoGenerateColumns="false"
OnRowDataBound="gvMessages_RowDataBound" OnSelectedIndexChanged="gvMessages_SelectedIndexChanged"
OnRowDeleting="gvMessages_RowDeleting" Width="100%" Style="float: left;">
<Columns>
<asp:BoundField DataField="MessageID" InsertVisible="false" />
<cc:ExtendedCommandField DeleteConfirmationText="Are you certain you wish to delete this record?"
DeleteText="Delete" ShowDeleteButton="true" />
<asp:BoundField DataField="FromUser" HeaderText="From" />
<asp:BoundField DataField="ToUser" HeaderText="To" />
<asp:TemplateField>
<HeaderTemplate>
Sent Date</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbl1" runat="server" Text='<%# Bind("SentDate") %>' Style="white-space: nowrap;" /></ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Subject" HeaderText="Subject" ItemStyle-Width="100%" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>