从受保护的子VB.NET调用Page_Load

时间:2015-12-28 17:39:31

标签: vb.net

我有一个正面的ASPX页面,它显示了几个按钮,并预先形成了所需的代码。问题是我正在读取一个继电器,所以我的网页中继的当前状态总是落后于我的页面刷新,所以我想我只需要从Page_Load事件开始,以使这种情况正确发生并重新生成状态继电器。

我的问题是,如何从按钮Click?

中调用Page_Load()
'Global Gate Interface
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim tcpClient As New TcpClient()

    'Connect to webrelay
    tcpClient.Connect("xx.xx.xx.xx", 80)

    'Create a network stream object
    Dim netStream As NetworkStream = tcpClient.GetStream()

    'Create the XML command to send to Webrelay
    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("GET /state.xml HTTP/1.1" & vbCrLf & "Authorization: Basic bm9uZTp3ZWJyZWxheQ==" & vbCrLf & vbCrLf)

    'Send the command to webrelay
    netStream.Write(sendBytes, 0, sendBytes.Length)

    'Get the response from webrelay
    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    netStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

    'Convert the bytes received from Webrelay into a string
    Dim returndata As String = Encoding.ASCII.GetString(bytes)

    'Convert the string into and array
    Dim array1 As Char() = returndata.ToCharArray()

    'Relay State found at index 66 of array
    If array1(66) = "1" Then
        relayState.Text = "ON"
    Else
        relayState.Text = "OFF"
    End If

    'Input State found at index 94 of array
    If array1(94) = "1" Then
        inputState.Text = "ON"
    Else
        inputState.Text = "OFF"
    End If

    'Close the connection
    tcpClient.Close()


End Sub

'Turn East relay On
Protected Sub relayOnEast_Click(sender As Object, e As EventArgs) Handles eastOpen.Click

    Dim tcpClient As New TcpClient()

    'Connect to webrelay
    tcpClient.Connect("xx.xx.xx.xx", 80)

    'Create a network stream object
    Dim netStream As NetworkStream = tcpClient.GetStream()

    'Create the XML command to send to Webrelay
    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("GET /state.xml?relayState=1 HTTP/1.1<CR><LF>" & vbCrLf & "Authorization: Basic bm9uZTp3ZWJyZWxheQ==<CR><LF><CR><LF>" & vbCrLf & vbCrLf)

    'Send the command to webrelay
    netStream.Write(sendBytes, 0, sendBytes.Length)

    'Close the connection
    tcpClient.Close()

'RERUN PAGE LOAD HERE?



End Sub

2 个答案:

答案 0 :(得分:1)

只需将代码从Page_Load()移至单独的私有方法,然后在Page_Load()relayOnEast_Click()中调用该方法。

附注:这可能不起作用,因为您必须注意ASPX页面的生命周期。

答案 1 :(得分:0)

将受保护子区域内的代码放入一个单独的子区域,如下所示 然后从表单加载事件中调用new sub。然后你可以从你的其他子调用相同的代码。

Protected sub PageReload
    Dim tcpClient As New TcpClient()

    'Connect to webrelay
    tcpClient.Connect("xx.xx.xx.xx", 80)

    'Create a network stream object
    Dim netStream As NetworkStream = tcpClient.GetStream()

    'Create the XML command to send to Webrelay
    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("GET /state.xml HTTP/1.1" & vbCrLf & "Authorization: Basic bm9uZTp3ZWJyZWxheQ==" & vbCrLf & vbCrLf)

    'Send the command to webrelay
    netStream.Write(sendBytes, 0, sendBytes.Length)

    'Get the response from webrelay
    Dim bytes(tcpClient.ReceiveBufferSize) As Byte
    netStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

    'Convert the bytes received from Webrelay into a string
    Dim returndata As String = Encoding.ASCII.GetString(bytes)

    'Convert the string into and array
    Dim array1 As Char() = returndata.ToCharArray()

    'Relay State found at index 66 of array
    If array1(66) = "1" Then
        relayState.Text = "ON"
    Else
        relayState.Text = "OFF"
    End If

    'Input State found at index 94 of array
    If array1(94) = "1" Then
        inputState.Text = "ON"
    Else
        inputState.Text = "OFF"
    End If

    'Close the connection
    tcpClient.Close()
End Sub