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>
5
  • Is you code is working? Commented Mar 1, 2017 at 7:31
  • Use var hdnfldVariable = document.getElementById("<%= hdnfldVariable.ClientID %>") if the hidden field ClientID is dynamic one. Commented Mar 1, 2017 at 7:31
  • thank for your answer , my code working , i tried to pass address input that powerd by script of google maps, so i think my code is dynamic. Commented Mar 1, 2017 at 7:36
  • take a look here forums.asp.net/t/… Commented Mar 1, 2017 at 7:36
  • If you are using a master page then document.getElementById('<%= hdnfldVariable.ClientID%>').value = autocomplete; It should then be visible in code behind. Commented Mar 1, 2017 at 7:40

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

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.

Comments

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

1 Comment

how can i use ajax? can you please typed it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.