Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Variable from JS to VB.net

I have a variable that gets a value in a js function. I need to get its value as a double into a vb.net variable.

I have tried putting the variable into a label then grabbing it from the label in vb.net as shown in the code below:

Js part.

document.getElementById("Label1").innerText = nwLat;

then in the vb part

   Dim nwLat As Double
    nwLat = Label1.Text
    MsgBox(nwLat)

it does not work for me any ideas? the error that comes up is Input string was not in a correct format.

Cheers!

like image 231
Sam Avatar asked Aug 18 '11 15:08

Sam


2 Answers

Easiest way without any type of ajax would be to use a hidden field.

Markup:

<asp:HiddenField ID="nwLatHidden" runat="server" Value="" />

JS:

document.getElementById('nwLatHidden').value = '6.00'; // or of course the value from your function.

.NET during your postback routine:

Dim nwLat As Double
nwLat = nwLatHidden.Value
like image 50
cillierscharl Avatar answered Nov 14 '22 23:11

cillierscharl


In JavaScript:

__doPostBack(
    'edit',
    JSON.stringify({ ID: id, Code: code, AcctNo: acctNo })
);

In VB.NET:

Protected Sub Page_Load(s As Object, e As EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        '...
    Else
        Dim eventTarget As String = Request.Form!__EVENTTARGET
        Dim eventArgument As String = Request.Form!__EVENTARGUMENT
        If Not eventArgument Is Nothing AndAlso Not eventTarget Is Nothing AndAlso eventTarget = "edit" Then
            Dim jss As New JavaScriptSerializer()
            Dim ac As AccountCode = jss.Deserialize(Of AccountCode)(eventArgument)
        End If
    End If
End Sub
like image 33
shawndumas Avatar answered Nov 15 '22 00:11

shawndumas