using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace CompositePalindromeControl
{
//[DefaultProperty("Text")]
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
public class ServerControl1 : CompositeControl, INamingContainer
{
// [Bindable(true)]
// [Category("Appearance")]
// [DefaultValue("")]
// [Localizable(true)]
public event EventHandler PalindromeFound;
protected TextBox textboxPalindrome;
protected Button buttonCheckForPalindrome;
protected LiteralControl literalcontrolPalindromeStatus;
protected Label labelForTextBox;
protected Table tablePalindromes;
protected ArrayList alPalindromes;
private String text;
public string Text
{
get
{
//String s = (String)ViewState["Text"];
//return ((s == null) ? "[" + this.ID + "]" : s);
return text;
}
set
{
//ViewState["Text"] = value;
text = value;
this.alPalindromes = (ArrayList)this.ViewState["palindromes"];
if (this.alPalindromes == null)
{
this.alPalindromes = new ArrayList();
}
if (this.checkForPalindrome())
{
if (PalindromeFound != null)
{
PalindromeFound(this, EventArgs.Empty);
}
alPalindromes.Add(text);
literalcontrolPalindromeStatus.Text = String.Format("This is a Palindrome <br/> <Font size=\"5\" color=\"blue\"></b>{0}</b></font>", text);
}
else
{
literalcontrolPalindromeStatus.Text = String.Format("This is not a Palindrome <br/> <Font size=\"5\" color=\"red\"></b>{0}</b></font>", text);
}
this.ViewState.Add("palindromes", alPalindromes);
this.BuildPalindromesTable();
}
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(Text);
}
protected string alphanumeric(string str)
{
string strTemp = (String)str.Clone();
if (str != null)
{
char[] var = strTemp.ToCharArray();
int i = 0;
foreach (char c in var)
{
if (char.IsLetterOrDigit(c))
{
i++;
}
else
{
strTemp = strTemp.Remove(i, 1);
}
}
}
return strTemp;
}
protected bool checkForPalindrome()
{
if (this.Text != null)
{
String strControlText = this.Text;
String strTextToUpper = null;
strTextToUpper = Text.ToUpper();
strControlText = this.alphanumeric(strTextToUpper);
char[] rgcReverse = strControlText.ToCharArray();
Array.Reverse(rgcReverse);
String strReverse = new String(rgcReverse);
if (strControlText == strReverse)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
public void OnCheckPalindrome(Object o, System.EventArgs e)
{
this.Text = this.textboxPalindrome.Text;
this.checkForPalindrome();
}
protected override void CreateChildControls()
{
//base.CreateChildControls();
labelForTextBox = new Label();
labelForTextBox.Text = "Enter a Palindrome";
this.Controls.Add(labelForTextBox);
textboxPalindrome = new TextBox();
this.Controls.Add(textboxPalindrome);
Controls.Add(new LiteralControl("<br/>"));
buttonCheckForPalindrome = new Button();
buttonCheckForPalindrome.Text = " Click For Palindrome";
buttonCheckForPalindrome.Click += new EventHandler(OnCheckPalindrome);
this.Controls.Add(buttonCheckForPalindrome);
Controls.Add(new LiteralControl("<br/>"));
this.tablePalindromes = new Table();
this.Controls.Add(tablePalindromes);
this.ChildControlsCreated = true;
}
protected void BuildPalindromesTable()
{
this.alPalindromes = (ArrayList)this.ViewState["palindromes"];
if (this.alPalindromes != null)
{
foreach (string s in this.alPalindromes)
{
TableCell tablecell = new TableCell();
tablecell.BorderStyle = BorderStyle.Double;
tablecell.BorderWidth = 3;
tablecell.Text = s;
TableRow tablerow = new TableRow();
tablerow.Cells.Add(tablecell);
this.tablePalindromes.Rows.Add(tablerow);
}
}
}
}
}