I'm in a situation where I need a listbox to remember after postback the attributes I've added to a number of the listitems in it. I found this solution onlin开发者_开发知识库e which appears to solve the issue but am not sure how to implement it.
List box solution
He says he wrote a class that inherits from Listbox which is fine, I've done that and have called it EnhancedListBox but how do I then apply that to the Listbox I'm using on the page?
I can't just substitute
<asp:ListBox >
with
<asp:EnhancedListBox>
but how else do I let the page know I want to use my inherited code?
Thanks.
SaveViewState
and LoadViewState
are virutal methods that you can override. What you want to try is creating "your own" ListBox
:
class EnhancedListBox : ListBox
{
protected override object SaveViewState()
{
// code here from the tutorial
}
protected override void LoadViewState(object savedState)
{
// code here from the tutorial
}
}
This is also called "Creating a custom control", it's very common to do it this way and it gives you great flexibility.
you need to create a custom control for this check this out http://msdn.microsoft.com/en-us/library/ms366537.aspx for the startup.
精彩评论