Fixing the Button Bug in Internet Explorer
September 7th, 2006Yesterday I learned that Internet Explorer 6 and 7 both handle the button improperly. It seems that instead of sending the value of the value attribute as a part of the form submission, it actually sends whatever happens to be the inner HTML of the button tag. That causes trouble when you embed an image into that tag so show an icon along with the text for the button.
When this markup is included in the submission it causes a validation error because it is a security risk to allow HTML markup to be handled by the server. To fix that validation error you must prevent the markup from being included in the PostBack. I thought this was an interesting problem, so I created a test page so I could attempt to fix it.
Initially I used Javascript to change the value of the value attribute to just Text for the button control, to purposely exclude the included markup. That did prevent the validation error, but strangely it caused another problem. The PostBack failed to properly identify which button sent the request.
Finally the solution was to simply disable the buttons instead of resetting the value so that the markup and text is not sent to the server at all. This prevents the error and calls the right PostBack handler.
My test pages now include a few scenarios which you can try for yourself: Button Bug.
Below is the VB.NET code in the ASP.NET which I used to solve the problem.
Dim key1 As String = "Fix-" & ImageTextButton1.ClientID
If Not Page.ClientScript.IsOnSubmitStatementRegistered(key1) Then
Dim script As String = "if (document.all) {" & vbCrLf & _
"var btn1 = document.all['" & ImageTextButton1.ClientID & "']; " & _
"btn1.disabled = 'true';" & vbCrLf & "}" & vbCrLf
Page.ClientScript.RegisterOnSubmitStatement(Me.GetType(), key1, script)
End If
Dim key2 As String = "Fix-" & ImageTextButton2.ClientID
If Not Page.ClientScript.IsOnSubmitStatementRegistered(key2) Then
Dim script As String = "if (document.all) {" & vbCrLf & _
"var btn2 = document.all['" & ImageTextButton2.ClientID & "']; " & _
"btn2.disabled = 'true';" & vbCrLf & "}" & vbCrLf
Page.ClientScript.RegisterOnSubmitStatement(Me.GetType(), key2, script)
End If
End Sub
