Page_Load in UserControl

Discussion in 'ASP .Net Web Controls' started by news.iq.ca, Dec 3, 2004.

  1. news.iq.ca

    news.iq.ca Guest

    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
    news.iq.ca, Dec 3, 2004
    #1
    1. Advertising

  2. "news.iq.ca" <> 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
    John Saunders, Dec 3, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Olav Tollefsen

    UserControl, button click and Page_Load?

    Olav Tollefsen, Jan 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,004
    Steve C. Orr [MVP, MCSD]
    Jan 3, 2004
  2. bminder
    Replies:
    1
    Views:
    2,154
    nfedin
    Feb 23, 2004
  3. z. f.
    Replies:
    0
    Views:
    551
    z. f.
    Oct 19, 2004
  4. Bart

    page_load twice usercontrol

    Bart, Dec 31, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    356
  5. rockdale
    Replies:
    1
    Views:
    650
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Nov 16, 2006
Loading...

Share This Page