Public variable set in OnLoad event of ListBox doesn't persist - why?

Last post 11-04-2007 3:41 AM by mombassa. 4 replies.

Sort Posts:

  • Public variable set in OnLoad event of ListBox doesn't persist - why?

    04-18-2007, 3:29 PM
    • Member
      17 point Member
    • se_gordon
    • Member since 02-07-2007, 9:21 PM
    • Posts 48

    I have two ListBoxes in a DetailsView that are visible only in Edit mode, lbAllPeople and lbSelectedPeople.  I am populating lbSelectedPeople in its OnLoad event based on data in SQL Server.

     

    The user can select a person from lbAllPeople and click an Add button, which adds the ListItem to lbSelectedPeople and also causes a postback.  The OnLoad event for lbSelectedPeople fires again and executes all its code, which I don’t want it to do; I want the code to execute only the first time it loads.

     

    I can’t use if (!IsPostBack) because the original load does occur in a PostBack.

     

    I’ve tried setting a public Boolean that says the page is already loaded, but the value of that Boolean doesn’t persist when the page is posted back ... I’m setting it to true in the OnLoad event but it gets set back to false before the Add button is clicked.  I don’t understand why.

     

    Thanks for any help!

  • Re: Public variable set in OnLoad event of ListBox doesn't persist - why?

    04-18-2007, 4:58 PM
    • Star
      10,657 point Star
    • ps2goat
    • Member since 11-17-2006, 10:43 PM
    • Posts 1,944
    Move the code that loads data into its own function that requires you to pass a listbox that will be filled.  Then all you have to do is call FillListbox(myListBox) to load the data, and it only happens when you explicitly call it.

    I.e.

    Private Sub FillListbox(ByVal LB As ListBox)
      Lb.Items.Add()' add items however you are now.
    End Sub
    
    

     

    I'm assuming there's some kind of event (like a button click) that causes the listbox to load? (You said it loads originally during a postback.)  In your click event, do the following:

    Protected Sub Btn1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn1.Click
      ' this button was clicked, so we want to load the listbox.
      Call FillListbox(lbSelectedPeople)
    End Sub

     
    ---------------------------------------
    MCP - Web Based Client Development .NET 2.0
  • Re: Public variable set in OnLoad event of ListBox doesn't persist - why?

    04-18-2007, 6:08 PM
    • Member
      17 point Member
    • se_gordon
    • Member since 02-07-2007, 9:21 PM
    • Posts 48

     The event that causes the listbox to load is the mode changing to Edit in the detailsview.

    My original approach was to attempt to fill the listbox as follows:

    protected void DetailsView1_ModeChanged(object sender, EventArgs e)

    {

    if (DetailsView1.CurrentMode.Equals(DetailsViewMode.Edit))

     ListBox lbSelectedPeople = DetailsView1.FindControl("lbSelectedComposers") as ListBox;

    // then fill the box}

    However, FindControl returns null here because the DetailsView is in read only mode when the page is loaded, and the listbox is not part of the read only view.

    Therefore, I still can't figure out a solution that allows me to fill the box once and only once.

    Any ideas?

  • Re: Public variable set in OnLoad event of ListBox doesn't persist - why?

    04-18-2007, 10:08 PM
    Answer
    • Member
      17 point Member
    • se_gordon
    • Member since 02-07-2007, 9:21 PM
    • Posts 48

     

    After reading this thread

    http://forums.asp.net/1537657/ShowThread.aspx#1537657

     yet again, I was able to solve this problem by doing a FindControl in the ItemCreated event.

     Thank you to ps2goat for responding and to everyone who participated in the aforementioned thread.

     

     

  • Re: Public variable set in OnLoad event of ListBox doesn't persist - why?

    11-04-2007, 3:41 AM
    • Member
      94 point Member
    • mombassa
    • Member since 05-17-2006, 5:46 PM
    • Vancouver, BC
    • Posts 30

    Viewstate baby, VIEWSTATE

Page 1 of 1 (5 items)
Microsoft Communities