Thursday, January 26, 2012

Working with Custom and User Controls

Bellow is the code for my "Calculator" Control. A user control is simply a container to controls
Calculator.ascx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Calculator : System.Web.UI.UserControl
{
    public event EventHandler MagicNumber;
    public string TitleText { get{return (string)ViewState["TitleText"];} set{ViewState["TitleText"]=value;} }// retains the state of the property while postbacks
    public FontUnit Fontsize { get { return (FontUnit)ViewState["Fontsize"]; } set { ViewState["Fontsize"] = value; } }
    public Calculator()
    {

         TitleText = "My Calculator";
         Fontsize = FontUnit.Small ;// is over ridden every time by the viewState having the value the first load
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        lblLegend.Text = TitleText;
        lblLegend.Font .Size  = Fontsize;
        lblResult.Font.Size = Fontsize;
        txtNum1.Font.Size = Fontsize;       
        txtNum2.Font.Size = Fontsize;
        btnEnter.Font.Size = Fontsize;
    }
  
    protected void btnEnter_Click(object sender, EventArgs e)
    {
        int num1 = int.Parse(txtNum1.Text);
        int num2 = int.Parse(txtNum2.Text);
        int res = num1 + num2;
        if (res == 8 && MagicNumber != null)
            MagicNumber(this, EventArgs.Empty);
        lblResult .Text = res.ToString();
    }

}
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Session["Theme"] = "~/SkinFile";
       if (!IsPostBack)
        {
        
            myCalc1.Fontsize = FontUnit.Smaller;
        }
        Panel1.Controls.Add(LoadControl("~/Calculator.ascx"));

    }
    protected void OnMagicNumberCalculated(object sender, EventArgs e)
    {
        showMagNum.Text = "Magic Number Calculated";
    }
}


Default.aspx 
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 <%@ Register Src="~/Calculator.ascx" TagName="myCalc" TagPrefix="C" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">

<C:myCalc ID="myCalc1"  runat="server" TitleText="Super Calculator!"  OnMagicNumber="OnMagicNumberCalculated" />
<asp:Label ID ="showMagNum" runat="server" EnableViewState ="false" ></asp:Label>
<asp:Panel ID="Panel1" runat="server">
<CC:MyCustomControl runat="server" ID="MyCC" />
</asp:Panel>

</form></body>
</html>
Web.config File
 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 <%@ Register Src="~/Calculator.ascx" TagName="myCalc" TagPrefix="C" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">

<C:myCalc ID="myCalc1"  runat="server" TitleText="Super Calculator!"  OnMagicNumber="OnMagicNumberCalculated" />
<asp:Label ID ="showMagNum" runat="server" EnableViewState ="false" ></asp:Label>
<asp:Panel ID="Panel1" runat="server">
<CC:MyCustomControl runat="server" ID="MyCC" />
</asp:Panel>

</form></body>
</html>
MyCustomControl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace NameSpaceSaad
{
    public class MyCustomControl: WebControl
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            writer.Write("<b><h6><marquee>HI!! CHECK MY CALCULATOR I M ADDRESSING YOU FROM MY CUSTOM CONTROL</marquee></h2></b>");
            base.Render(writer);
        }
   
    }
}

No comments:

Post a Comment