Friday, November 2, 2007

Tell a friend : Custom Control

·

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Diagnostics;
using System.IO;
using System.Data;
using System.Web.UI.Design;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
namespace ccc.TellaFriend
{
public enum TellaFreindText
{
Your_Name=0,
Your_Email=1,
Friend_Email=2,
Message=3,
Button=4,
Title=5

}

[DefaultProperty("Text"),
ToolboxData("<{0}:TellaFriend runat=server></{0}:TellaFriend>"),
Designer(typeof(ccc.TellaFriend.Design.TellaFriendDesing))]

public class TellaFriend :System.Web.UI.WebControls.WebControl,INamingContainer
{


public Label lbl_title;

public TextBox txt_yourname;
public TextBox txt_yourEmail;
public TextBox txt_friendEmail;
public TextBox txt_message;

public Button btn_send;

public RequiredFieldValidator req1;
public RequiredFieldValidator req2;
public RequiredFieldValidator req3;
public RequiredFieldValidator req4;

public RegularExpressionValidator reg1;
public RegularExpressionValidator reg2;

public ValidationSummary vs;

#region Tell a Friend Text

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]

public string Text
{
get
{
this.EnsureChildControls();
string retVal = "";

switch (TellaFreindText)
{
case TellaFreindText.Your_Name:
retVal = txt_yourname.Text;
break;

case TellaFreindText.Your_Email:
retVal = txt_yourEmail.Text;
break;

case TellaFreindText.Friend_Email:
retVal = txt_friendEmail.Text;
break;

case TellaFreindText.Message:
retVal = txt_message.Text;
break;
case TellaFreindText.Button:
retVal = btn_send.Text;
break;
case TellaFreindText.Title:
retVal = lbl_title.Text;
break;
}

return (retVal);
}

set
{
this.EnsureChildControls();
switch (TellaFreindText)
{
case TellaFreindText.Your_Name:
txt_yourname.Text = value;
break;
case TellaFreindText.Your_Email:
txt_yourEmail.Text = value;
break;
case TellaFreindText.Friend_Email:
txt_friendEmail.Text = value;
break;
case TellaFreindText.Message:
txt_message.Text = value;
break;
case TellaFreindText.Button:
btn_send.Text = value;
break;
case TellaFreindText.Title:
lbl_title.Text = value;
break;
}
}
}

#endregion

#region Tell a Freind CSS

[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]

public string TellaFreindCss
{
get
{
this.EnsureChildControls();
string retCss = "";

switch (TellaFreindText)
{
case TellaFreindText.Your_Name:
retCss = txt_yourname.CssClass;
break;

case TellaFreindText.Your_Email:
retCss = txt_yourEmail.CssClass;
break;

case TellaFreindText.Friend_Email:
retCss = txt_friendEmail.CssClass;
break;

case TellaFreindText.Message:
retCss = txt_message.CssClass;
break;
case TellaFreindText.Button:
retCss = btn_send.CssClass;
break;
case TellaFreindText.Title:
retCss = lbl_title.CssClass;
break;
}

return (retCss);
}

set
{
this.EnsureChildControls();
switch (TellaFreindText)
{
case TellaFreindText.Your_Name:
txt_yourname.CssClass = value;
break;
case TellaFreindText.Your_Email:
txt_yourEmail.CssClass = value;
break;
case TellaFreindText.Friend_Email:
txt_friendEmail.CssClass = value;
break;
case TellaFreindText.Message:
txt_message.CssClass = value;
break;
case TellaFreindText.Button:
btn_send.CssClass = value;
break;
case TellaFreindText.Title:
lbl_title.CssClass = value;
break;
}
}
}

#endregion

#region Tell a Freind Width

[Bindable(false), Category("Appearance"), DefaultValue(null),
Description("The width of the button text in between the decorations for Text mode buttons. The full width of the button for Button mode buttons. Not used for Image mode buttons.")]

public Unit TellafriendWidth
{
get
{
Unit retWidth = System.Web.UI.WebControls.Unit.Percentage(50);

switch (TellaFreindText)
{
case TellaFreindText.Your_Name:
retWidth = txt_yourname.Width;
break;

case TellaFreindText.Your_Email:
retWidth = txt_yourEmail.Width;
break;

case TellaFreindText.Friend_Email:
retWidth = txt_friendEmail.Width;
break;

case TellaFreindText.Message:
retWidth = txt_message.Width;
break;
case TellaFreindText.Button:
retWidth = btn_send.Width;
break;
case TellaFreindText.Title:
retWidth = lbl_title.Width;
break;
}

return (retWidth);
}

set
{
this.EnsureChildControls();
switch (TellaFreindText)
{
case TellaFreindText.Your_Name:
txt_yourname.Width = value;
break;
case TellaFreindText.Your_Email:
txt_yourEmail.Width = value;
break;
case TellaFreindText.Friend_Email:
txt_friendEmail.Width = value;
break;
case TellaFreindText.Message:
txt_message.Width = value;
break;
case TellaFreindText.Title:
lbl_title.Width = value;
break;
}
}
}
#endregion

#region Tell a Freind Height

[Bindable(false), Category("Appearance"), DefaultValue(null),
Description("The width of the button text in between the decorations for Text mode buttons. The full width of the button for Button mode buttons. Not used for Image mode buttons.")]

public Unit TellafriendHeight
{
get
{
Unit retHeight = System.Web.UI.WebControls.Unit.Percentage(50);

switch (TellaFreindText)
{
case TellaFreindText.Your_Name:
retHeight = txt_yourname.Height;
break;

case TellaFreindText.Your_Email:
retHeight = txt_yourEmail.Height;
break;

case TellaFreindText.Friend_Email:
retHeight = txt_friendEmail.Height;
break;

case TellaFreindText.Message:
retHeight = txt_message.Height;
break;
case TellaFreindText.Button:
retHeight = btn_send.Height;
break;
case TellaFreindText.Title:
retHeight = lbl_title.Height;
break;
}

return (retHeight);
}

set
{
this.EnsureChildControls();
switch (TellaFreindText)
{
case TellaFreindText.Your_Name:
txt_yourname.Height = value;
break;
case TellaFreindText.Your_Email:
txt_yourEmail.Height = value;
break;
case TellaFreindText.Friend_Email:
txt_friendEmail.Height = value;
break;
case TellaFreindText.Message:
txt_message.Height = value;
break;
case TellaFreindText.Title:
lbl_title.Height = value;
break;
}
}
}
#endregion

#region Tell a FriendText

[Bindable(false), Category("Appearance"),
Description("")]

public TellaFreindText TellaFreindText
{
get
{

this.EnsureChildControls();

TellaFreindText retVal;

if (ViewState["textid"] == null)
retVal = TellaFreindText.Your_Name;
else
retVal = (TellaFreindText)ViewState["textid"];

return (retVal);

}

set
{
ViewState["textid"] = value;
this.EnsureChildControls();
TellaFreindText retVal;
retVal = value;


}
}
#endregion

#region Bind

protected override void CreateChildControls()
{

req1 = new RequiredFieldValidator();
req2 = new RequiredFieldValidator();
req3 = new RequiredFieldValidator();
req4 = new RequiredFieldValidator();

reg1 = new RegularExpressionValidator();
reg2 = new RegularExpressionValidator();

vs = new ValidationSummary();

lbl_title = new Label();

txt_yourname = new TextBox();
txt_yourEmail = new TextBox();
txt_friendEmail = new TextBox();
txt_message = new TextBox();

btn_send = new Button();
btn_send.Click+=new EventHandler(btn_send_Click);

HtmlTable table = new HtmlTable();
HtmlTableRow newRow;
HtmlTableCell newCell;

table.Border = 0;
table.Style.Add("DISPLAY", "inline");
table.Style.Add("VERTICAL-ALIGN", "middle");

///1st row
newRow = new HtmlTableRow();
newCell = new HtmlTableCell();

newCell.Controls.Add(lbl_title);
newCell.ColSpan = 2;
newRow.Cells.Add(newCell);
table.Rows.Add(newRow);

///2nd row
newRow =new HtmlTableRow();
newCell = new HtmlTableCell();
newCell.Align = "right";
newCell.InnerHtml = "<strong>Your Name:</strong>";
newRow.Controls.Add(newCell);

newCell = new HtmlTableCell();
txt_yourname.ID = "txtyourName";
newCell.Controls.Add(txt_yourname);

req1.ControlToValidate = txt_yourname.ID;
req1.ErrorMessage = "Enter Your Name";
req1.SetFocusOnError = true;
req1.Display = ValidatorDisplay.None;

newCell.Controls.Add(req1);

newRow.Cells.Add(newCell);
table.Rows.Add(newRow);

//3dr row
newRow = new HtmlTableRow();
newCell = new HtmlTableCell();
newCell.Align = "right";
newCell.InnerHtml = "<strong>Your Email:</strong>";
newRow.Controls.Add(newCell);

newCell = new HtmlTableCell();
txt_yourEmail.ID = "txtyourEmail";
newCell.Controls.Add(txt_yourEmail);

req2.ControlToValidate = txt_yourEmail.ID;
req2.ErrorMessage = "Enter Your Email Address";
req2.SetFocusOnError = true;
req2.Display = ValidatorDisplay.None;
newCell.Controls.Add(req2);

reg1.ControlToValidate = txt_yourEmail.ID;
reg1.ErrorMessage = "Invalid Email Address";
reg1.ValidationExpression = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
reg1.SetFocusOnError = true;
reg1.Display = ValidatorDisplay.None;
newCell.Controls.Add(reg1);

newRow.Cells.Add(newCell);
table.Rows.Add(newRow);

//4th row
newRow = new HtmlTableRow();
newCell = new HtmlTableCell();
newCell.Align = "right";
newCell.InnerHtml = "<strong>Friend's Email:</strong>";
newRow.Controls.Add(newCell);

newCell = new HtmlTableCell();
txt_friendEmail.ID = "txtFriendEmail";
newCell.Controls.Add(txt_friendEmail);

req3.ControlToValidate = txt_friendEmail.ID;
req3.ErrorMessage = "Enter Your Freind's Email Address";
req3.SetFocusOnError = true;
req3.Display = ValidatorDisplay.None;
newCell.Controls.Add(req3);

reg2.ControlToValidate = txt_friendEmail.ID;
reg2.ErrorMessage = "Invalid Email Address";
reg2.ValidationExpression = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
reg2.SetFocusOnError = true;
reg2.Display = ValidatorDisplay.None;
newCell.Controls.Add(reg2);

newRow.Cells.Add(newCell);
table.Rows.Add(newRow);

//5th row
newRow = new HtmlTableRow();
newCell = new HtmlTableCell();
newCell.Align = "right";
newCell.InnerHtml = "<strong>Message:</strong>";
newRow.Controls.Add(newCell);

newCell = new HtmlTableCell();
txt_message.TextMode = TextBoxMode.MultiLine;
txt_message.ID = "txtMessage";
newCell.Controls.Add(txt_message);

req4.ControlToValidate = txt_message.ID;
req4.ErrorMessage = "Enter Message";
req4.SetFocusOnError = true;
req4.Display = ValidatorDisplay.None;
newCell.Controls.Add(req4);

newRow.Cells.Add(newCell);
table.Rows.Add(newRow);

//6th row
newRow = new HtmlTableRow();
newCell = new HtmlTableCell();
newCell.Align = "right";
newCell.InnerHtml = "";
newRow.Controls.Add(newCell);

newCell = new HtmlTableCell();

newCell.Controls.Add(btn_send);

vs.ShowMessageBox = true;
vs.ShowSummary = false;
newCell.Controls.Add(vs);

newRow.Cells.Add(newCell);
table.Rows.Add(newRow);

table.CellPadding = 2;
table.CellSpacing = 4;
Controls.Add(table);

this.TellaFreindText = TellaFreindText.Button;
this.Text = "Send Message";
//this.Text = "Enter your name";

}
#endregion

#region Button Event

///
/// This delegate is called when the CoolButtonMode is set to Text.
/// It's only job is to forward the event to any registered handelers that
/// are encapsulating this control, including parent composite controls, or
/// the page itself.
///
///<span> <</span>param name="sender">The sender of the event<span></</span>param>
/// <span><</span>param name="e">An EventArgs object.<span></</span>param>

public void btn_send_Click(object sender, EventArgs e)
{

System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
TellaFreindText = TellaFreindText.Your_Name;
string subject = "Mail from" + this.Text;
TellaFreindText = TellaFreindText.Your_Email;
string from = this.Text;
TellaFreindText = TellaFreindText.Friend_Email;
string to = this.Text;
TellaFreindText = TellaFreindText.Message;
string body = this.Text;

System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(from, to, subject, body);
System.Net.Mail.SmtpClient cl = new System.Net.Mail.SmtpClient("localhost");
mm.IsBodyHtml = true;
cl.Send(mm);

}
#endregion
}
}

namespace ccc.TellaFriend.Design
{
public class TellaFriendDesing :ControlDesigner
{
///
/// Returns a design view of the control as rendered by the control itself.
///
/// The HTML of the design time control.
///

#region Bind control at desing time

public override string GetDesignTimeHtml()
{


TellaFriend tf = (TellaFriend)Component;

// If there are no controls, then it's the first time through the
// designer, so set the text to the unique id. This will also
// cause EnsureChildControls() to be called in Text(), which will
// build out the rest of the control.
if (tf.Controls.Count == 0)
tf.Text = tf.UniqueID;

StringWriter sw = new StringWriter();
HtmlTextWriter tw = new HtmlTextWriter(sw);

tf.RenderBeginTag(tw);
tf.RenderControl(tw);
tf.RenderEndTag(tw);

return (sw.ToString());
}
#endregion
}
}

0 comments: