Ketone Cops

August 19, 2008

Invalid character in a Base-64 string (ASP.NET)

Filed under: ASP.NET 2.0,programming — delroger @ 11:16 am

Just came across a very peculiar problem with a website I’m developing. Some visitors to the website hit an error every time they click one of the controls on a particular page; the page loads fine the first time, but any sort of postback triggers the error. The message is:

Invalid character in a Base-64 string

and the stack trace is:

at System.Convert.FromBase64String(String s) at System.Web.UI.ObjectStateFormatter.Deserialize(String inputString) at System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Deserialize(String serializedState) at System.Web.UI.Util.DeserializeWithAssert(IStateFormatter formatter, String serializedState) at System.Web.UI.HiddenFieldPageStatePersister.Load()

The problem seems to be with the ViewState. The ViewState is encrypted, and when an attempt is made to decrypt it on postback, the error is triggered.  The solution is actually quite simple: in the web.config file, set the ViewState not to be encrypted, like this:

 <pages viewStateEncryptionMode=”Never”>


OK, the error is no longer produced, but why is it there in the first place?  The page works perfectly well for some visitors, but not others – and that’s when the setup is exactly the same (Windows XP, IE 7) and the same requests are going to the server.  So what’s in the ViewState for different users that is creating the problem?  Still trying to find out…

As an addendum to this post one week on, I did work out why the encryption of the ViewState was a problem.  There was quite a lot of data being stored in the ViewState: above a certain length the ViewState decryption cuts off, and therefore the string doesn’t appear like a valid one for decryption.  You can solve this by splitting the ViewState into several sections using the MaxPageStateFieldLength in the pages tag in web.config – see this post for instance:

Therefore, if you want to maintain encryption on your ViewState, that’s a slightly better solution for you. Personally, I’m not convinced it matters greatly since if someone wants to decrypt your ViewState, it is trivial enough to do anyhow (see here for instance).

August 6, 2008

Creating javascript alerts in ASP.NET with UpdatePanel (or without)

Filed under: Ajax,ASP.NET 2.0,Javascript,programming,UpdatePanel — delroger @ 8:33 pm

Just thought I’d share a couple of simple methods for creating javascript alert messages from an ASP.NET webpage, both when you are using AJAX (and an update panel) or just from a standard page.

In a standard page, you can easily create an alert message in the code-behind like this: 

ClientScript.RegisterClientScriptBlock(Me.GetType(), “yourkeyname”, “alert(‘hello’);”, True)

The parameters that are passed in are the System.Type (the Page type in this case), an arbitrary name for your script ‘key’, the javascript itself as a string, and a boolean indicating whether to add the script tags – i.e., <script type=”javascript”></script> – which you might as well use since it shortens the script itself.

Likewise, creating an alert when you are using an UpdatePanel is slightly different, but simpler than you might think – you just need to make sure the System.Type is the UpdatePanel type, and use the ScriptManager rather than the ClientScript like so:

ScriptManager.RegisterStartupScript(Me.UpdatePanel1, Me.UpdatePanel1.GetType(), “yourkeyname”, “alert(‘hello’);”, True)

This is much the same as before except you are passing in the ID of your UpdatePanel along with the other parameters.

Now, since this is code that you are likely to want to re-use throughout an application, it makes sense to create a Class file for it with a couple of generic methods, like this:

Imports Microsoft.VisualBasic
Imports System.Web
Imports System.Web.UI
Public Class Messages
    Public Shared Sub CreateMessageAlertInUpdatePanel(ByVal up As UpdatePanel, ByVal strMessage As String)
        Dim strScript As String = “alert(‘” & strMessage & “‘);”
        Dim guidKey As Guid = Guid.NewGuid()
        ScriptManager.RegisterStartupScript(up, up.GetType(), guidKey.ToString(), strScript, True)
    End Sub
    Public Shared Sub CreateMessageAlert(ByVal strMessage As String)
        Dim guidKey As Guid = Guid.NewGuid()
        Dim pg As Page = HttpContext.Current.Handler
        Dim strScript As String = “alert(‘” & strMessage & “‘);”
        pg.ClientScript.RegisterStartupScript(pg.GetType(), guidKey.ToString(), strScript, True)
    End Sub
End Class

(We’re just using the Guid for the script key to ensure a new key each time we create an alert)

And that’s it! You can now create alert messages from any of your webpages simply by using:

CreateMessageAlertInUpdatePanel(Me.UpdatePanel1, “hello”)


Blog at