CheckBoxがあるときのフォーカス復帰

CheckBox には今まで使っていたプログラムではポストバックから帰ってきたときのフォーカスの復帰が出来なかった。

原因はCheckBoxにonFocusを入れようとすると<span>でくくられてそこに入っていたから。
例)


<span onfocus="〜〜〜"><input id="CheckBox1" type="checkbox" name="CheckBox1" /></span>


そこでCheckBoxには他のコントロールとは別にonFocusをセットする為に文中の2行を加える

  ↓これ
Const SCRIPT_ONFOCUS_BY_CHECKBOX As String = "var elements = document.getElementsByTagName('input');" & _
"function onfocusHandler(){document.getElementById('__LASTFOCUS').value=document.activeElement.id;document.getElementById('txtLastFocus').value=document.getElementById('__LASTFOCUS').value;}" & _
"for (var i = 0; i < elements.length; i++) {if (elements[i].type == 'checkbox') {elements[i].attachEvent('onfocus', onfocusHandler);}}"


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack() Then
HookOnFocus(CType(Me.Page, Control))
End If
    ↓これ
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "ScriptOnFocusByCheckBox", SCRIPT_ONFOCUS_BY_CHECKBOX, True)
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "ScriptDoFocus", SCRIPT_DOFOCUS.Replace("REQUEST_LASTFOCUS", Request("__LASTFOCUS")), True)
End Sub


document.getElementById('__LASTFOCUS').value=document.activeElement.id;
CheckBoxの場合thi.idでは名前が取得出来ないようなのでアクティブはコントロールのidを取得する為にdocument.activeElement.idに変更