Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I'm invoking in the ItemDataBound event of repeater control an object that uses the value of the user's session, but I get error, it seems that the object can not to be created and when I use this same object in subsequent events at load, I haven't problem, I need to know when to start the session's variables at exist?.


the code is the follow:
C#
protected void repDias_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
	DateTime date = (DateTime)e.Item.DataItem;
	Literal litDia = (Literal)e.Item.FindControl("litDia");
	HtmlTableCell tdDia = (HtmlTableCell)e.Item.FindControl("tdDia");
	int vencidos=0;
	int mantenimientos = 0;
	string usuarioid;
	string clienteid;
    if (Session["Usuarioid"] != null)
		usuarioid = Session["Usuarioid"].ToString();
	if (Session["clienteid"] != null)
		clienteid = Session["clienteid"].ToString();
	lstFechaMantenimiento.DataSource = _m.obtenerTodos("", "",usuarioid, "", clienteid);
	lstFechaMantenimiento.DataTextField = "sigrev";
	lstFechaMantenimiento.DataValueField = "id";
	lstFechaMantenimiento.DataBind();
	DateTime dt= new DateTime();
	DateTime dthoy = new DateTime();
	DateTime dtfecha = new DateTime();
	dt=DateTime.Now;
	string hoy = string.Format("{2}-{1}-{0}", dt.Year, dt.Month.ToString("D2"), dt.Day.ToString("D2"));
	dthoy = Convert.ToDateTime(hoy); 
	if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
		tdDia.Attributes.Add("class", "weekend");
		if (_actualMes == date.Month)
		{
			litDia.Text = string.Format("<a href=\"{0}?CalDate={3}-{2}-{1}\"{4}>{3}</a>", Request.Path, date.Year, date.Month.ToString("D2"), date.Day.ToString("D2"), (_specialDaySelected && date.Equals(_selectedDate)) ? " class=\"selected\"" : string.Empty);
			fechamantenimiento = string.Format("{2}-{1}-{0}", date.Year, date.Month.ToString("D2"), date.Day.ToString("D2"));
			dtfecha = Convert.ToDateTime(fechamantenimiento); 
			for (int i = 0; i < lstFechaMantenimiento.Items.Count; i++)
			{
				if (dthoy <= dtfecha)
				{
					if (fechamantenimiento == lstFechaMantenimiento.Items[i].Text)
					{	mantenimientos = mantenimientos + 1;	}
				}
				else
				{
					if (fechamantenimiento == lstFechaMantenimiento.Items[i].Text)
					{	vencidos = vencidos + 1;	}
				}
			}
			if (mantenimientos > 0)
				tdDia.Attributes.Add("class", "fechamantenimiento");
			if (vencidos > 0)
				tdDia.Attributes.Add("class", "vencido");
		}
		else
			litDia.Text = string.Format("<span>{0}</span>", date.Day.ToString("D2"));
		tdDia.ID = string.Empty;	
	}
}


Please, someone could help me.
thans in advance.
Posted
Updated 4-Aug-14 3:50am
v9
Comments
_Amy 31-Jul-14 11:10am    
Can I see your code?
danigeraleddin 31-Jul-14 12:13pm    
My guide is the following link http://stackoverflow.com/questions/4724350/asp-net-availability-calendar-showing-full-year , my purpose is to assign a class to a cell in an annual calendar, the color of the cell depends on the values ​​that are in a list that is filled with a procedure that uses the session's variable, but i believe that the session's variable is null at the time. I updated my question and I've put the code
Wombaticus 31-Jul-14 13:18pm    
page_load is called before OnItemDataBound, or indeed before any controls' events.
danigeraleddin 31-Jul-14 15:11pm    
According to the page's trace , the databound event fires before the page loads as often as needed to build the control. This is the order of events showing me the trace.

Begin PreInit,End PreInit,Begin Init,End Init,Begin InitComplete,End InitComplete,Begin PreLoad,End PreLoad,Begin Load,Page_Load,End Load,Begin LoadComplete,End LoadComplete,Begin PreRender,End PreRender,Begin PreRenderComplete,End PreRenderComplete,Begin SaveState,End SaveState,Begin SaveStateComplete,End SaveStateComplete,Begin Render,End Render.

1 solution

Use the System.Web.HttpContext.Current.Session class to read session variables.
For e.g. var param = System.Web.HttpContext.Current.Session["loginParameters"].
 
Share this answer
 
Comments
danigeraleddin 4-Aug-14 9:58am    
Hi, thank for the post, I tried with System.Web.HttpContext.Current.Session, but the problem persists, for events prior to load the session variable appears with null.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900