Home > Blockchain >  Cannot access html value set by script from backend
Cannot access html value set by script from backend

Time:11-23

I have an html input tag with runat="server" and id. A jquery function (slider) sets the value of the input field. However, I am not able to access the value of the input control on the server side. How do I get the value of the input control?

jquery slider function snippet:

    var num = $('#body_num1')
    $('#slider').slider({
     min:0,
     max:100, 
     slide: function(event, ui){
        num.val(ui.values[0]);
     }
    });

the asp.net code:

    <input type="text" id="num1" readonly runat="server"/>

when button doSomething is cliked (it should access num1's value):

    Protected Sub btn_doSomething_ServerClick(sender As Object, e As EventArgs)
      Dim num1_input As String = num1.Value
      System.Diagnostics.Debug.Writeline(num1_input)
    End Sub

I get the JIT Compiler encountered an internal limitation error.

CodePudding user response:

Try this:

        <div id="MyTestSlider" style="width:30%">
        </div>

        <asp:HiddenField ID="MySlideValue" runat="server" ClientIDMode="Static"/>
        <asp:TextBox ID="TextBox1" runat="server" ClientIDMode="Static"></asp:TextBox>
                    
        <asp:Button ID="Button1" runat="server" Text="Button" />

        
        <script>

            $('#MyTestSlider').slider({
                min: 0,
                max: 100,
                step: 5,
                slide: function (e) {
                    slideValue = $('#MyTestSlider').slider("value")
                    $('#TextBox1').val(slideValue)
                    $('#MySlideValue').val(slideValue)

                }
                });

        </script>

And thus code behind is this:

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Debug.Print("Slider value = " & MySlideValue.Value)
    Debug.Print("Slider value in text box = " & TextBox1.Text)


End Sub

So I have both a hidden field, and a text box. You of course don't need both.

  • Related