By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

Can an ImageButton be added to a Calendar day Cell?

P: n/a
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(th is.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>
Nov 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
"Chuck Hartman" <Ch**********@usonline.ibm.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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(th is.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>

Nov 18 '05 #2

P: n/a
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

"Teemu Keiski" <jo****@aspalliance.com> wrote in message
news:eM**************@tk2msftngp13.phx.gbl...
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

Nov 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.