如何从调用非静态方法asp.net使用jQuery的代码隐藏

Within a web form I have the following jQuery setup to call a method in my code behind file.

 <script src="js/jquery-1.7.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(function () {
            $("#newBtn").click(function () {
                $.ajax({
                    type: "POST",
                    url: "AJAXCaller.aspx/GetTimer",
                    data: "{}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    async: true,
                    cache: false

                });
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <a href="#" class="link1" runat="server">Click here</a><br /><br />
    <asp:LinkButton ID="newBtn" runat="server" Text="ASP Link" />
    <asp:Label ID="time_lbl" runat="server" />
    </div>
    </form>
</body>

And within the code behind I have the following:

[WebMethod]
        public void GetTimer()
        {
            time_lbl.Text = DateTime.Now.ToString();
        }

I realize this currently does not work in the current syntax.

I've been able to access the method if I set it to static however I don't want to just return a string or single value. I want to access a method that in turn access a web service to return a collection of data.

I would like to know if it is possible to perform something like this where I can call a method and have it update a field in the UI such as the above method where when called the server side script would to the ui a value for the date field?

Thanks in advance for any suggestions.

#0

It is not at all possible because you cannot update any field on UI from server side.

Instead of using WebMethod in aspx page use Asp.Net webservices instead to write web methods which can be called using ajax.

ajax method has a success callback where you can grab the service response and then update the UI field by accessing it with appropriate selector.

E.g

    $(function () {
        $("#newBtn").click(function () {
            $.ajax({
                type: "POST",
                url: "AJAXCaller.asmx/GetTimer",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                cache: false,
                success: function(data){
                     $("#ClientIdOftime_lbl").text(data);
                }

            });
            return false;
        });
    });

推荐文章

完全失去了“安装”OpenCV/cTypesOpencvforPython3

完全失去了“安装”OpenCV/cTypesOpencvforPython3

推荐文章

通过Spring从请求填充集合?

通过Spring从请求填充集合?

推荐文章

这是什么Ruby语法?

这是什么Ruby语法?

推荐文章

是否可以为ASP.NET Web窗体应用程序定义堆栈?

是否可以为ASP.NET Web窗体应用程序定义堆栈?

推荐文章

SQL Server从datetime中删除毫秒

SQL Server从datetime中删除毫秒

推荐文章

是否有一种方法可以检查点是否与矩形相交?

是否有一种方法可以检查点是否与矩形相交?

推荐文章

在Python中从文件夹导入多个文件

在Python中从文件夹导入多个文件

推荐文章

如何从Informix网络连接处获取完整的主机名?

如何从Informix网络连接处获取完整的主机名?

推荐文章

为什么这是哈斯克尔SDL线青色时,应该是白色?

为什么这是哈斯克尔SDL线青色时,应该是白色?

推荐文章

有人能解释一下为什么javascript中的这个特性不起作用吗?

有人能解释一下为什么javascript中的这个特性不起作用吗?

推荐文章

删除Javascript中的所有多个空格并替换为单个空格

删除Javascript中的所有多个空格并替换为单个空格

推荐文章

WCF.Net 4.0客户端反序列化回复消息正文时出错

WCF.Net 4.0客户端反序列化回复消息正文时出错

推荐文章

iphone的可变内存问题

iphone的可变内存问题

推荐文章

为什么此代码会导致0?

为什么此代码会导致0?

推荐文章

我在主题文件中看到的“role”属性的用途

我在主题文件中看到的“role”属性的用途

推荐文章

在Eclipse中,“Open Type”和“Open Resource”有什么区别?

在Eclipse中,“Open Type”和“Open Resource”有什么区别?