Can an ImageButton be added to a Calendar day Cell?

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>
 
T

Teemu Keiski

Hi,

Calendar's DayRender happens at render phase of the Page lifecycle and that
is too late to add controls to the Page for postback events to be
raised..You can still check Form post (request.Form) collection manually to
check if button was clicked.

On the other hand one might think adding ImageButtons via Controls
collection before rendering (and at Page_Load on postback), but Calendar has
EmptyControlCollection as the collection type (means it won't allow child
controls) and the actual rendering of the links is pretty much hardcoded.
Some kind of trick with having a button at the Page (hidden) and having
"dummy" button (plain HTML) with a delegated postback call (__doPostBack
call, in onclick attribute, that's done for other Button as if that button
would have been clicked), but it seems pretty hard route.

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist


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>
 
C

Chuck Hartman

Teemu,

Thanks. I did attempt both of the paths that you suggest and I do think it
could be done. You're right, it's harder than it seems like it should be,
therefore I am probably just going to abandon the post back button infavor
of a (less desirable for my App) HyperLink'ed Image with a QueryString.

Chuck Hartman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top