0

I am trying to get value of Javascript variable on server side, but it always shows values on second button click and I don't want to reload this page again

    protected void Button1_Click(object sender, EventArgs e)
    {

        Page.ClientScript.RegisterStartupScript(Page.GetType(), "MessageBox", "<script language='javascript'>setValues();</script>");
        Thread.Sleep(5000);
        Response.Write(HiddenField1.Value);
    }

Javascript Code . . . ..

<script type="text/javascript">

    function setValues()
    {
        var abc = "thi is first";
        document.getElementById('<%=HiddenField1.ClientID%>').value = abc;
    }

</script>
Athar Raja
  • 19
  • 5
  • How do you expect to get it during the first load? It is calculated inside the javascript function that operates on the complete page. You can either duplicate the calculation logic in the C# code-behind that will set the `HiddenField.Value` if it is not PostBack, try to get the same value from stateless Javascript function with some [Javascript Engine](http://stackoverflow.com/questions/172753/embedding-javascript-engine-into-net) or just try to once more reconsider your design - think about [what you need and when](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Eugene Podskal Apr 12 '15 at 12:09
  • What is the purpose of doing this - I mean you are doing this - In code behind you are calling a JS to set value of hidden field and again there in code behind accessing that changed value, what is the point of doing that, you can access that directly in code behind! – Arindam Nayak Apr 12 '15 at 13:11
  • i am working with google map api and i am reading sms on server side while i am reading lat and lng from message at that time i want to get some values against lat , lng and then want that value on server side in same function on server side – Athar Raja Apr 13 '15 at 10:22

0 Answers0