Groups | Blog | Home
all groups > asp.net building controls > may 2004 >

asp.net building controls : Listbox in Custom Control doesn't keep listitems added on Page_Load of container page


Joshua Gunder via .NET 247
5/13/2004 9:58:11 AM
This is my first attempt at building my own compiled custom=
control=2E I have a custom control that contains a textbox and a=
listbox=2E
In a given aspx=2Evb class I will instantiate my custom control and=
bind its listbox to a datareader, (If Not PostBack)=2E The custom
control loads on the page with my textbox and listbox below it=
and everything is fine=2E However if I add a submit button the
containing page and click it, the list comes back empty the next=
time=2E 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=2E Note: I didnt' know of a good way to access the=

textbox and listbox so I created "Get=2E=2E=2E" functions that return=
references to these webControls=2E ANy help would be appreciated,=
thanks=2E

BTW, I tried posting this message before, but I couldn't find it=
in the list=2E 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=2E Its like a
circus in here with no explanation as to how the site works=2E

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

Imports System=2EWeb=2EUI
Imports System=2EWeb=2EUI=2EWebControls


Public Class FilterList : Inherits WebControl

Private _txtFilter As New TextBox
Private _lstItems As New ListBox

Protected Overrides Sub Render(ByVal output As=
System=2EWeb=2EUI=2EHtmlTextWriter)

_txtFilter=2EID =3D "txtFilterBox"
_txtFilter=2EWidth =3D Me=2EWidth

_lstItems=2EID =3D "lstItems"
_lstItems=2EWidth =3D Me=2EWidth
_lstItems=2EHeight =3D Me=2EHeight

_txtFilter=2EAttributes=2EAdd("onkeyup", "LoadNearest('" &=
_txtFilter=2EID & "', '" & _lstItems=2EID & "');")

output=2EWrite(GenerateClientScript)
output=2EWrite("<table cellpadding=3D""0"" cellspacing=3D""0""=
border=3D""0"">" & vbCrLf & _
"<tr><td>" & vbCrLf)
_txtFilter=2ERenderControl(output)
output=2EWrite("</td></tr><tr><td>")
_lstItems=2ERenderControl(output)
output=2EWrite("</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 =2ENET 247 (http://www=2Edotnet247=2Ecom/)

Teemu Keiski
5/13/2004 8:05:24 PM
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


[quoted text, click to view]
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>

AddThis Social Bookmark Button