jump to navigation

VB.NET Code Behind For XMPP Project April 5, 2012

Posted by jinzaistudio in ASP.NET 3.5, jinzai-studio.net, Michael T. Blake, programming, Uncategorized, Visual Basic.
trackback

This blog post will show the VisualBasic code behind that I use to pass information to the client side Javascript that produces the client’s requests. ASPX itself is a server side paradigm and script is the client’s way of passing information back to the server. The mechanism for this is a postback, which involves a roundtrip and the usual response to that is a complete reload of the page and loss of information. The mechanism that is ordinarily used to persist information is either one of the server side entities, such as the Session object or Viewstate, or the HTML mechanism known as hidden fields. All of these accompany the page itself and are available to different extents to the programmer. The method I chose was hidden fields and the server fills the fields by calling client side Javascript to pass variables to the code that are used to piece together the client’s request. It is not an elegant solution, but I have created a nice implementation of it, at least. (Or so I tell myself…..) Here is the page’s code behind:

Imports System.Xml
Imports System.IO

Partial Class ChatterBox
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) Handles Me.Load
        If Not (Page.IsPostBack) Then
            SetHiddenFieldValue("idFrom", "mtb@jinzai-studio.net")
            SetHiddenFieldValue("idHold", "1")
            SetHiddenFieldValue("idRid", "1573741820")
            SetHiddenFieldValue("idRoute", "xmpp:jinzai-studio.net:9999")
            SetHiddenFieldValue("idTo", "jinzai@jinzai-studio.net")
            SetHiddenFieldValue("idVer", "1.6")
            SetHiddenFieldValue("idWait", "60")
        End If
    End Sub

    Protected Sub btnSend_Click(ByVal sender As Object, _
                                ByVal e As System.EventArgs) _
                                Handles btnSend.Click
        Dim theScriptBuilder As New StringBuilder

        theScriptBuilder.Append("Send('")
        theScriptBuilder.Append(TextBox1.Text)
        theScriptBuilder.Append("');")

        ClientScript.RegisterStartupScript(Me.GetType(), _
                                           Guid.NewGuid().ToString(), _
                                           theScriptBuilder.ToString, _
                                           True)
    End Sub


    Private Sub SetHiddenFieldValue(ByVal theField As String, _
                                    ByVal theValue As String)
        Dim theScriptBuilder As New StringBuilder

        theScriptBuilder.Append("SetHiddenFieldValue('")
        theScriptBuilder.Append(theField)
        theScriptBuilder.Append("', '")
        theScriptBuilder.Append(theValue)
        theScriptBuilder.Append("');")

        ClientScript.RegisterStartupScript(Me.GetType(), _
                                           Guid.NewGuid().ToString(), _
                                           theScriptBuilder.ToString, _
                                           True)
    End Sub
End Class

Next up is the Javascript to create the client’s requests.

Advertisements

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: