C
Chuck Hartman
I've been trying to add an ImageButton object to a Calendar table cell, but
so far I am unable to handle the Command event from that button in my form's
code behind. Below is an example of what I am trying to do. The ImageButton
that is on the form handles its Command event just fine, but the ImageButton
that is added to the cell does not handle the event. Am I doing something
wrong, or is not possible to add server controls to a calendar cell? Thanks.
Chuck Hartman
--
<CalendarContainingButton.aspx>
<%@ Page language="c#" Codebehind="CalendarContainingButton.aspx.cs"
AutoEventWireup="false" Inherits="Samples.CalendarContainingButton"
trace="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CalendarContainingButton</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Calendar id="Calendar1" runat="server" Width="100%"></asp:Calendar>
<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="http://msdn.microsoft.com/msdn-online/shared/graphics/icons/rtg_pr
int.gif"
CommandName="PrintForm"
CommandArgument="DateTime.Now()"></asp:ImageButton>
</form>
</body>
</HTML>
</CalendarContainingButton.aspx>
<CalendarContainingButton.aspx.cs>
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Samples
{
/// <summary>
/// Summary description for CalendarContainingButton.
/// </summary>
public class CalendarContainingButton : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Calendar Calendar1;
protected System.Web.UI.WebControls.ImageButton ImageButton1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Calendar1.DayRender += new
System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender);
this.ImageButton1.Command += new
System.Web.UI.WebControls.CommandEventHandler(this.ImageButton1_Command);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Calendar1_DayRender(object sender,
System.Web.UI.WebControls.DayRenderEventArgs e)
{
if (e.Day.Date.Day == 15)
{
ImageButton _ib = new ImageButton();
_ib.ID = "ib15";
_ib.Command += new CommandEventHandler(ImageButton_Command);
_ib.ImageAlign = ImageAlign.Left;
_ib.ImageUrl =
@"http://msdn.microsoft.com/msdn-online/shared/graphics/icons/rtg_print.gif"
;
_ib.CommandName = "PrintDay";
_ib.CommandArgument = e.Day.Date.ToString();
e.Cell.Controls.AddAt(0, _ib);
Trace.Write("Added ImageButton Control to Calendar Day Cell.");
}
}
private void ImageButton_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
Trace.Write("ImageButton inside Calendar Day Cell was pressed.");
}
private void ImageButton1_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
Trace.Write("ImageButton on Form was pressed.");
}
}
}
</CalendarContainingButton.aspx.cs>
so far I am unable to handle the Command event from that button in my form's
code behind. Below is an example of what I am trying to do. The ImageButton
that is on the form handles its Command event just fine, but the ImageButton
that is added to the cell does not handle the event. Am I doing something
wrong, or is not possible to add server controls to a calendar cell? Thanks.
Chuck Hartman
--
<CalendarContainingButton.aspx>
<%@ Page language="c#" Codebehind="CalendarContainingButton.aspx.cs"
AutoEventWireup="false" Inherits="Samples.CalendarContainingButton"
trace="True"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CalendarContainingButton</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Calendar id="Calendar1" runat="server" Width="100%"></asp:Calendar>
<asp:ImageButton id="ImageButton1" runat="server"
ImageUrl="http://msdn.microsoft.com/msdn-online/shared/graphics/icons/rtg_pr
int.gif"
CommandName="PrintForm"
CommandArgument="DateTime.Now()"></asp:ImageButton>
</form>
</body>
</HTML>
</CalendarContainingButton.aspx>
<CalendarContainingButton.aspx.cs>
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Samples
{
/// <summary>
/// Summary description for CalendarContainingButton.
/// </summary>
public class CalendarContainingButton : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Calendar Calendar1;
protected System.Web.UI.WebControls.ImageButton ImageButton1;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Calendar1.DayRender += new
System.Web.UI.WebControls.DayRenderEventHandler(this.Calendar1_DayRender);
this.ImageButton1.Command += new
System.Web.UI.WebControls.CommandEventHandler(this.ImageButton1_Command);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Calendar1_DayRender(object sender,
System.Web.UI.WebControls.DayRenderEventArgs e)
{
if (e.Day.Date.Day == 15)
{
ImageButton _ib = new ImageButton();
_ib.ID = "ib15";
_ib.Command += new CommandEventHandler(ImageButton_Command);
_ib.ImageAlign = ImageAlign.Left;
_ib.ImageUrl =
@"http://msdn.microsoft.com/msdn-online/shared/graphics/icons/rtg_print.gif"
;
_ib.CommandName = "PrintDay";
_ib.CommandArgument = e.Day.Date.ToString();
e.Cell.Controls.AddAt(0, _ib);
Trace.Write("Added ImageButton Control to Calendar Day Cell.");
}
}
private void ImageButton_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
Trace.Write("ImageButton inside Calendar Day Cell was pressed.");
}
private void ImageButton1_Command(object sender,
System.Web.UI.WebControls.CommandEventArgs e)
{
Trace.Write("ImageButton on Form was pressed.");
}
}
}
</CalendarContainingButton.aspx.cs>