从pagemethods调用的静态方法访问aspx控件 [英] Accessing aspx controls from static methods called from pagemethods

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

问题描述

如何使用PageMethods从静态方法中访问控件并设置aspx页面中存在的控件的属性。



例如:

How can i access the controls and set the properties of the controls, that are present in the aspx page, inside the static method which is called from javascript using PageMethods.

for eg:

<html>
<head>
<script type="text/javascrip">
   var timer;
   function chk_me()
   {

       clearTimeout(timer);
       timer = setTimeout(function validate()
       {
           var name = document.getElementById(<%=txtname.ClientID>).value;
           var address = document.getElementById(<%=txtaddress.ClientID>).value;
           PageMethods.ProcessIT(name, address, onSucess, onError);
           function onSucess(result) {
               alert(result);
           }
           function onError(result) {
               alert("Something wrong.");
           }
       }, 2000);
   }
</script>
</head>

<body>
<form id="form1"  runat="server">
<asp:ScriptManager ID='ScriptManager1' runat='server' EnablePageMethods='true' />
<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
<asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
 <asp:TextBox ID="txt_1" runat="server" onkeyup="chk_me()"></asp:TextBox>
</form>
</body>
</html>





代码背后





In Code Behind

[WebMethod]
   public static void ProcessIT(string name, string address)
   {

   Here I want to access the textbox
and set the textbox properties dynamically.How can i achieve that.

   }

推荐答案

你好sunilsourav,



您无法访问Web方法或任何静态方法中的Web表单控件,因为在静态方法中只能访问静态成员,并且您在网页上定义的控件不是静态的。



所以你需要编写javascript函数来为你的控件设置一些不足的值,比如你的web方法返回一些你需要在标签文本上显示的值,然后在你的标签中找到那个标签使用核心javascript(document.getElementByID或Name)的javascript函数,或者你可以使用jquery选择器来选择你的控件并绑定它的属性来设置你的web服务数据。





您好朋友可以请您查看一下..它可能会解决您的问题,您需要浏览一下文章并对代码进行适当的更改以解决您的问题....



将DataSet / DataTable绑定到ASP.NET网格 [ ^ ]
hello sunilsourav,

You can not access the Web form controls in Web Method or any static method, because in static method only static members are accessible, and the control you defined on your web pages is not static.

so you need to write javascript function to set some short of values to your controls, like if your web method returns some short of value which you need to display on your label text then just find that label in your javascript function using either core javascript (document.getElementByID or Name) or you can use jquery selector to select your control and bind its property to set your web service data.


hello friend can you please check this out.. it might solve your problem, you need to take look around the article and make appropriate changes to code to solve your problem....

Bind DataSet/DataTable to ASP.NET grid[^]


这篇关于从pagemethods调用的静态方法访问aspx控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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