简体   繁体   中英

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!

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

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

您需要使用ClientID或UniqueID:

document.getElementById("<%=Label1.UniqueID%>").innerText = "Hi";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM