Listbox in Custom Control doesn't keep listitems added on Page_Load of container page

Discussion in 'ASP .Net Building Controls' started by Joshua Gunder via .NET 247, May 13, 2004.

  1. This is my first attempt at building my own compiled customcontrol. I have a custom control that contains a textbox and alistbox.
    In a given aspx.vb class I will instantiate my custom control andbind its listbox to a datareader, (If Not PostBack). The custom
    control loads on the page with my textbox and listbox below itand everything is fine. However if I add a submit button the
    containing page and click it, the list comes back empty the nexttime. Why isn't viewstate for child controls in a custom controlmaintained? How do I accomplish this? Below is my code for thecustom control. Note: I didnt' know of a good way to access the
    textbox and listbox so I created "Get..." functions that returnreferences to these webControls. ANy help would be appreciated,thanks.

    BTW, I tried posting this message before, but I couldn't find itin the list. While full of information, this site is
    horribly painful to navigate you can never figure out where youare, or why some topics are different colors, etc. Its like a
    circus in here with no explanation as to how the site works.

    ----------------------

    Imports System.Web.UI
    Imports System.Web.UI.WebControls


    Public Class FilterList : Inherits WebControl

    Private _txtFilter As New TextBox
    Private _lstItems As New ListBox

    Protected Overrides Sub Render(ByVal output AsSystem.Web.UI.HtmlTextWriter)

    _txtFilter.ID = "txtFilterBox"
    _txtFilter.Width = Me.Width

    _lstItems.ID = "lstItems"
    _lstItems.Width = Me.Width
    _lstItems.Height = Me.Height

    _txtFilter.Attributes.Add("onkeyup", "LoadNearest('" &_txtFilter.ID & "', '" & _lstItems.ID & "');")

    output.Write(GenerateClientScript)
    output.Write("<table cellpadding=""0"" cellspacing=""0""border=""0"">" & vbCrLf & _
    "<tr><td>" & vbCrLf)
    _txtFilter.RenderControl(output)
    output.Write("</td></tr><tr><td>")
    _lstItems.RenderControl(output)
    output.Write("</td></tr></table>")

    End Sub

    Public Function GetTextBox() As TextBox
    Return _txtFilter
    End Function

    Public Function GetListBox() As ListBox
    Return _lstItems
    End Function

    End Class

    ----------------------

    --------------------------------
    From: Joshua Gunder

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>97+4V033dk2M4ec/5utMew==</Id>
    Joshua Gunder via .NET 247, May 13, 2004
    #1
    1. Advertising

  2. Joshua Gunder via .NET 247

    Teemu Keiski Guest

    Hi,

    1) Implement INamingContainer interface in your control
    2) Create instances of child controls in overridden CreateChildControls
    method and add them to the Controls collection of your control. This way
    child controls participate in the control lifecycle and will also keep their
    state.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke


    "Joshua Gunder via .NET 247" <> wrote in message
    news:%...
    This is my first attempt at building my own compiled custom control. I have
    a custom control that contains a textbox and a listbox.
    In a given aspx.vb class I will instantiate my custom control and bind its
    listbox to a datareader, (If Not PostBack). The custom
    control loads on the page with my textbox and listbox below it and
    everything is fine. However if I add a submit button the
    containing page and click it, the list comes back empty the next time. Why
    isn't viewstate for child controls in a custom control maintained? How do I
    accomplish this? Below is my code for the custom control. Note: I didnt'
    know of a good way to access the
    textbox and listbox so I created "Get..." functions that return references
    to these webControls. ANy help would be appreciated, thanks.

    BTW, I tried posting this message before, but I couldn't find it in the
    list. While full of information, this site is
    horribly painful to navigate you can never figure out where you are, or why
    some topics are different colors, etc. Its like a
    circus in here with no explanation as to how the site works.

    ----------------------

    Imports System.Web.UI
    Imports System.Web.UI.WebControls


    Public Class FilterList : Inherits WebControl

    Private _txtFilter As New TextBox
    Private _lstItems As New ListBox

    Protected Overrides Sub Render(ByVal output As
    System.Web.UI.HtmlTextWriter)

    _txtFilter.ID = "txtFilterBox"
    _txtFilter.Width = Me.Width

    _lstItems.ID = "lstItems"
    _lstItems.Width = Me.Width
    _lstItems.Height = Me.Height

    _txtFilter.Attributes.Add("onkeyup", "LoadNearest('" & _txtFilter.ID
    & "', '" & _lstItems.ID & "');")

    output.Write(GenerateClientScript)
    output.Write("<table cellpadding=""0"" cellspacing=""0""
    border=""0"">" & vbCrLf & _
    "<tr><td>" & vbCrLf)
    _txtFilter.RenderControl(output)
    output.Write("</td></tr><tr><td>")
    _lstItems.RenderControl(output)
    output.Write("</td></tr></table>")

    End Sub

    Public Function GetTextBox() As TextBox
    Return _txtFilter
    End Function

    Public Function GetListBox() As ListBox
    Return _lstItems
    End Function

    End Class

    ----------------------

    --------------------------------
    From: Joshua Gunder

    -----------------------
    Posted by a user from .NET 247 (http://www.dotnet247.com/)

    <Id>97+4V033dk2M4ec/5utMew==</Id>
    Teemu Keiski, May 13, 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. Guest
    Replies:
    1
    Views:
    406
    William F. Robertson, Jr.
    Oct 28, 2003
  2. Simon Prince
    Replies:
    2
    Views:
    3,308
    =?Utf-8?B?TWljaGFlbCBUYXlsb3I=?=
    Oct 19, 2004
  3. Bennett Haselton
    Replies:
    1
    Views:
    503
    John M Deal
    Nov 8, 2004
  4. Vikram

    ListItems as checkbox in a listbox

    Vikram, Mar 1, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    4,967
    Wilco Bauwer
    Mar 1, 2005
  5. DC
    Replies:
    2
    Views:
    1,172
Loading...

Share This Page