Event Calendar with two events on the same day

  • Thread starter Thread starter hal
  • Start date Start date
H

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.
 
hal said:
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
 
I'm having the same issue to solve!
Could you, please, show an example?
Thank you in advance!
Adriana


Lloyd Sheen said:
hal said:
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
 
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(this.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 said:
[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/>
 
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 De Janeiro|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 said:
[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 />";
}
 
Back
Top