0

I am trying to pass the value of a variable from javascript to C#.

Here is the client side part:

  <input type="text" value="1" id="h" name="n" runat="server" />
  <script>
    $("a").on("tap", function (event) {
    var a = event.target.id;
    $("#h").val(a);
    });
  </script>          

and the server side part:

<%
string param = h.Value;
Response.Write(param);
            %>

The input field has an initial value=1. A JQuery event changes the value attribute to, let's say, 5. When I try to access the value of input from server side (h.Value), the value is again, 1.

What am I doing wrong?

Thank you.

Kostas
  • 1
  • 5
    There is a fundamental problem here. Changes on the browser happen on the browser. They have nothing to do with the server and you can't access them from the server. To communicate from the browser to the server you need to send an HTTP request from the client back to the server (e.g. via an AJAX request or form post). – James Gaunt Feb 14 '15 at 15:44
  • http://stackoverflow.com/questions/958040/what-is-ajax-really | http://api.jquery.com/jquery.ajax/ – Sumurai8 Feb 14 '15 at 15:58
  • That's why the input field has the "runat" attribute. It is an aspx page. – Kostas Feb 14 '15 at 16:05
  • The runat attribute makes no difference. It in no way connects the browser to the server directly so they always "see" the same data. However if you are posting back - and this is winforms, your HTML markup doesn't look right. You probably need an ASP control (e.g. . It depends on the framework, and what happened to get the state back to the server. – James Gaunt Feb 14 '15 at 16:56
  • @James Gaunt Eventually, I used url parameters. – Kostas Feb 15 '15 at 21:24
  • What about this: http://stackoverflow.com/a/17301481/2576398 – Shago Feb 18 '15 at 19:05

1 Answers1

0

I beleive you need to use an ASP.NET TextBox control instead of a plain html input.

Shago
  • 605
  • 4
  • 12
  • @James Gaunt Using an ASP.NET control has the same effect. It seems that posting the value with AJAX is unavoidable. – Kostas Feb 15 '15 at 09:20