asp.net 2.0 master pages problem

M

Martijn Saly

Hi there,

I've created a master page with some controls on it, a Calendar control
among others. Now depending on the date(s) selected, the content page needs
to be updated. In the masterpage I've created StartDate and EndDate
properties, which are basically wrappers around the SelectedDates property
from the Calendar control.

When I select another date in the Calendar, the page updates normally, but
when Page_Load for the content page fires, the selected dates aren't updated
yet. The problem is probably that the content page gets updated first, and
then the master page.

How do I work around this problem? Or is this more like a bug in the
framework? Matbe what should happen, is that both the master page *and* the
content page should be updated, and *then* the Page_Load event(s) should be
called.

Anyone has any input on this?
 
S

Scott Allen

The SelectedDate property of the Calendar will not be updated until
the calendar's SelectionChanged event fires (which will be after
Page_Load for both the content page and the master).

Yes, I think the Calendar is a bit quirky in this respect. Any other
control that has an 'auto-postback' will be updated by Page_Load.
 
C

clintonG

Scott, your closing statement is not neccessarily correct when controls are
used in MasterPages.

A LinkButton that is a child of a Panel located in a MasterPage will not and
need not raise a PostBack event when the LinkButton click event is raised.
Furthermore, the controls disappear from the ViewState reverting to their
state before any click events were raised (why in my circumstances is now
undestood but how to respond has brought me to a stand still).

I begin to discuss the topic at the ASP.NET Forums [1] and have been trying
to get the attention of somebody like yourself who has done the early work
with 2.0 to help debug and make sense of how to respond to controls that
simply disappear from ViewState. Will you take it on with me here on in the
forums?

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

[1] http://forums.asp.net/1039817/ShowPost.aspx
 
M

Martijn Saly

Scott said:
The SelectedDate property of the Calendar will not be updated until
the calendar's SelectionChanged event fires (which will be after
Page_Load for both the content page and the master).

Not true, when selecting a date, I can clearly see while debugging the
Page_load event of the master page, that SelectedDates is updated as expected.
Yes, I think the Calendar is a bit quirky in this respect. Any other
control that has an 'auto-postback' will be updated by Page_Load.

The problem is that the master page should be updated before the content
page. Now it's the other way around, and therefor a content page cannot know
about any changes in any control on its master page.
 
S

Scott Allen

Not true, when selecting a date, I can clearly see while debugging the
Page_load event of the master page, that SelectedDates is updated as expected.

Are we talking about a date selected by the user who clicked on a new
day / week in the Calendar control? This calendar will not know the
date until the logic in RaisePostBackEvent executes, which is after
Page_Load. The code for a form at the bottom of this post will
demonstrate this fact by checking the SelectedDate property during
Page_Load, and during Page_PreRender (which is after
RaisePostBackEvent). The 2 labels on the form will show different
dates when you start clicking on links.
The problem is that the master page should be updated before the content
page. Now it's the other way around, and therefor a content page cannot know
about any changes in any control on its master page.

Ah - I think I understand the problem now. When you say "selecting a
date" you mean programatically, not by clicking on the Calendar at
runtime, right?

The way master pages are implemented they become a child control in
the content page's Controls collection, thus Page_Load for the content
page will always fire before Page_Load in the master page:

http://odetocode.com/Blogs/scott/archive/2005/09/10/2179.aspx


It's handy to think of the master page as just another user control on
the page when it comes time to executing logic in response to events.

Code:

<%@ Page Language="C#" %>

<script runat="server">
void Page_Load(object o, EventArgs e)
{
LoadLabel.Text = Calendar1.SelectedDate.ToString();
}

void Page_PreRender(object o, EventArgs e)
{
PreRenderLabel.Text = Calendar1.SelectedDate.ToString();
}
</script>

<html>
<head runat="server">
<title>Calendar</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Calendar runat="server" ID="Calendar1"/>
<br />
<asp:Label runat="server" ID="LoadLabel"/>
<br />
<asp:Label runat="server" ID="PreRenderLabel" />
</div>
</form>
</body>
</html>
 
S

Scott Allen

Can you give me some code to try myself? I'll definitely take a look.
Feel free to email me, too.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Scott, your closing statement is not neccessarily correct when controls are
used in MasterPages.

A LinkButton that is a child of a Panel located in a MasterPage will not and
need not raise a PostBack event when the LinkButton click event is raised.
Furthermore, the controls disappear from the ViewState reverting to their
state before any click events were raised (why in my circumstances is now
undestood but how to respond has brought me to a stand still).

I begin to discuss the topic at the ASP.NET Forums [1] and have been trying
to get the attention of somebody like yourself who has done the early work
with 2.0 to help debug and make sense of how to respond to controls that
simply disappear from ViewState. Will you take it on with me here on in the
forums?

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

[1] http://forums.asp.net/1039817/ShowPost.aspx
 
C

clintonG

As it turns out I've been trying to build my own tabbed navigation and am
now going to try to do so using a Menu control that will load SiteMapPath
controls instead of LinkButtons and Panel controls but I do want to follow
through on the issue I raised as there is an important issue related to
referencing controls that I have not mastered (pun intended) when using
MasterPages.

The former method I've been asking questions about used LinkButtons to
change visibility for a Panel control which contained other LinkButtons used
to load content pages in the master. When I attempted to use the LinkButton
which is the child of the Panel the content page would load but the Panel
and its child controls would disappear.

Somebody finally pointed out the reason the control state was being lost was
because I was actually navigating and not using a PostBack and actually just
reloading the MasterPage in the same state it was before the LinkButton
event was used to load a content page. I understand that but still need to
master how to access a child control such as a LinkButton when it is a child
of the Panel which is in the MasterPage.

Your otherwise outstanding article "In Search of ASP.NET Controls" [1] was
helpful and a frequent reference but did not hit the sweet spot for me in
this unique circumstance when using MasterPages primarily I believe because
your focus while writing the article was the use of the FindControl method
which is rather easy to use when controls are in the page but not when a
control is a child of a control in the page when using MasterPages.

I even tried using the new Cross-Page Posting, PostBackUrl and PreviousPage
property which will pass the control state to the new page but I could not
reference that LinkButton child of the Panel.

Mood: braindead ;-)

<%= Clinton Gallagher

[1] http://www.odetocode.com/Articles/116.aspx




Scott Allen said:
Can you give me some code to try myself? I'll definitely take a look.
Feel free to email me, too.

--
Scott
http://www.OdeToCode.com/blogs/scott/

Scott, your closing statement is not neccessarily correct when controls
are
used in MasterPages.

A LinkButton that is a child of a Panel located in a MasterPage will not
and
need not raise a PostBack event when the LinkButton click event is raised.
Furthermore, the controls disappear from the ViewState reverting to their
state before any click events were raised (why in my circumstances is now
undestood but how to respond has brought me to a stand still).

I begin to discuss the topic at the ASP.NET Forums [1] and have been
trying
to get the attention of somebody like yourself who has done the early work
with 2.0 to help debug and make sense of how to respond to controls that
simply disappear from ViewState. Will you take it on with me here on in
the
forums?

<%= Clinton Gallagher
METROmilwaukee (sm) "A Regional Information Service"
NET csgallagher AT metromilwaukee.com
URL http://metromilwaukee.com/
URL http://clintongallagher.metromilwaukee.com/

[1] http://forums.asp.net/1039817/ShowPost.aspx


Scott Allen said:
The SelectedDate property of the Calendar will not be updated until
the calendar's SelectionChanged event fires (which will be after
Page_Load for both the content page and the master).

Yes, I think the Calendar is a bit quirky in this respect. Any other
control that has an 'auto-postback' will be updated by Page_Load.

--
Scott
http://www.OdeToCode.com/blogs/scott/



Hi there,

I've created a master page with some controls on it, a Calendar control
among others. Now depending on the date(s) selected, the content page
needs
to be updated. In the masterpage I've created StartDate and EndDate
properties, which are basically wrappers around the SelectedDates
property
from the Calendar control.

When I select another date in the Calendar, the page updates normally,
but
when Page_Load for the content page fires, the selected dates aren't
updated
yet. The problem is probably that the content page gets updated first,
and
then the master page.

How do I work around this problem? Or is this more like a bug in the
framework? Matbe what should happen, is that both the master page *and*
the
content page should be updated, and *then* the Page_Load event(s) should
be
called.

Anyone has any input on this?
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top