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

Event Calendar with two events on the same day

P: n/a
hal
Hello All,

I got this code somewhere, so i'm not even sure if this is the best
way to do this. Basically what i want is to display events in an
asp.net calendar control which i'm able to do, but not when i have two
events on the same day. I'm getting the data for the events from an
XML file. Here's my code.

aspx page just has the calendar control.

code behind page:

public partial class Calendar : System.Web.UI.Page
{
private DataSet _ds;

protected void Page_Load(object sender, EventArgs e)
{
_ds = new DataSet();
_ds.ReadXml(Server.MapPath("Calendar.xml"));
}

protected void Calendar1_DayRender(object sender,
DayRenderEventArgs e)
{
foreach (DataRow row in _ds.Tables[0].Rows)
{
DateTime dt = DateTime.Parse(row["date"].ToString());

if (e.Day.Date == dt)
{
e.Cell.Text = row["day"].ToString() + "<br/>" +
row["message"].ToString().Trim();
}
}
}
}

XML Page

<?xml version="1.0" encoding="utf-8" ?>
<root>

<data>
<date>8/30/2008</date>
<day>30</day>
<message>Test1</message>
</data>

<data>
<date>8/30/2008</date>
<day>30</day>
<message>Test2</message>
</data>

</root>

The second event always replaces the first event, so in the calendar
control it only displays the one event. Any help on how to get the
calendar control to display both events will be appreciated.
Aug 28 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
hal wrote:
Hello All,

I got this code somewhere, so i'm not even sure if this is the best
way to do this. Basically what i want is to display events in an
asp.net calendar control which i'm able to do, but not when i have two
events on the same day. I'm getting the data for the events from an
XML file. Here's my code.

aspx page just has the calendar control.

code behind page:

public partial class Calendar : System.Web.UI.Page
{
private DataSet _ds;

protected void Page_Load(object sender, EventArgs e)
{
_ds = new DataSet();
_ds.ReadXml(Server.MapPath("Calendar.xml"));
}

protected void Calendar1_DayRender(object sender,
DayRenderEventArgs e)
{
foreach (DataRow row in _ds.Tables[0].Rows)
{
DateTime dt = DateTime.Parse(row["date"].ToString());

if (e.Day.Date == dt)
{
e.Cell.Text = row["day"].ToString() + "<br/>" +
row["message"].ToString().Trim();
}
}
}
}

XML Page

<?xml version="1.0" encoding="utf-8" ?>
<root>

<data>
<date>8/30/2008</date>
<day>30</day>
<message>Test1</message>
</data>

<data>
<date>8/30/2008</date>
<day>30</day>
<message>Test2</message>
</data>

</root>

The second event always replaces the first event, so in the calendar
control it only displays the one event. Any help on how to get the
calendar control to display both events will be appreciated.

You are setting the e.cell.text to a value which will overlay any
previous entries. On each entry for a day you need to concatonate the
value. You will have to check if any entries are already there and if
they are concatonate rather than just setting the value.

LS
Aug 28 '08 #2

P: n/a
I'm having the same issue to solve!
Could you, please, show an example?
Thank you in advance!
Adriana
"Lloyd Sheen" wrote:
hal wrote:
Hello All,

I got this code somewhere, so i'm not even sure if this is the best
way to do this. Basically what i want is to display events in an
asp.net calendar control which i'm able to do, but not when i have two
events on the same day. I'm getting the data for the events from an
XML file. Here's my code.

aspx page just has the calendar control.

code behind page:

public partial class Calendar : System.Web.UI.Page
{
private DataSet _ds;

protected void Page_Load(object sender, EventArgs e)
{
_ds = new DataSet();
_ds.ReadXml(Server.MapPath("Calendar.xml"));
}

protected void Calendar1_DayRender(object sender,
DayRenderEventArgs e)
{
foreach (DataRow row in _ds.Tables[0].Rows)
{
DateTime dt = DateTime.Parse(row["date"].ToString());

if (e.Day.Date == dt)
{
e.Cell.Text = row["day"].ToString() + "<br/>" +
row["message"].ToString().Trim();
}
}
}
}

XML Page

<?xml version="1.0" encoding="utf-8" ?>
<root>

<data>
<date>8/30/2008</date>
<day>30</day>
<message>Test1</message>
</data>

<data>
<date>8/30/2008</date>
<day>30</day>
<message>Test2</message>
</data>

</root>

The second event always replaces the first event, so in the calendar
control it only displays the one event. Any help on how to get the
calendar control to display both events will be appreciated.


You are setting the e.cell.text to a value which will overlay any
previous entries. On each entry for a day you need to concatonate the
value. You will have to check if any entries are already there and if
they are concatonate rather than just setting the value.

LS
Aug 29 '08 #3

P: n/a
"Adriana" <Ad*****@discussions.microsoft.comwrote in message
news:E4**********************************@microsof t.com...

[top-posting corrected]
>>if (e.Day.Date == dt)
{
e.Cell.Text = row["day"].ToString() + "<br/>" +
row["message"].ToString().Trim();
}

The second event always replaces the first event, so in the calendar
control it only displays the one event. Any help on how to get the
calendar control to display both events will be appreciated.

You are setting the e.cell.text to a value which will overlay any
previous entries. On each entry for a day you need to concatonate the
value. You will have to check if any entries are already there and if
they are concatonate rather than just setting the value.

I'm having the same issue to solve!
Could you, please, show an example?
if (e.Day.Date == dt)
{
e.Cell.Text += row["day"].ToString() + "<br />" +
row["message"].ToString().Trim() + <br /><br />;
}

Notice also <br /instead of <br/>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 29 '08 #4

P: n/a
Thank you for your quick reply! But my code it's different and I don't know
what to do! Please, help me again! Thank you a lot!

Here is my code
==============================================
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Calendar Calendar1;
protected DataSet dsHoliday = new DataSet("holiday");
private void Page_Load(object sender, System.EventArgs e)
{
dsHoliday.ReadXml("s://home/zabeo/web/calendario/holidays.xml");
DataGrid1.DataSource = dsHoliday;
DataGrid1.DataMember = "holiday";
DataGrid1.DataBind();
// 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);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Calendar1.DayRender += new
System.Web.UI.WebControls.DayRenderEventHandler(th is.Calendar1_DayRender);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Calendar1_DayRender(object sender,
System.Web.UI.WebControls.DayRenderEventArgs e)
{
DataGrid1.DataSource = dsHoliday;
e.Day.IsSelectable = false;

foreach (DataRow myDataRow in dsHoliday.Tables["holiday"].Rows)
{
string s = myDataRow["Date"].ToString();
string c = myDataRow["Cor"].ToString();
string d = myDataRow["Description"].ToString();
DateTime holiday_date = DateTime.Parse(s);

Style holidayStyle = new Style();
holidayStyle.BorderWidth = 1;
holidayStyle.Font.Bold = true;
holidayStyle.BorderColor =
System.Drawing.ColorTranslator.FromHtml("#"+c+"");
holidayStyle.ForeColor =
System.Drawing.ColorTranslator.FromHtml("#000000") ;

if ((e.Day.Date == holiday_date) && (!e.Day.IsOtherMonth))
{
e.Cell.ApplyStyle(holidayStyle);
e.Day.IsSelectable = true;
}
}
}
}
}
==============================================

"Mark Rae [MVP]" wrote:
"Adriana" <Ad*****@discussions.microsoft.comwrote in message
news:E4**********************************@microsof t.com...

[top-posting corrected]
>if (e.Day.Date == dt)
{
e.Cell.Text = row["day"].ToString() + "<br/>" +
row["message"].ToString().Trim();
}

The second event always replaces the first event, so in the calendar
control it only displays the one event. Any help on how to get the
calendar control to display both events will be appreciated.

You are setting the e.cell.text to a value which will overlay any
previous entries. On each entry for a day you need to concatonate the
value. You will have to check if any entries are already there and if
they are concatonate rather than just setting the value.
I'm having the same issue to solve!
Could you, please, show an example?

if (e.Day.Date == dt)
{
e.Cell.Text += row["day"].ToString() + "<br />" +
row["message"].ToString().Trim() + <br /><br />;
}

Notice also <br /instead of <br/>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 29 '08 #5

P: n/a
"Adriana" <Ad*****@discussions.microsoft.comwrote in message
news:43**********************************@microsof t.com...

[please don't top-post]
>>I'm having the same issue to solve!
Could you, please, show an example?

if (e.Day.Date == dt)
{
e.Cell.Text += row["day"].ToString() + "<br />" +
row["message"].ToString().Trim() + <br /><br />;
}

Thank you for your quick reply! But my code it's different and I don't
know
what to do! Please, help me again! Thank you a lot!
Here is my code
foreach (DataRow myDataRow in dsHoliday.Tables["holiday"].Rows)
{
d = myDataRow["Description"].ToString();
}
Step back for a minute and look at what the code above is doing.

It says "walk through each of the rows in my datatable (one at a time) and,
each time, populate a string variable with one of the fields from that row".

And that's your problem. Each time your code moves to the next row, it
overwrites the value in the string variable which it populated in the
previous row.

So...

string d = String.Empty;
foreach (DataRow myDataRow in dsHoliday.Tables["holiday"].Rows)
{
d += myDataRow["Description"].ToString() + <br />";
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 29 '08 #6

P: n/a
Hi again... Maybe it's easy showing you what I need to do:

I have this calendar: http://www.zabeo.com.br/calendario/webform1.aspx

As you can see there in datagrid, holiday 1, 2 and 3 are in the same day,
with different colors, and I must use them in holidayStyle.BorderColor one
for each, in the same day.

What I need to do is exactly what is in this page bellow:

http://www.xpe.com.br/?on=16&in=8&cid=Rio%20De%20Janeiro|3653&estado_id= RJ

The calendar style shows borders colors (one for each event even in the same
day, creating borders from inside to outside...

I'd be very greatfull if you can help me to solve this!
"Mark Rae [MVP]" wrote:
"Adriana" <Ad*****@discussions.microsoft.comwrote in message
news:43**********************************@microsof t.com...

[please don't top-post]
>I'm having the same issue to solve!
Could you, please, show an example?

if (e.Day.Date == dt)
{
e.Cell.Text += row["day"].ToString() + "<br />" +
row["message"].ToString().Trim() + <br /><br />;
}
Thank you for your quick reply! But my code it's different and I don't
know
what to do! Please, help me again! Thank you a lot!
Here is my code
foreach (DataRow myDataRow in dsHoliday.Tables["holiday"].Rows)
{
d = myDataRow["Description"].ToString();
}

Step back for a minute and look at what the code above is doing.

It says "walk through each of the rows in my datatable (one at a time) and,
each time, populate a string variable with one of the fields from that row".

And that's your problem. Each time your code moves to the next row, it
overwrites the value in the string variable which it populated in the
previous row.

So...

string d = String.Empty;
foreach (DataRow myDataRow in dsHoliday.Tables["holiday"].Rows)
{
d += myDataRow["Description"].ToString() + <br />";
}
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Aug 29 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.