Viewstate problem

C

Code Scorpio

Hi all,

I'm developing a custom control that renders a calendar in a html table. (in
C#)
Let me tell first that is not <asp:calendar> builtin control.

To change the current week (weekly view of the calendar), i've added two <a>
html elements with a code like this :

_render += "<a title='Next Week' id=\"" + this.UniqueID + "\"
href=\"javascript:" + Page.GetPostBackEventReference(this, "Next") +"\">";
_render += "<a title='Previous Week' id=\"" + this.UniqueID + "\"
href=\"javascript:" + Page.GetPostBackEventReference(this, "Prev") +"\">";

I capture the postbacks in the control's Render function : (First question
is this a wrong way to capture the postbacks ?)

protected override void Render(HtmlTextWriter output)
{
if ((Page.IsPostBack) && (Page.Request["__EVENTTARGET"] ==
this.UniqueID))
{
switch (Page.Request["__EVENTARGUMENT"])
{
case "Prev":
this.CalendarDate = this.CalendarDate.AddDays (-7);
break;
case "Next":
this.CalendarDate = this.CalendarDate.AddDays (7);
break;
}
}
else.................



CalendarDate variable is saved in the ViewState with a code like this : (is
this true ?)


[Bindable(true), Category("Calendar")]
public DateTime CalendarDate
{
get
{
return (DateTime)ViewState["CalendarDate"]; // don't forget to check if
it's null or not
}
set
{
ViewState["CalendarDate"] = value;
SetExpressions ();
}
}


When "previous" or "next" week links clicked in the container page, i
capture the postbacks.
And i add or subtract 7 days to the current date saved in the CalendarDate
variable (saved in the ViewState also).

Here is step by step problem with ViewState:

1- CalendarDate set to Today's date by WebForm page load at the first step..

2- I click previous week link..

3- The component successfully subtracts 7 days from the CalendarDate value
and saves it to it self again to obtain ViewState synchronization.

4- Page show the previous week..

5- I click the next week link.. (normally i should see the same week with
step 1)

6- The component successfully adds 7 days BUT to the original value where
the WebForm page set at the step 1.

Even i save the value to the ViewState in the component, ViewState doesn't
change the content..

Please help me, i'm stucked :(

Thnx

M.Gurel
 
W

Wilco Bauwer

You should understand the page's life cycle to understand your problem
better. The problem in your case is that you are basically too late
adding stuff to the viewstate. Once the page is in its rendering phase,
the viewstate has already been saved.

As a sidenote: you should really look into some techniques like
handling postback data, etc. You are handling things at a very low
level at this moment (like checking what the posted "__EVENTARGUMENT"'s
value is). There is an interface IPostBackDataHandler which you could
implement to achieve exactly what you want to achieve: handle post-back
data.

I would also suggest you take a look at the built-in controls. I see
you are manually building hyperlinks with strings. I think that if you
take a look at LinkButton, you will see that you are probably
re-inventing the wheel, in a bad way.
 
C

Code Scorpio

Wilco Bauwer said:
You should understand the page's life cycle to understand your problem
better. The problem in your case is that you are basically too late
adding stuff to the viewstate. Once the page is in its rendering phase,
the viewstate has already been saved.

Ok i'm learning.
Thnx for suggestions but even i use LinkButton there have to be a way to
synchronize with the ViewState. no ?
As a sidenote: you should really look into some techniques like
handling postback data, etc. You are handling things at a very low
level at this moment (like checking what the posted "__EVENTARGUMENT"'s
value is). There is an interface IPostBackDataHandler which you could
implement to achieve exactly what you want to achieve: handle post-back
data.

That's why i asked if it's a wrong way to capture.. I felt that must be more
hi-level...
I'll review the documents about that thnx.
 
C

Code Scorpio

Wilco Bauwer said:
You should understand the page's life cycle to understand your problem
better. The problem in your case is that you are basically too late
adding stuff to the viewstate. Once the page is in its rendering phase,
the viewstate has already been saved.

Thanks a lot for this suggestion.

I've read the page's life cycle from the MSDN and now my problem is solved
by defining a click postback event handler.

Thanks again :)))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top