1<%@ Page language="C#" debug="true"%> 2<%@ Register TagPrefix="mono" TagName="MonoSamplesHeader" src="~/controls/MonoSamplesHeader.ascx" %> 3<%@ Import namespace="System.ComponentModel" %> 4<%@ Import namespace="System.Globalization" %> 5<!-- This test was used to fix bug #59495 6 Authors: 7 Gonzalo Paniagua Javier (gonzalo@ximian.com) 8--> 9 10<html> 11<head> 12<title>ViewState + TypeConverter</title> 13<link rel="stylesheet" type="text/css" href="/mono-xsp.css"> 14<script runat="server"> 15 public class MyStuffConverter : TypeConverter 16 { 17 public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType) 18 { 19 // This is the second thing called. Type: string 20 HttpContext.Current.Response.Write ("<!-- CanConvertFrom called -->"); 21 return true; 22 throw new Exception (sourceType.ToString ()); 23 } 24 25 public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType) 26 { 27 // This is called first. Type: System.String 28 // return false -> cannot be serialized 29 HttpContext.Current.Response.Write ("<!-- CanConvertTo called -->"); 30 return true; 31 //throw new Exception (destinationType.ToString ()); 32 } 33 34 public override object ConvertFrom (ITypeDescriptorContext context, 35 CultureInfo culture, 36 object value) 37 { 38 // This is called after clicking the button, even before CanConvertFrom 39 HttpContext.Current.Response.Write ("<!-- ConvertFrom called -->"); 40 MyStuff ms = new MyStuff (); 41 ms.somevalue = Convert.ToInt32 (value); 42 return ms; 43 //throw new Exception (culture.Name); 44 } 45 46 public override object ConvertTo (ITypeDescriptorContext context, 47 CultureInfo culture, 48 object value, 49 Type destinationType) 50 { 51 HttpContext.Current.Response.Write ("<!-- ConvertTo called -->"); 52 // Third. culture.Name is null or "" -> Invariant. Destination: string 53 MyStuff ms = (MyStuff) value; 54 return ms.somevalue.ToString (culture); 55 //throw new Exception ("\"" + culture.Name + "\""+ " " + destinationType.Name); 56 } 57 } 58 59 [TypeConverter (typeof (MyStuffConverter))] 60 public class MyStuff { 61 public int somevalue; 62 } 63 64 void Page_Load () 65 { 66 if (IsPostBack) { 67 MyStuff old = (MyStuff) ViewState ["mystuff"]; 68 lbl.Text = String.Format ("<br><b>Old value:<b> {0}<br>", old.somevalue); 69 old.somevalue++; 70 ViewState ["mystuff"] = old; 71 } else { 72 MyStuff ms = new MyStuff (); 73 ViewState ["mystuff"] = ms; 74 } 75 } 76</script> 77</head> 78<body><mono:MonoSamplesHeader runat="server"/> 79<form runat="server"> 80<asp:button type="submit" runat="server" Text="Click here" /> 81<asp:label id="lbl" runat="server" /> 82</form> 83</body> 84</html> 85 86