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

  • Thread starter Joshua Gunder via .NET 247
  • Start date
J

Joshua Gunder via .NET 247

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
 
T

Teemu Keiski

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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top