Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Page_Load in UserControl

Reply
Thread Tools

Page_Load in UserControl

 
 
news.iq.ca
Guest
Posts: n/a
 
      12-03-2004
Hello,

I am *VERY* confused - I understand that the Page_Load event of a user
control is completely independent of the Page_Load event of the container
page.

I just started with User Controls and now I was trying my hand at creating a
"navigation" control. Assuming that I name a sequence of pages like
"PageUsingUserControls_1", "PageUsingUserControls_2" and
"PageUsingUserControls_3",. I have written this code:

HTML----------------------------------------------------------------------------------------------------------------------
<asp:Button id="cmdNext" accessKey="N" text="Next" width="125"
runat="server" ToolTip="Advances to the next page"
EnableViewState="False"></asp:Button>

<asp:Button id="cmdPrevious" accessKey="P" text="Previous" width="125"
runat="server" ToolTip="Goes to the previous page"
EnableViewState="False"></asp:Button>


Code-Behind-----------------------------------------------------------------------------------------------------------------

Public MustInherit Class Navigation

Inherits System.Web.UI.UserControl
Protected WithEvents cmdPrevious As System.Web.UI.WebControls.Button
Protected WithEvents cmdNext As System.Web.UI.WebControls.Button

Private Const PAGE_NAME As String = "PageUsingUserControls_"

Private m_intPage As Integer

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not IsPostBack Then
m_intPage = 1
ViewState("Page") = m_intPage
Else
m_intPage = ViewState("Page")
End If
If m_intPage = 1 Then cmdPrevious.Enabled = "false"
If m_intPage = 3 Then cmdNext.Enabled = "false"
End Sub

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdNext.Click

m_intPage += 1
ViewState("Page") = m_intPage
Response.Redirect(PAGE_NAME & m_intPage & ".aspx")
End Sub

Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdPrevious.Click

m_intPage -= 1
ViewState("Page") = m_intPage
Response.Redirect(PAGE_NAME & m_intPage & ".aspx")
End Sub
End Class
----------------------------------------------------------------------------------------------------------------------------

This code does not behave !

In the beginning it works correctly - m_intPage is 1. I press on Next and
m_intPage is incremented. Fine. But then, when page
"PageUsingUserControls_2" loads, it goes again through "m_intPage=1", makes
cmdPrevious disabled, and so on - m_intPage never becomes 3 !

Okay, so the Page_load of the Navigation Control is independent of the
Page_Load event of the containing page....

1. What is the sequence of events ? Which executes first ? Page_Load of the
page ? Or page_load of the user control ?

2. Why when loading in Page 2, my navigation control thinks that it is NOT
postback ? Actually, I think it is... Is this only because it is contained
in another page ?

3. Apparently I can't use the viewstate to control my pagecounter, because
each time my control will show in a new page, it will start a new viewstate.
In this case.... what should I do ? Write a text file with the value of
m_intPage - that doesn't seem that elegant, does it ? Create a cookie ? What
can I do ? What am I doing wrong here ? How would one solve this problem ?

Thank you.
Alex


 
Reply With Quote
 
 
 
 
John Saunders
Guest
Posts: n/a
 
      12-03-2004
"news.iq.ca" <(E-Mail Removed)> wrote in message
news:41b02454_4@aeinews....
> Hello,
>
> I am *VERY* confused - I understand that the Page_Load event of a user
> control is completely independent of the Page_Load event of the container
> page.
>
> I just started with User Controls and now I was trying my hand at creating
> a "navigation" control. Assuming that I name a sequence of pages like
> "PageUsingUserControls_1", "PageUsingUserControls_2" and
> "PageUsingUserControls_3",. I have written this code:
>

....

> Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles cmdPrevious.Click
>
> m_intPage -= 1
> ViewState("Page") = m_intPage
> Response.Redirect(PAGE_NAME & m_intPage & ".aspx")
> End Sub
> End Class
> ----------------------------------------------------------------------------------------------------------------------------
>
> This code does not behave !
>


ViewState is just within a page. You're redirecting to a different page,
where the ViewState has not been set.

Try using Session state instead.

John Saunders


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Materpage page_load event fired after content page_load? rockdale ASP .Net 1 11-16-2006 06:59 PM
page_load twice usercontrol Bart ASP .Net 0 12-31-2004 04:04 AM
in VB.NET Page_load of a base class called after the derived class Page_load ? z. f. ASP .Net 0 10-19-2004 12:01 PM
Why does page_load fire twice when inheriting from a common overridable Page_Load bminder ASP .Net 1 02-23-2004 08:54 PM
UserControl, button click and Page_Load? Olav Tollefsen ASP .Net 1 01-03-2004 08:41 PM



Advertisments