views:

376

answers:

1

I am deriving a control from syste.web.ui.webcontrols.button. I am then calling it buttonv2. I am then adding an arbitrary property to this new class, "int abc", accessing it via a get/set accessor that stores it's value in the viewstate (it also returns 0 if the viewstate value is null, i.e. i've tried to access it without setting it).

Now, whenever i give 'abc' a value, and then watch it in debug mode, it hits the 'set' portion of the accessor fine, and enters the value i give it (i.e. 55), then straight after, it's hitting some part of my code 'if (abc >0){}', where it gets the value from the viewstate as 0!

This is only happening when im using asp.net 'skins', and presetting the 'abc' value in the skin file. (i.e.) skin file reads: <asp:buttonv2 runat="server" SkinID="bob" abc="55" />

and in my page im using <asp:buttonv2 runat="server" SkinID="bob" /> and my prob is abc always equals 0?!

+1  A: 

I think the skin parameters get assigned during the initialisation of the control whereas the viewstate will only store after OnInit is complete.

From MS: "It tracks changes to these attributes only after the OnInit method is executed for a page request, and saves the changes to the page's or control's view state."

http://msdn.microsoft.com/en-us/library/ms178472.aspx

and

http://msdn.microsoft.com/en-us/library/system.web.ui.statebag.aspx

for more.

What happens if you just assign the value to a local variable on "set" and then add it to the viewstate from the "get" on page init/load?

Justin Wignall