N
news.iq.ca
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
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