p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Basics (http://p2p.wrox.com/forumdisplay.php?f=60)
-   -   Page_Load on recreate CheckListBox (http://p2p.wrox.com/showthread.php?t=9576)

pjirelan February 19th, 2004 07:07 PM

Page_Load on recreate CheckListBox
 
From other posts page_init and page_load are run each time before other controls like button_click. If the page_load creates a checklistbox and new items in the list. When the user selects items then clicks the button isn't the checkboxlist recreated and selected items lost?

bmains February 19th, 2004 09:05 PM

If you need the checklistbox to stay on the form, either create it at the design level in the page and alter the visibility, or if you need something special with the checklistbox, create a custom control.

Why do you need to create it in the page_load event?

planoie February 20th, 2004 04:35 PM

This is what happens you create controls and add them to the page programmatically:

Stage 1: Initial page hit (no postback)
1) ASPX markup is parsed (with no dynamic controls)
2) Page_load(): you add controls to the page's controls collection.
3) Page does whatever else it needs (i.e. fires events, etc)
4) States and values of all page controls (INCLUDING dynamic controls) are saved to the page's viewstate
5) All controls are rendered

Stage 2: form postback
1) ASPX markup is parsed (with no dynamic controls)
2) Control values and states are restored from viewstate. Controls who's values have changed assume the new (posted) value.
3) Page_load(): you add controls to the page's controls collection.
3a) When each control is loaded and added to the page, the framework attempts to match it up with information in viewstate to restore its condition.
4) Page does whatever else it needs (i.e. fires events, etc)
4) States and values of all page controls (INCLUDING dynamic controls) are saved to the page's viewstate
5) All controls are rendered

As long as you use the same control ID when you create the controls, they should restore correctly. If you need to preset a control that is created dynamically, you should only preset it when the page is in its first instance (i.e., If Not IsPostback ...).

Peter
------------------------------------------------------
Work smarter, not harder.

pjirelan February 24th, 2004 03:37 PM

Here is the form class with code. Basically its loads a checkboxlist with 3 items and a submit button. The user selects items then hits the submit button. When I select items then hit submit the mycheck.Items(i).Selected is never true.

Public Class WebForm1
    Inherits System.Web.UI.Page

    Dim mytext As String
    Protected WithEvents CheckList As System.Web.UI.WebControls.CheckBoxList
    Protected WithEvents CheckListPlace As System.Web.UI.WebControls.PlaceHolder
    Protected WithEvents SubmitButton As System.Web.UI.WebControls.Button
    Protected WithEvents DebugLabel As System.Web.UI.WebControls.Label
    Dim count As Integer

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' CheckList.AutoPostBack = False
        CheckList.ID = "CheckList"
        CheckList.CellPadding = 2
        CheckList.CellSpacing = 2
        CheckList.RepeatDirection = RepeatDirection.Vertical
        CheckList.RepeatLayout = RepeatLayout.Table
        CheckList.TextAlign = TextAlign.Right

        If Not Page.IsPostBack Then
            CheckList.Items.Add(New ListItem("Item1"))
            CheckList.Items.Add(New ListItem("Item2"))
            CheckList.Items.Add(New ListItem("Item3"))
            CheckList.DataBind()
        End If

        SubmitButton.Text = "Fill Out Details"
        AddHandler SubmitButton.Click, AddressOf SubmitButton_Click
        If CheckList.Items.Count = 0 Then
            DebugLabel.Text = "NO ITEMS"
        End If
        CheckListPlace.Controls.Add(CheckList)

    End Sub

    Private Sub SubmitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' Make sure a control was found.
        Dim sel As Integer
        Dim s As String
        Dim i As Integer
        Dim mycheck As CheckBoxList = _
                   CType(CheckListPlace.FindControl("CheckList"), CheckBoxList)

        If Not mycheck Is Nothing Then
            s = "Selected items:<br>"
            For i = 0 To mycheck.Items.Count - 1
                If mycheck.Items(i).Selected Then
                    s = s & mycheck.Items(i).Text & "<br>"
                End If
            Next i
            DebugLabel.Text = s
        End If
    End Sub
End Class


planoie February 24th, 2004 05:14 PM

Try creating each list item for the checkbox list with an ID. I would think that the checkboxlist would restore from viewstate correctly, but maybe you need each item to have an ID.

Peter
------------------------------------------------------
Work smarter, not harder.


All times are GMT -4. The time now is 11:12 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.