Monday, July 27, 2009

Dynamic load user control with delegate event

Hello Friends,
Here I will explain you how can you load dynamically user control and perform delegate event on it.
Let's see example here.

Usercontrol : Calc.ascx
-------------------------------

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Calc.ascx.cs" Inherits="Calc" %>

<asp:Label ID="lblNum1" runat="server" Text="Enter The Number One"></asp:Label>  
<asp:TextBox ID="txtNum1" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblOperation" runat="server" Text="+" runat="server"></asp:Label><br />
<asp:Label ID="lblNum2" runat="server" Text="Enter The Number Two"></asp:Label>  
<asp:TextBox ID="txtNum2" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnAnswer" runat="server" Text="Answer" CausesValidation="false" OnClick="btnAnswer_Click" />
<br />

Usercontrol : Calc.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 Calc : System.Web.UI.UserControl
{
public delegate void PerformOperationHandler(double num1, double num2);
public event PerformOperationHandler PerformOperation;

protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnAnswer_Click(object sender, EventArgs e)
{
if (PerformOperation != null)
{
PerformOperation(double.Parse(txtNum1.Text),double.Parse(txtNum2.Text));
}
}
}


Page Name:Default.aspx
--------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
<%@ Reference Control="~/Calc.ascx" %>


<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="placeholder" runat="server"></asp:PlaceHolder>
<br />
<asp:Label ID="lblAnswer" runat="server"></asp:Label>
</div>
</form>

Page Name:Default.aspx.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 Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

UserControl Calculation = LoadControl("~/Calc.ascx") as UserControl ;
placeholder.Controls.Clear();
placeholder.Controls.Add(Calculation);
((Calc)Calculation).PerformOperation+=new Calc.PerformOperationHandler(PerformOperation);

}

public void PerformOperation(double num1, double num2)
{
lblAnswer.Text = (num1 + num2).ToString();
}
}

I hopes it will help you.

Regards,
Kinjal Shah

Create Custom Calendar

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack && Request["date"] == null)
{
RenderCalendar(System.DateTime.Now);
}
else
{
if (Request["date"] != null)
{
RenderCalendar(Convert.ToDateTime(Request["date"]));
}
}
}

private void RenderCalendar(DateTime CurrentDate)
{
tblCalendar.Rows.Clear();
TableRow TitleRow = new TableRow();
TableCell PrevCell = new TableCell();
HyperLink hypPrev = new HyperLink();
hypPrev.Text = "<";
hypPrev.NavigateUrl = "Calendar.aspx?date=" + CurrentDate.AddMonths(-1).ToShortDateString();
TitleRow.Cells.Add(PrevCell);
PrevCell.Controls.Add(hypPrev);


TableCell DateHeaderCell = new TableCell();
DateHeaderCell.ColumnSpan = 5;
Label lblHeader = new Label();

lblHeader.Text = String.Format("{0:MMM}", CurrentDate) + " " + CurrentDate.Year.ToString();
DateHeaderCell.VerticalAlign = VerticalAlign.Middle;
DateHeaderCell.HorizontalAlign = HorizontalAlign.Center;
DateHeaderCell.Font.Bold = true;
TitleRow.Cells.Add(DateHeaderCell);
DateHeaderCell.Controls.Add(lblHeader);

TableCell NextCell = new TableCell();
HyperLink hypNext = new HyperLink();
hypNext.Text = ">";
hypNext.NavigateUrl = "Calendar.aspx?date=" + CurrentDate.AddMonths(1).ToShortDateString();

TitleRow.Cells.Add(NextCell);
NextCell.Controls.Add(hypNext);

tblCalendar.Rows.Add(TitleRow);

TableRow DayRow = new TableRow();
TableCell DayCellSun = new TableCell();
DayCellSun.Text = "Sun";
DayCellSun.Font.Bold = true;
DayRow.Cells.Add(DayCellSun);

TableCell DayCellMon = new TableCell();
DayCellMon.Text = "Mon";
DayCellMon.Font.Bold = true;
DayRow.Cells.Add(DayCellMon);

TableCell DayCellTue = new TableCell();
DayCellTue.Text = "Tue";
DayCellTue.Font.Bold = true;
DayRow.Cells.Add(DayCellTue);

TableCell DayCellWed = new TableCell();
DayCellWed.Text = "Wed";
DayCellWed.Font.Bold = true;
DayRow.Cells.Add(DayCellWed);

TableCell DayCellThu = new TableCell();
DayCellThu.Text = "Thu";
DayCellThu.Font.Bold = true;
DayRow.Cells.Add(DayCellThu);

TableCell DayCellFri = new TableCell();
DayCellFri.Text = "Fri";
DayCellFri.Font.Bold = true;
DayRow.Cells.Add(DayCellFri);

TableCell DayCellSat = new TableCell();
DayCellSat.Text = "Sat";
DayCellSat.Font.Bold = true;
DayRow.Cells.Add(DayCellSat);

tblCalendar.Rows.Add(DayRow);

String nDate = Convert.ToString(CurrentDate.ToShortDateString());

int year = CurrentDate.Year;
int month = CurrentDate.Month;
// int totalDays = DateTime.DaysInMonth(year,month);

String StartDate = month + "/1/" + year;
String FirstDay = Convert.ToDateTime(StartDate).DayOfWeek.ToString();

int currentDay = Convert.ToInt16(Convert.ToDateTime(nDate).Day);
// string CurrentWeekDay = Convert.ToDateTime(nDate).DayOfWeek.ToString();


int count = 0;
DateTime CountDate = Convert.ToDateTime(StartDate);
for (int i = 0; i <= 6; i++)
{
TableRow tr = new TableRow();
for (int j = 1; j <= 7; j++)
{
String CurrDay = GetDay(Convert.ToString(j));
TableCell td = new TableCell();
if (count >= 1 || FirstDay == CurrDay)
{
if (month == Convert.ToDateTime(CountDate).Month)
{
td.Text = Convert.ToDateTime(CountDate).Day.ToString();
count = count + 1;
DateTime dt = Convert.ToDateTime(CountDate).AddDays(1);
CountDate = Convert.ToDateTime(dt.ToShortDateString());

td.Attributes.Add("onClick", "Javascript:alert(" + count + ");");
}
}
else
{
td.Text = " ";
}
tr.Cells.Add(td);
}
tblCalendar.Rows.Add(tr);
}
}
private String GetDay(String Day)
{
switch (Day)
{
case "1":
return "Sunday";
case "2":
return "Monday";
case "3":
return "Tuesday";
case "4":
return "Wednesday";
case "5":
return "Thursday";
case "6":
return "Friday";
case "7":
return "Saturday";

}
return null;
}