0

I have a problem, I want to pass javascript var to server side.

I want to pass the value of var autocomplete to string named Event_Address;

aspx.cs:

protected void ButtonAddEvent_Click(object sender, EventArgs e)
{
    string Event_Address =hdnfldVariable.Value;
}

aspx:

var autocomplete;

<script type="text/javascript">
    var somefunction = function() {
        var hdnfldVariable = document.getElementById('hdnfldVariable');
        hdnfldVariable.value = autocomplete;
    }
</script>

<asp:HiddenField ID="hdnfldVariable" runat="server" />

<asp:Button ID="ButtonAddEvent" runat="server" Text="הוסף אירוע חדש" 
        Font-Bold="True" Font-Names="Gisha" Font-Size="17pt" ForeColor="Blue" 
        onclick="ButtonAddEvent_Click" 
        style="z-index: 1; left: 497px; top: 774px; position: absolute" />
</asp:Content>
Mikhail Tulubaev
  • 4,141
  • 19
  • 31

4 Answers4

1

In addition to teo van kot's answer, if the ClientID for HiddenField is a dynamic one, it should be defined as dynamic ClientID like this:

var autocomplete = "[somevalue]";

    <script type="text/javascript">
    var somefunction = function() {
        // set this if you have dynamic ClientID
        var hdnfldVariable = document.getElementById("<%= hdnfldVariable.ClientID %>");
        hdnfldVariable.value = autocomplete;
    }
    </script>

      <asp:HiddenField ID="hdnfldVariable" runat="server" />

       <asp:Button ID="ButtonAddEvent" runat="server" Text="הוסף אירוע חדש" 
        Font-Bold="True" Font-Names="Gisha" Font-Size="17pt" ForeColor="Blue" 
        onclick="ButtonAddEvent_Click"
    OnClientClick="somefunction();"
        style="z-index: 1; left: 497px; top: 774px; position: absolute" />
</asp:Content>

Reference:

Passing values from javascript to code behind in ASP.NET

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
0

If you want to call your js function on Button click you shold add it to OnClientClick attribute. like this:

var autocomplete;

    <script type="text/javascript">
    var somefunction = function() {
        var hdnfldVariable = document.getElementById('hdnfldVariable');
        hdnfldVariable.value = autocomplete;
    }
    </script>

      <asp:HiddenField ID="hdnfldVariable" runat="server" />

       <asp:Button ID="ButtonAddEvent" runat="server" Text="הוסף אירוע חדש" 
        Font-Bold="True" Font-Names="Gisha" Font-Size="17pt" ForeColor="Blue" 
        onclick="ButtonAddEvent_Click" 
        OnClientClick="somefunction();"
        style="z-index: 1; left: 497px; top: 774px; position: absolute" />
</asp:Content>
teo van kot
  • 12,350
  • 10
  • 38
  • 70
0

Either you can save the value of your "autocomplete" to hidden field or you can use ajax method to call a c# function , to pass the value from front-end to back end.

or in your case use the attrbute property to change the value .

please refer this link for the ajax call Ajax method call

Community
  • 1
  • 1
0

You can use [Web Method] in server side. You should add using System.Web.Services; to your cs code for using web methods. Here is the steps:

  • Change your button; use html button instead of server side button.
  • Create js function and call it with onclick event in button.
  • Pass your variable with PageMethods.YourWebMethod(yourParam); in js.
  • Then you have it in server side web method. Here is an ex for you:

     function JsFunctionOnclick(){ 
      PageMethods.MyPageMethod(myParameter);
     }
    

    and this is server side:

    [WebMethod]
    public static void MyPageMethod(string param)
    {
        //do something
    }
    

Be sure your web method is static Good luck.

Flardryn
  • 457
  • 1
  • 10
  • 25