You must Sign In to post a response.
  • Category: JavaScript

    How to access the js value in code behind(in server side)

    Hello Sir

    I have some problem regarding access the value

    I wan to use some value in code behind on_load() which will set from the javascript window.onload()
    and it will display always blank

    is there any way to set value in js(onload()) and access in code behind

    Bhavik Shah
  • #767851
    if you want to access some value on_Page_load() in code being why don't you directly code it on page_load why you want to use client side scripting, you can easily fetch data from database and access it from code behind
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767867

    if you want to access your server side value into client side and client side value into server side the best approach is using Hidden Fields, using hidden fields we can easily accessible the values.

    Give respect to your work, Instead of trying to impress your boss.

    Blog :

  • #767879

    you can use hidden fields to pass the values from client to server side and vice versa.
    Refer below example for this.

    <body onload="getScreenSize()">
    <form id="form1" runat="server">

    <input type="hidden" name="hiddenW" ID="hiddenW" runat="server" />
    <input type="hidden" name="hiddenH" ID="hiddenH" runat="server" />
    function getScreenSize() {
    var myW = document.getElementById("hiddenW");
    myW.value = window.innerWidth;
    var myH = document.getElementById("hiddenH");
    myH.value = window.innerHeight;
    <asp:Button ID="Button1" runat="server" Text="Button" />

    Code behind:
    public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    Response.Write(hiddenW.Value+" by " +hiddenH.Value);


  • #767900

    Refer the below..
    We can pass data from Javascript to the C# code by using hiddenfields.

    window.onload = function() {

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

    Code behind:
    textbox1.text= hidden1.value;

    Hope this will help you

    Never lose hope..You never know what tomorrow will bring

  • Sign In to post your comments