I'm using an ashx file to access messages that are stored in a database and outputting the results into a string builder. The problem I'm having with this method is that depending on the varying length of user names, messages etc. That the end result can appear messy when output onto the web browser. I will submit the code for the ashx file that I am using:
Expand|Select|Wrap|Line Numbers
- <%@ WebHandler Language="C#" Class="AjaxHandler" %>
- using System;
- using System.Data;
- using System.Text;
- using System.Web;
- using ChatClassLibrary.DataAccess;
- public class AjaxHandler : IHttpHandler {
- public void ProcessRequest (HttpContext context) {
- context.Response.ContentType = "text/plain";
- context.Response.Expires = 0;
- context.Response.Clear();
- int userID = Convert.ToInt32(context.Request.Params["uid"]);
- int lastRead = Messages.GetLastReceived(userID);
- DataTable results = Messages.GetLatestMessages(userID, lastRead);
- if (results.Rows.Count > 0)
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("<table border='0' width= '750px' id='chatTable' class='overflow' >");
- ;
- sb.Append("<tbody>");
- foreach (DataRow row in results.Rows)
- {
- sb.Append("<tr width='200px'>");
- sb.Append("<td style='width:50px' >");
- string userName = row["UserName"].ToString();
- sb.Append(userName);
- sb.Append("</td>");
- sb.Append("<td style='width:50px'>");
- string message = row["MsgContent"].ToString();
- sb.Append(message);
- sb.Append("</td>");
- sb.Append("<td style='width:50px'>");
- string time = row["TimeSent"].ToString();
- sb.Append(time);
- sb.Append("</td>");
- sb.Append("<td style='width:50px'>");
- string delete = "Del";
- sb.Append(delete);
- sb.Append("</td>");
- sb.Append("</tr>");
- }
- sb.Append("</tbody>");
- sb.Append("</table>");
- context.Response.Write(sb.ToString());
- }
- else
- {
- context.Response.Write("");
- }
- }
- public bool IsReusable {
- get {
- return false;
- }
- }
- }
Thanks,
Davo