PrivateSub Page_Load()Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load txtPassword.Attributes.Add("value", txtPassword.Text)‘’‘’是这一句 If Page.IsPostBack =FalseThen Try SetFormFocus(txtUsername) Catch 'control not there or error setting focus EndTry EndIf Dim objSystemMessages AsNew SystemMessageController lblLogin.Text = objSystemMessages.FormatSystemMessage(PortalId, "MESSAGE_LOGIN_INSTRUCTIONS", UserId) End Sub
二,打开页面时自动将焦点放到指定位置
'set focus to any control PublicSub SetFormFocus()Sub SetFormFocus(ByVal control As Control) IfNot control.Page IsNothingAnd control.Visible Then If control.Page.Request.Browser.JavaScript =TrueThen ' Create JavaScript Dim sb AsNew System.Text.StringBuilder sb.Append("<SCRIPT LANGUAGE='JavaScript'>") sb.Append("<!--") sb.Append(ControlChars.Lf) sb.Append("function SetInitialFocus() {") sb.Append(ControlChars.Lf) sb.Append(" document.") ' Find the Form Dim objParent As control = control.Parent WhileNotTypeOf objParent Is System.Web.UI.HtmlControls.HtmlForm objParent = objParent.Parent EndWhile sb.Append(objParent.ClientID) sb.Append("['") sb.Append(control.UniqueID) sb.Append("'].focus(); }") sb.Append("window.onload = SetInitialFocus;") sb.Append(ControlChars.Lf) sb.Append("// -->") sb.Append(ControlChars.Lf) sb.Append("</SCRIPT>") ' Register Client Script control.Page.RegisterClientScriptBlock("InitialFocus", sb.ToString()) EndIf EndIf End Sub
' convert datareader to dataset '转换DATAREADER为dataset PublicFunction ConvertDataReaderToDataTable()Function ConvertDataReaderToDataTable(ByVal reader As IDataReader) As DataTable ' create datatable from datareader Dim objDataTable AsNew DataTable Dim intFieldCount AsInteger= reader.FieldCount Dim intCounter AsInteger For intCounter =0To intFieldCount -1 objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter)) Next intCounter ' populate datatable objDataTable.BeginLoadData() Dim objValues(intFieldCount -1) AsObject While reader.Read() reader.GetValues(objValues) objDataTable.LoadDataRow(objValues, True) EndWhile reader.Close() objDataTable.EndLoadData() Return objDataTable End Function