0

I have the code needed to pass a variable from javascript to vb.net, but it is not working for some reason and I can't seem to figure out what is wrong with the code. Please help. I declared a hidden variable and it stores the value and is then posted back, but the variable in the codebehind file is always empty.

Aspx code (EDITED:)

            <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication3.WebForm1" %>

            <%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>

            <!DOCTYPE html>

            <html xmlns="http://www.w3.org/1999/xhtml">
            <head runat="server">
                <title></title>
            </head>
            <body>
                <form id="form1" runat="server">
                           <asp:HiddenField ID="nwLatHidden" runat="server" Value="" />
                           <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
                               <Scripts>
                                   <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js">
                                   </asp:ScriptReference>
                                   <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js">
                                   </asp:ScriptReference>
                                   <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js">
                                   </asp:ScriptReference>
                               </Scripts>
                           </telerik:RadScriptManager>
                           <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
                           </telerik:RadAjaxManager>
                           <telerik:RadHtmlChart runat="server" ID="RadHtmlChart1">
                                <Legend>
                                    <Appearance Position="Bottom">
                                        <TextStyle FontSize="14" Color="Blue" FontFamily="Courier New, sans-serif" />
                                    </Appearance>
                                </Legend>
                                <PlotArea>
                                    <XAxis>
                                        <Items>
                                            <telerik:AxisItem LabelText="KM41872"/>
                                            <telerik:AxisItem LabelText="KM41873"/>
                                            <telerik:AxisItem LabelText="KM41871"/>
                                        </Items>
                                        <MajorGridLines Visible="false" />
                                        <MinorGridLines Visible="false" />
                                    </XAxis>
                                    <YAxis>
                                        <MinorGridLines Visible="false" />
                                    </YAxis>
                                    <Series>
                                        <telerik:LineSeries Name="Mean">
                                            <SeriesItems>
                                                <telerik:CategorySeriesItem Y="0.59" />
                                                <telerik:CategorySeriesItem Y="0.63" />
                                                <telerik:CategorySeriesItem Y="0.6" />
                                                <telerik:CategorySeriesItem Y="0.65" />
                                                <telerik:CategorySeriesItem Y="0.64" />
                                                <telerik:CategorySeriesItem Y="0.63" />
                                                <telerik:CategorySeriesItem Y="0.65" />
                                                <telerik:CategorySeriesItem Y="0.67" />
                                                <telerik:CategorySeriesItem Y="0.63" />
                                            </SeriesItems>
                                        </telerik:LineSeries>
                                        <telerik:LineSeries Name="Min">
                                            <SeriesItems>
                                                <telerik:CategorySeriesItem Y="0.55" />
                                                <telerik:CategorySeriesItem Y="0.56" />
                                                <telerik:CategorySeriesItem Y="0.55" />
                                                <telerik:CategorySeriesItem Y="0.61" />
                                                <telerik:CategorySeriesItem Y="0.56" />
                                                <telerik:CategorySeriesItem Y="0.57" />
                                                <telerik:CategorySeriesItem Y="0.59" />
                                                <telerik:CategorySeriesItem Y="0.61" />
                                                <telerik:CategorySeriesItem Y="0.55" />
                                            </SeriesItems>
                                        </telerik:LineSeries>
                                        <telerik:LineSeries Name="Max">
                                            <SeriesItems>
                                                <telerik:CategorySeriesItem Y="0.66" />
                                                <telerik:CategorySeriesItem Y="0.74" />
                                                <telerik:CategorySeriesItem Y="0.66" />
                                                <telerik:CategorySeriesItem Y="0.71" />
                                                <telerik:CategorySeriesItem Y="0.72" />
                                                <telerik:CategorySeriesItem Y="0.73" />
                                                <telerik:CategorySeriesItem Y="0.71" />
                                                <telerik:CategorySeriesItem Y="0.74" />
                                                <telerik:CategorySeriesItem Y="0.71" />
                                            </SeriesItems>
                                        </telerik:LineSeries>
                                    </Series>
                                </PlotArea>
                            </telerik:RadHtmlChart>
                </form>
                <script type="text/javascript">
                    document.getElementById('nwLatHidden').value = '6.00';
                </script>
            </body>
            </html>

Codebehind: Public Class WebForm1 Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Dim nwLat As String
            nwLat = nwLatHidden.Value.ToString


    End Sub

    End Class
hysoftwareeng
  • 487
  • 3
  • 15
  • Read these articles to [learn how to](http://www.creativebloq.com/javascript/javascript-debugging-beginners-3122820) [**debug** JavaScript](https://developers.google.com/chrome-developer-tools/docs/javascript-debugging), so you can provider more useful context information (and help yourself). Then you would find that you get an error like "Can't access property value of null". – Felix Kling Jul 09 '14 at 15:12
  • I don't think my DOM doesnt exist, I am simply passing a constant, not searching the DOM – hysoftwareeng Jul 09 '14 at 15:15
  • You are executing `document.getElementById('nwLatHidden')` before the element appears in the document. You can't get a reference to an element that doesn't exist yet. `getElementById` *does search* the DOM for an element with that ID (albeit in a very efficient manner). – Felix Kling Jul 09 '14 at 15:16
  • oh ok, sorry, I am really new to javascript, I will double check. Thanks for the comment – hysoftwareeng Jul 09 '14 at 15:17
  • I have moved the script code down before the body tag – hysoftwareeng Jul 09 '14 at 15:23
  • it is still empty for some reason.... – hysoftwareeng Jul 09 '14 at 15:48
  • Do a view source and check the ID of your hidden field. The .NET ID isn't always the same as the id in the compiled html version of the page. – the_lotus Jul 09 '14 at 15:58

1 Answers1

0

try this

document.getElementById('<%=nwLatHidden.clientID').value = '6.00';

Behzad
  • 767
  • 6
  • 25