8

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!

4
  • 1
    What isn't working? Does the code throw an Exception? Is the value simply empty? We need more details... Commented Aug 18, 2011 at 15:49
  • Input string was not in a correct format. is the error Commented Aug 18, 2011 at 15:50
  • is Label1 the actual id of the label in the HTML? Commented Aug 18, 2011 at 15:50
  • The label's text value will be maintained in and retrieved from ViewState, so this is unlikely to work. Try using an <input type="hidden" /> instead. Commented Aug 18, 2011 at 15:53

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

it Errors with ~Input string was not in a correct format at nwLat = CDbl(nwLatHidden.value)
Do you know how to attach a debugger? What is the value of nwLatHidden before trying to cast it to a double?
See my edit above and check the result to see if it can be parsed into the double type.
2

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

Comments

1

You need to use the ClientID or the UniqueID:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.