473,396 Members | 2,029 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,396 software developers and data experts.

What am I missing? ViewState object changes type from derived to base on postback.

Hi,

I derived a class from the Hashtable class, with ISerializable implemented
and the [Serializable] tag added.

Everything works fine with one exception. I store the hashtable in
ViewState, but on a postback, the object in ViewState changes to Hashtable,
instead of MyHashtable.

I'm assuming this is a simple serialization setting, but I can't seem to
find the answer.

Can somebody give me a pointer?

Thanks in advance.
John
May 2 '06 #1
4 1408
what does your isearlizable implementation look like? can you post a short
but complete program demonstrating the issue? Short but complete is defined
here http://www.yoda.arachsys.com/csharp/complete.html

--

________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Professional VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------

"John" <pl*@dont.spam.me.com> wrote in message
news:EN*****************@news20.bellglobal.com...
Hi,

I derived a class from the Hashtable class, with ISerializable implemented
and the [Serializable] tag added.

Everything works fine with one exception. I store the hashtable in
ViewState, but on a postback, the object in ViewState changes to
Hashtable, instead of MyHashtable.

I'm assuming this is a simple serialization setting, but I can't seem to
find the answer.

Can somebody give me a pointer?

Thanks in advance.
John

May 2 '06 #2
"Alvin Bruney" <www.lulu.com/owc> wrote in message
news:OI**************@TK2MSFTNGP05.phx.gbl...
what does your isearlizable implementation look like? can you post a short
but complete program demonstrating the issue? Short but complete is
defined here http://www.yoda.arachsys.com/csharp/complete.html


Thanks Alvin,

Let me explain exactly what my sample program is doing.

1. I created a new class called MyHashtable, which inherits Hashtable.
2. The first time the page is loaded I instatiate a new MyHashtable object
and store it in ViewState.
3. Then I display the object type in the literal control. It is shown as
type MyHashtable.
4. I click the button
5. The object type is again shown in the literal control. But instead of
the expected MyHashtable type, it is shown as Hashtable (the base class).

Please notice in the code below, ISerializable is implemented for
MyHashtable. I believe this is necessary to accomplish my objective, but
please be aware the same thing happens even if the class is empty, like so:

public class MyHashtable : Hashtable {}

I know this is something disgustingly simple, but I can't figure out what.

Any help is appreciated. Please find code below.

Regards,
John

----------------------------
-- MyHashtable.cs
----------------------------
using System;
using System.Collections;
using System.Runtime.Serialization;
namespace DerivedHashTable
{
[Serializable]
public class MyHashtable : Hashtable, ISerializable
{
private int irrelevantVar; // misc variable

public MyHashtable() : base() {}

public MyHashtable(SerializationInfo info, StreamingContext context)
: base( info, context)
{
irrelevantVar = info.GetInt32( "irrelevantVar");
}

public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData(info,context);
info.AddValue( "irrelevantVar", irrelevantVar);
}
}
}
----------------------------

----------------------------
WebForm1.aspx
----------------------------
<%@ Page language="c#" %>
<%@ Import namespace="DerivedHashTable" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
ViewState["MyHashtable"] = new MyHashtable();

lit.Text = "Object type name = " +
ViewState["MyHashtable"].GetType().Name;
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Literal ID="lit" Runat="server"/><br>
<asp:Button Text="Postback" Runat="server"/>
</form>
</body>
</html>
----------------------------
May 2 '06 #3
I don't know the solution (never encountered the problem), but if you're
desperate, don't extend from Hashtable; just wrap it in your object. That
way the Session can't downcast your object.

"John" wrote:
"Alvin Bruney" <www.lulu.com/owc> wrote in message
news:OI**************@TK2MSFTNGP05.phx.gbl...
what does your isearlizable implementation look like? can you post a short
but complete program demonstrating the issue? Short but complete is
defined here http://www.yoda.arachsys.com/csharp/complete.html


Thanks Alvin,

Let me explain exactly what my sample program is doing.

1. I created a new class called MyHashtable, which inherits Hashtable.
2. The first time the page is loaded I instatiate a new MyHashtable object
and store it in ViewState.
3. Then I display the object type in the literal control. It is shown as
type MyHashtable.
4. I click the button
5. The object type is again shown in the literal control. But instead of
the expected MyHashtable type, it is shown as Hashtable (the base class).

Please notice in the code below, ISerializable is implemented for
MyHashtable. I believe this is necessary to accomplish my objective, but
please be aware the same thing happens even if the class is empty, like so:

public class MyHashtable : Hashtable {}

I know this is something disgustingly simple, but I can't figure out what.

Any help is appreciated. Please find code below.

Regards,
John

----------------------------
-- MyHashtable.cs
----------------------------
using System;
using System.Collections;
using System.Runtime.Serialization;
namespace DerivedHashTable
{
[Serializable]
public class MyHashtable : Hashtable, ISerializable
{
private int irrelevantVar; // misc variable

public MyHashtable() : base() {}

public MyHashtable(SerializationInfo info, StreamingContext context)
: base( info, context)
{
irrelevantVar = info.GetInt32( "irrelevantVar");
}

public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData(info,context);
info.AddValue( "irrelevantVar", irrelevantVar);
}
}
}
----------------------------

----------------------------
WebForm1.aspx
----------------------------
<%@ Page language="c#" %>
<%@ Import namespace="DerivedHashTable" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
ViewState["MyHashtable"] = new MyHashtable();

lit.Text = "Object type name = " +
ViewState["MyHashtable"].GetType().Name;
}
</script>
<html>
<body>
<form id="Form1" method="post" runat="server">
<asp:Literal ID="lit" Runat="server"/><br>
<asp:Button Text="Postback" Runat="server"/>
</form>
</body>
</html>
----------------------------

May 3 '06 #4

"William Sullivan" <Wi*************@discussions.microsoft.com> wrote in
message news:D2**********************************@microsof t.com...
I don't know the solution (never encountered the problem), but if you're
desperate, don't extend from Hashtable; just wrap it in your object. That
way the Session can't downcast your object.


Thanks William,

That's what I eventually wound up doing.

But I must say, I would like to know what was happening there.

If anybody knows, I'd love to have a clue thrown my way. ;-)

Regards,
John
May 5 '06 #5

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

Similar topics

4
by: Jim Strathmeyer | last post by:
I'm writing code with a whole lot of inheritance and am finding myself getting an object's type a lot. I do this by having a enumerated data member in the base class, and setting it in the derived...
5
by: Chris Capon | last post by:
Is there any way to cast a base class object to a derived class datatype when the derived class adds no new fields nor alters the object allocation in any way? The scenario would be where you...
1
by: leal ting | last post by:
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList lealting@hotmail.com] the class inherited...
2
by: ElanKathir | last post by:
Hi ! What is ViewState object ? What is use of that? and I wrote one Function in Server side and also Client Side. I want to run both one by one. Like Validation and Submit. Thanks &...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
5
by: Martin Jørgensen | last post by:
Hi, Consider this code: --- beginning of code --- #include <iostream> using namespace std; class Child{ public:
1
by: Allan Ebdrup | last post by:
I get the error: "Cannot create an object of type 'CustomWizard' from its string representation 'CustomWizard1' for the CustomWizard Property." when I view my custom server web control in...
9
by: bg_ie | last post by:
Hi, Is it possible to find out if an object is of a certain type or of a type derived from this type? Thanks, Barry
1
by: =?Utf-8?B?U2NvdHQ=?= | last post by:
Hello, Using VS2008 in a C# web service application, a class has been created that inherits from the ConfigurationSelection. This class file has been placed in the App_Code folder. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.