vb.net 从 webmethod 访问 ASP 控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16178938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Accessing ASP control from webmethod
提问by norbert
I've inherited some ASP 2.0 webforms code that queries a webmethod and returns a string, a simplified version of it would be
我继承了一些查询 webmethod 并返回一个字符串的 ASP 2.0 webforms 代码,它的简化版本是
Code Behind
背后的代码
<System.Web.Services.WebMethod()> _
Public Function StockLevel() as String
return "120"
End Sub
.aspx Page
.aspx 页面
function GetStockLevel() {
$.ajax({
type: 'POST',
url: 'Stock.aspx/StockLevel',
// data: '{ }',
contentType: 'application/json; charset=utf-8',
dataType: 'text',
success: function (data) {
alert(data);
}
});
}
The page has a asp label control on it e.g asp:Label id="stockLabel" runat="server"
该页面上有一个 asp 标签控件,例如 asp:Label id="stockLabel" runat="server"
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
目前,当我尝试修改 StockLevel 函数以将标签文本设置为 120 时,它会将字符串 120 返回到 jquery 调用中并显示警报,例如
stockLabel.Text = "120"
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?
我得到了智能感知,但它似乎没有更新页面上的值,我想这是由于 AJAX 的性质(在这种情况下,我应该只使用 ajax 调用的返回值来设置标签的值。 ),并且此时可能未加载控件或存在范围问题。这样对吗?我想知道为什么会发生这种情况,这是我应该期望的正确行为还是我做错了什么并且标签应该用正确的值更新?
Any pointers or advice would be great.
任何指示或建议都会很棒。
回答by tymeJV
I believe ASP Labels get rendered as Spans, you should be able to change it like so:
我相信 ASP 标签会呈现为 Span,您应该能够像这样更改它:
success: function (data) {
$("#<%=stockLabel.ClientID %>").text(data);
}
As far as accessing page controls from a web method, you aren't allowed to. This post goes into it a lot better than I can: Access ASP.NET control from static [WebMethod] (JS ajax call)
至于从 Web 方法访问页面控件,则不允许。这篇文章比我能做的要好得多:Access ASP.NET control from static [WebMethod] (JS ajax call)
回答by Kenneth
You cannot do that when you're calling the webmethod through AJAX.
当您通过 AJAX 调用 webmethod 时,您不能这样做。
If you make a call through AJAX you cannot access your web form controls because you're not actually requesting the page, you're just calling the method.
如果您通过 AJAX 进行调用,则无法访问 Web 表单控件,因为您实际上并未请求该页面,而只是调用了该方法。
Instead, you should find the label with jQuery and update it through Javascript:
相反,您应该使用 jQuery 找到标签并通过 Javascript 更新它:
function GetStockLevel() {
$.ajax({
type: 'POST',
url: 'Stock.aspx/StockLevel',
// data: '{ }',
contentType: 'application/json; charset=utf-8',
dataType: 'text',
success: function (data) {
$("#<%=stockLabel.ClientID %>").text(data); // this only works if the javascript is in your .aspx-file
}
});
}
If the script is not inside your .aspx, you need to find another way of finding the node on the client (by using a specific class for example).
如果脚本不在您的 .aspx 中,您需要找到另一种在客户端上查找节点的方法(例如,通过使用特定的类)。

