从访问的WebMethod控制ASP [英] Accessing ASP control from webmethod

查看:157
本文介绍了从访问的WebMethod控制ASP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了一些ASP 2.0 web表单code,查询一个WebMethod并返回一个字符串,它的一个简化版本会

I've inherited some ASP 2.0 webforms code that queries a webmethod and returns a string, a simplified version of it would be

code背后

<System.Web.Services.WebMethod()> _
Public Function StockLevel() as String
    return "120"
End Sub

.aspx页面中

.aspx Page

function GetStockLevel() {
$.ajax({
    type: 'POST',
    url: 'Stock.aspx/StockLevel',
    // data: '{ }',
    contentType: 'application/json; charset=utf-8',
    dataType: 'text',
    success: function (data) {
    alert(data);
    }
});
}

该网页上有一个asp的标签控件例如ASP:标签ID =stockLabel=服务器

The page has a asp label control on it e.g asp:Label id="stockLabel" runat="server"

目前它返回字符串120到jQuery的呼叫,并显示警报,当我尝试修改StockLevel函数来设置标签文本120如

Currently it returns the string 120 into the jquery call and displays the alert, when i try to modify the StockLevel function to set the label text to 120 such as

stockLabel.Text = "120"

我得到智能感知,但它不会出现在网页上更新的价值,我想这是由于AJAX的性质(在这种情况下,我应该只使用返回值从AJAX调用设置的值的标签。),并且该控制可能是不此时装载或有一个范围的问题。它是否正确?我想知道为什么会这样,这是正确的行为,我应该想到还是我做错了什么,标签应与正确的值更新?

I get intellisense, but it does not appear to update the value on the page, i guess this is due to the nature of AJAX (In which case i should just use the return value from the ajax call to set the value of the label.), and the control is probably not loaded at this point or there is a scope issue. Is this correct? I'd like to know why this happens, is this the correct behaviour i should expect or am i doing something wrong and the label should update with the correct value?

任何指针或建议将是巨大的。

Any pointers or advice would be great.

推荐答案

我相信ASP标签得到呈现为跨度,你应该能够改变它,像这样:

I believe ASP Labels get rendered as Spans, you should be able to change it like so:

success: function (data) {
    $("#<%=stockLabel.ClientID %>").text(data);
}

至于从Web访问方法页面控件,你不准。这篇文章进入了很多比我更可以:<一href=\"http://stackoverflow.com/questions/2133194/access-asp-net-control-from-static-webmethod-js-ajax-call\">Access从静态[的WebMethod](JS ajax调用)ASP.NET控件

这篇关于从访问的WebMethod控制ASP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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