472,804 Members | 800 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,804 software developers and data experts.

Can an ImageButton be added to a Calendar day Cell?

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
2 3574
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce W.1 | last post by:
I'm trying to put together a horizontal tab nav in a User Control, and I want to have some mouseOver behavior. This could easily be done with just CSS, however hyperlinking this way (client-side)...
1
by: Henke | last post by:
Hello, I have one ImageButton controls which I initialize in Page_Load and declare on class level. ImageButton save = new ImageButton(); save.ImageUrl = "save.gif" save.Click += new...
3
by: Shevek | last post by:
Hi All, Hope someone can help! I am building an Event Calendar app based around the Calendar WebControl which builds an SQL string based on the SelectedDates property which is passed to...
5
by: Peter | last post by:
Is it possible to span / display a graphic in the web calendar control that spans more than one cell / day? Does anyone have an example of this? Thanks Peter
27
by: Dino M. Buljubasic | last post by:
I'd like to build a calendar displaying all days in a month in a grid so that when I click on the header of a square representing a day of month, I can add a new meeting etc. I know that .net...
0
by: Tom Edelbrok | last post by:
I'm using VS 2005 to develop an intranet asp.net web application and I get a weird situation. If I start out with any ASPX page that contains an ImageButton control followed by a TextBox control,...
0
by: Dabbler | last post by:
I'm loading a calendar with data from a database. It seems that the default formatting of a celll places the date number vertically in the second 3rd of the date cell, with the top and bottom...
1
by: abhishekbrave | last post by:
The code below is opening a calendar on mouse over in the same window. I need the calendar to be opened in new window. Have to fulfill this requirement urgentely so posting the whole code here. I...
7
by: William (Tamarside) | last post by:
Please, if you have the time and knowledge to help me I'd truly appreciate it! I need to build a calendar page that displays available/unavailable info from a DB and colour a cell according to...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.