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

    Thanks
    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
    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #767867
    Hi,

    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.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767879
    Hi,

    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" />
    <script>
    function getScreenSize() {
    var myW = document.getElementById("hiddenW");
    myW.value = window.innerWidth;
    var myH = document.getElementById("hiddenH");
    myH.value = window.innerHeight;
    }
    </script>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </form>
    </body>

    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
    Hi,

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

    clientside:
    window.onload = function() {
    document.getElementById('<%=hidden1.ClientID%>').value=20;
    };

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

    Code behind:
    Page_Load()
    {
    textbox1.text= hidden1.value;
    }



    Hope this will help you

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring


Sign In to post your comments