Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 19th, 2004, 07:07 PM
Registered User
 
Join Date: Feb 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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?
  #2 (permalink)  
Old February 19th, 2004, 09:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Harrisburg, PA, USA.
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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?
  #3 (permalink)  
Old February 20th, 2004, 04:35 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
  #4 (permalink)  
Old February 24th, 2004, 03:37 PM
Registered User
 
Join Date: Feb 2004
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

  #5 (permalink)  
Old February 24th, 2004, 05:14 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the steps to recreate this database? greglh BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 March 14th, 2008 11:08 AM
CheckListBox epkgupta Visual Studio 2005 0 March 11th, 2008 04:17 AM
Page_Load question wayneamurphy BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 4 November 6th, 2006 03:54 AM
page_load() hassel vijayapteET ASP.NET 1.0 and 1.1 Basics 2 October 6th, 2005 01:30 PM
Page_Load not working! New2ASPnet General .NET 6 July 28th, 2004 01:53 PM



All times are GMT -4. The time now is 03:56 PM.


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