如何在静态Web方法中获取控件
我有一个 [WebMethod]
,我想使用以下代码将值分配给文本框:
[WebMethod]
public static void MyMethod(string s)
{
//TextBox1.Text = s; //Here how can i access the textbx?
}
I have a [WebMethod]
and i want to assign values to a text box using the this code:
[WebMethod]
public static void MyMethod(string s)
{
//TextBox1.Text = s; //Here how can i access the textbx?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。
[WebMethod]
的全部要点是它们不运行 ASP.Net 页面生命周期。这样,它们就可以快速且可并行化。您的控件不存在。
相反,您应该使用 Javascript(更好)或 UpdatePanel(更差)。
You can't.
The whole point of
[WebMethod]
s is that they don't run the ASP.Net page lifecycle. This way, they're fast and parallelizable.Your controls don't exist.
Instead, you should use Javascript (better) or an UpdatePanel (worse).
在静态方法(即页面方法异步调用)的情况下,整个页面不会被回发..因此服务器上没有有关页面控件(文本框或其他)的信息..
服务器不保留任何页面控件的状态渲染时的控件等,除非这样做(会话或任何其他状态管理)。
因此,如果您想处理页面控件的值,请在异步请求本身中发送此类信息并处理这些值和返回响应..并将其分配给相应的控件客户端脚本..
in case of static method(ie, page method asynchronous call), whole page is not posted back..and hence there is no information on server about the page controls(textbox or whatever)..
server doesn't retain the state of any of the controls or so while rendering unless made to do so(session or any other state management)..
So if you want to work one the values of the page controls, send such information in the asynchronous request itself and work on those values and return the response..and assign it to respective controls back in client side script..