Search code examples
c#javascriptjqueryasp.netcode-behind

asp.net passing values from JS/jquery to code behind c#


I have tried "every" possible way of sending the screen.width vlaue from the JS script on the aspx page to the c# in the code behind and while I can see the screen.width being assigned correctly it never gets assigned to my hidden field value.

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

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

   <script type="text/javascript" language="javascript">
       $(function(){ 
       $('#hiddenfield').val(screen.width); 
    });
    </script>

other content

</asp:Content>

and the code behind:

protected void btnChartGo_Click(object sender, EventArgs e)
{
   string s = hiddenfield.Value;
}

No matter what I try s is always ""

Something wrong with the above, everyone seems to be doing it like that and it works?


Solution

  • This should get the right selector:

    $('#<%= hiddenfield.ClientID %>').val(screen.width);