在code执行JavaScript函数的背后 - 如何获取返回值code的背后 [英] Executing JavaScript function in Code behind -- How to Get the Return Value in Code Behind

查看:121
本文介绍了在code执行JavaScript函数的背后 - 如何获取返回值code的背后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的JavaScript函数,该函数调用jQuery的JSON功能,相对于时区获得日期时间。这工作得很好。

I have following JavaScript function, Which call Jquery JSON function and get the DateTime with respect to timezone. This works fine.

  <script type="text/javascript">
    function JSFunctionName() {
        $(document).ready(function () {
            var timezone = "US/Central";
            $.getJSON("http://json-time.appspot.com/time.json?tz=" + timezone + "&callback=?",
function (data) {
    if (data.hour < 12) {
        //alert(data.hour + ':' + data.minute); // want to return this value data.hour + data.minute
        //document.getElementById('<%=HiddenField1.ClientID %>').value = data.hour + ':' + data.minute;// this does not work
    }
})
        });
    }
</script>

现在我正在按钮的onclick调用code这个Javascription功能背后

Now I am calling this Javascription function in Code behind on onclick of button

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

后面的 code

protected void Button1_Click(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(),
        "alert", "JSFunctionName();", true);
    // here I need the DateTime value that is get from JSON
    //Response.Write(HiddenField1.Value); 
}

我怎样才能从Javascript Page.ClientScript.RegisterStartupScript

请注意我有尝试在HiddenField设置的值,但它不工作。你可以在注释中看到的。

Please note I have try to set the value in HiddenField, but its not working. you can see in the comment.

任何想法或替代解决方案将成为AP preciated。

Any idea or alternative solution will be appreciated.

感谢

推荐答案

不回发到服务器你不能做到这一点。这样做的原因是,的JavaScript执行客户端,并在页面离开服务器后,将只执行。

You can't do this without posting back to the server. The reason for this is that javascript executes on the client, and it will only execute after the page has left the server.

我想这是一个人为的例子,但在这种特殊情况下,如果你想在客户端和服务器上提供相同的信息您需要计算在服务器,然后通上该出到客户端。

I assume this is a contrived example, but in this specific case, if you want to have the same information available on the client and server, you need to compute it on the server, and pass that out to the client.

如果无法做到这一点,你需要创建一个web服务,但必须异步处理的响应。

If this isn't possible, you'll need to create a webservice, but that will have to handle the response asynchronously.

这篇关于在code执行JavaScript函数的背后 - 如何获取返回值code的背后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆