Public Shared Sub RegisterClientScriptForRFIDObject(ByVal page As Page)
Dim csInitRFID As [String] = "InitRFID"
Dim csWaitRFIDEvent As [String] = "WaitRFIDEvent"
Dim cstype As Type = page.[GetType]()
Dim cs As ClientScriptManager = page.ClientScript
Dim sb As System.Text.StringBuilder = Nothing
If Not cs.IsStartupScriptRegistered(cstype, csInitRFID) Then
sb = New System.Text.StringBuilder()
sb.AppendLine("var loaded;")
sb.AppendLine("var rf40 = new ActiveXObject('IDLinkLibrary.CardReader');")
sb.AppendLine("document.body.onload = InitPort;")
sb.AppendLine("document.body.onunload = ClosePort;")
sb.AppendLine("function ClosePort()")
sb.AppendLine("{")
sb.AppendLine(" if (rf40 != null && loaded) rf40.ClosePort();")
sb.AppendLine("}")
sb.AppendLine("function InitPort()")
sb.AppendLine("{")
sb.AppendLine(" loaded = false;")
sb.AppendLine(" if (rf40 == null) rf40 = new ActiveXObject('IDLinkLibrary.CardReader');")
sb.AppendLine(" port = rf40.GetCommPort();")
sb.AppendLine(" if (port.indexOf('Err') > -1)")
sb.AppendLine(" {")
sb.AppendLine(" port = window.prompt('Please fill in serial port for RFID Reader:', '');")
sb.AppendLine(" if (port == null) port = '';")
sb.AppendLine(" }")
sb.AppendLine(" if (rf40.OpenPort(port))")
sb.AppendLine(" {")
sb.AppendLine(" loaded = true;")
sb.AppendLine(" }")
sb.AppendLine("}")
cs.RegisterStartupScript(cstype, csInitRFID, sb.ToString(), True)
End If
If Not cs.IsStartupScriptRegistered(cstype, csWaitRFIDEvent) Then
sb = New System.Text.StringBuilder()
sb.AppendLine("<script type='text/jscript' language='jscript'>")
sb.AppendLine(" function rf40::OnReadCard(cardSN)")
sb.AppendLine(" {")
sb.AppendLine(" if (loaded)")
sb.AppendLine(" {")
sb.AppendLine(" __doPostBack('__Page', cardSN);")
sb.AppendLine(" }")
sb.AppendLine(" }")
sb.AppendLine(" function rf40::OnReadError(errorMessage)")
sb.AppendLine(" {")
sb.AppendLine(" if (loaded)")
sb.AppendLine(" {")
sb.AppendLine(" alert('Please click OK then scan the card again.');")
sb.AppendLine(" }")
sb.AppendLine(" }")
sb.AppendLine("</script>")
cs.RegisterStartupScript(cstype, csWaitRFIDEvent, sb.ToString(), False)
End If
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RegisterClientScriptForRFIDObject(Me)
If Me.IsPostBack = true Then
Me.txtRName.Text = Request("__EVENTARGUMENT")
End If
End Sub