473,382 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

ASP.Net Server Custom control

Tom
I am developing a page that will contain multiple instances of a
Composite Custom Control that i have developed. The problem is that
the user will determine at run time how many of the control will be
added to the page at runtime (through add or delete button clicks). I
have created a class for the control(PersonPanel), and have created a
collection class(PersonPanelCollection)to hold and store all the
controls at runtime, but due to the page lifecycle in ASP.net, the
control collection(PersonPanelCollection) goes out of scope after the
page is destroyed. I read that i need to persist this object to the
page's viewstate(so as to hold all the information in the control
collection) and on each page request recast this control back to the
collection class(PersonPanelCollection). When i attempted this it
gave me an error saying i need to make the collection
class(PersonPanelCollection) serializable or create a type converter
for the class. From my understanding, a type converter is the way to
go over serialization for performance reasons - more data written to
the viewstate. I have since written a TypeConverter for each the
Control class(PersonPanel) and it collection
class(PersonPanelCollection), but i cannot cast the control to a
string to be written to the viewstate(VS.net complains that the class
does not support conversion to this type), nor if i use the
..ToString() method of the classes does it return any data. What do i
need to do to get this to work? Does the type conversion happen
automatically by just doing a cast, or is there something more to
this? My code is below:

[ToolboxData("<{0}:PersonPanel runat=server></{0}:PersonPanel>"),
TypeConverter(typeof(PersonPanelConverter))]
public class PersonPanel : System.Web.UI.WebControls.Panel,
INamingContainer
{
.... Person Panel class constructor, methods, etc.

}

public class PersonPanelConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{

if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
if (value is string)
{
...Logic to do the conversion and return the new PersonPanel object
}
return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(ITypeDescriptorContext Context,
CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
...Logic to do the conversion and return the string

}
return base.ConvertTo(context, culture, value, destinationType);
}
}


[ TypeConverter(typeof(PersonPanelCollectionConverte r))]
public class PersonPanelCollection :
System.Collections.CollectionBase, INamingContainer
{
public PersonPanelCollection()
{
}

public PersonPanel this[ int index ]
{
get
{
return( (PersonPanel) List[index] );
}
set
{
List[index] = value;
}
}
public int Add( PersonPanel value )
{
return(List.Add( value ) );
}

public int IndexOf(int value )
{
return( List.IndexOf( value ) );
}

public void Insert( int index, PersonPanel value )
{
List.Insert( index, value );
}

public void Remove(PersonPanel value )
{
List.Remove( value );
}
}
public class PersonPanelCollectionConverter : TypeConverter
{

public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{

if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}

public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
{
if (value is string)
{

..Logic to to create a new PersonPanelCollection, and use the
PersonPanel's TypeConverter to create a new panel for each of the
panel's in the collection - cannot cast as VS complains that it does
not support the case for each panel so i use the ToString() method

}
return base.ConvertFrom(context, culture, value);
}

public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
{
StringBuilder tempSB = new StringBuilder();

if (destinationType == typeof(string))
{
...create the string representation of the object and return it

}
return base.ConvertTo(context, culture, value, destinationType);
}
}
Nov 18 '05 #1
0 1273

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
2
by: Brian | last post by:
NOTE ALSO POSTED IN microsoft.public.dotnet.framework.aspnet.buildingcontrols I have solved most of my Server Control Collection property issues. I wrote an HTML page that describes all of the...
0
by: Ron | last post by:
I've an aspx page with a custom (composite) server control and a TextBox, both with RequiredFieldValidators. I'd like the custom control to behave the same way the default TextBox does when one...
2
by: Barbara Alderton | last post by:
I setup some standard Required Field Validation controls and one Custom validation control on an ASP.NET page (within a user control) to validate text entry. I also setup a Summary Control to post...
9
by: AFN | last post by:
I was just dropped into someone else's code (isn't that always so fun?). I can't figure out why a custom validation control's server event function is executing. There is nothing (that I see)...
7
by: Shimon Sim | last post by:
I have a custom composite control I have following property
2
by: Steve Franks | last post by:
In ASP.NET 2.0 you can now apparently do this: <asp:label runat="server" text="some browser" IE:text="any IE browser" IE5:text="the IE 5 browser" PIE:text="the Pocket PC browser" /> Now the...
1
by: Sam Samnah | last post by:
Hi Everyone. It has been a long time since my last post. Nevertheless, I have built a custom server control that allows a user to edit text, bolding, italics strike though table insertion and...
4
by: DKode | last post by:
I have developed a custom server control that displays a login page that authenticates against AD. The server control works fine, but now I am trying to figure out the best way to output custom...
2
by: Michal Valent | last post by:
I would like to fire some custom server control event before Page_Load event like this (from the trace of an aspx page) : Trace Information Category Message From First(s) From Last(s) aspx.page...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.