Sunday, January 17, 2010

Creating a GenderSelector composite control in C#:

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace CompositeControl2
{
public class GenderSelector:Control,INamingContainer
{
//creating a custom event
public delegate void DelGenderSelected();
public event DelGenderSelected GenderSelected;
public string SelectedGender;

//designing our control's interface
protected override void CreateChildControls()
{
base.CreateChildControls();

Label promptLabel = new Label();
promptLabel.Height = 50;
promptLabel.Width = 300;
promptLabel.Text = "Select Gender";
Controls.Add(promptLabel);
Controls.Add(new LiteralControl("
"));

RadioButton Male = new RadioButton();
Male.Text = "Male";
Male.AutoPostBack = true;
Male.GroupName = "Gender";
Controls.Add(Male);
Controls.Add(new LiteralControl("
"));
Male.CheckedChanged += new EventHandler(Male_CheckedChanged);

RadioButton Female = new RadioButton();
Female.Text = "Female";
Female.AutoPostBack = true;
Female.GroupName = "Gender";
Controls.Add(Female);
Controls.Add(new LiteralControl("
"));
Female.CheckedChanged += new EventHandler(Female_CheckedChanged);
}


protected void Male_CheckedChanged(object sender, System.EventArgs e)
{
EnsureChildControls();
SelectedGender = "Male";
this.GenderSelected();

}

protected void Female_CheckedChanged(object sender, System.EventArgs e)
{
EnsureChildControls();
SelectedGender = "Female";
this.GenderSelected();

}

}

}

No comments:

Post a Comment