User Controls and Custom Server Controls, Part 2
Pages: 1, 2, 3, 4
Example 6-7: Blog.cs
using System;
using System.Data;
using System.Drawing;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace aspnetian
{
public class Blog:Panel, INamingContainer
{
protected DataSet BlogDS;
protected TextBox TitleTB;
protected TextBox BlogText;
private string _addRedirect;
private string _email;
private string _mode = "display";
public string AddRedirect
{
get
{
return this._addRedirect;
}
set
{
this._addRedirect = value;
}
}
public string Email
{
get
{
return this._email;
}
set
{
this._email = value;
}
}
public string Mode
{
get
{
return this._mode;
}
set
{
this._mode = value;
}
}
protected override void OnInit(EventArgs e)
{
LoadData( );
base.OnInit(e);
}
protected override void CreateChildControls( )
{
if (this._mode.ToLower( ) != "add")
{
DisplayBlogs( );
}
else
{
NewBlog( );
}
}
protected void LoadData( )
{
BlogDS = new DataSet( );
try
{
BlogDS.ReadXml(Page.Server.MapPath("Blog.xml"));
}
catch (FileNotFoundException fnfEx)
{
CreateBlankFile( );
LoadData( );
}
}
protected void DisplayBlogs( )
{
DateTime BlogDate;
DateTime CurrentDate = new DateTime( );
DataRowCollection BlogRows = BlogDS.Tables[0].Rows;
foreach (DataRow BlogDR in BlogRows)
{
string BDate = BlogDR["date"].ToString( );
BlogDate = new DateTime(Convert.ToInt32(BDate.Substring(4, 4)),
Convert.ToInt32(BDate.Substring(0, 2)),
Convert.ToInt32(BDate.Substring(2, 2)));
if (CurrentDate != BlogDate)
{
Label Date = new Label( );
Date.Text = BlogDate.ToLongDateString( );
Date.Font.Size = FontUnit.Large;
Date.Font.Bold = true;
this.Controls.Add(Date);
this.Controls.Add(new LiteralControl("<br/><br/>"));
CurrentDate = BlogDate;
}
HtmlAnchor Anchor = new HtmlAnchor( );
Anchor.Name = "#" + BlogDR["anchorID"].ToString( );
this.Controls.Add(Anchor);
Label Title = new Label( );
Title.Text = BlogDR["title"].ToString( );
Title.Font.Size = FontUnit.Larger;
Title.Font.Bold = true;
this.Controls.Add(Title);
this.Controls.Add(new LiteralControl("<p>"));
LiteralControl BlogText = new LiteralControl("<div>" +
BlogDR["text"].ToString( ) + "</div>");
this.Controls.Add(BlogText);
this.Controls.Add(new LiteralControl("</p>"));
HyperLink Email = new HyperLink( );
Email.NavigateUrl = "mailto:" + BlogDR["email"].ToString( );
Email.Text = "E-mail me";
this.Controls.Add(Email);
this.Controls.Add(new LiteralControl(" | "));
HyperLink AnchorLink = new HyperLink( );
AnchorLink.NavigateUrl = Page.Request.Url.ToString( ) + "#" +
BlogDR["anchorID"].ToString( );
AnchorLink.Text = "Link";
this.Controls.Add(AnchorLink);
this.Controls.Add(new LiteralControl("<hr width='100%'/><br/>"));
}
}
protected void NewBlog( )
{
Label Title = new Label( );
Title.Text = "Create New Blog";
Title.Font.Size = FontUnit.Larger;
Title.Font.Bold = true;
this.Controls.Add(Title);
this.Controls.Add(new LiteralControl("<br/><br/>"));
Label TitleLabel = new Label( );
TitleLabel.Text = "Title: ";
TitleLabel.Font.Bold = true;
this.Controls.Add(TitleLabel);
TitleTB = new TextBox( );
this.Controls.Add(TitleTB);
this.Controls.Add(new LiteralControl("<br/>"));
Label BlogTextLabel = new Label( );
BlogTextLabel.Text = "Text: ";
BlogTextLabel.Font.Bold = true;
this.Controls.Add(BlogTextLabel);
BlogText = new TextBox( );
BlogText.TextMode = TextBoxMode.MultiLine;
BlogText.Rows = 10;
BlogText.Columns = 40;
this.Controls.Add(BlogText);
this.Controls.Add(new LiteralControl("<br/>"));
Button Submit = new Button( );
Submit.Text = "Submit";
Submit.Click += new EventHandler(this.Submit_Click);
this.Controls.Add(Submit);
}
protected void Submit_Click(object sender, EventArgs e)
{
EnsureChildControls( );
AddBlog( );
}
protected void AddBlog( )
{
DataRow NewBlogDR;
NewBlogDR = BlogDS.Tables[0].NewRow( );
NewBlogDR["date"] = FormatDate(DateTime.Today);
NewBlogDR["title"] = TitleTB.Text;
NewBlogDR["text"] = BlogText.Text;
NewBlogDR["anchorID"] = Guid.NewGuid().ToString( );
NewBlogDR["email"] = _email;
BlogDS.Tables[0].Rows.InsertAt(NewBlogDR, 0);
BlogDS.WriteXml(Page.Server.MapPath("Blog.xml"));
Page.Response.Redirect(_addRedirect);
}
protected string FormatDate(DateTime dt)
{
string retString;
retString = String.Format("{0:D2}", dt.Month);
retString += String.Format("{0:D2}", dt.Day);
retString += String.Format("{0:D2}", dt.Year);
return retString;
}
protected void CreateBlankFile( )
{
// code to create new file...omitted to conserve space
}
} // closing bracket for class declaration
} // closing bracket for namespace declaration

