Click here to Skip to main content
15,915,324 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
<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

C#
[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.

   }
Posted
Updated 10-Sep-13 2:39am
v3

1 solution

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[^]
 
Share this answer
 
v2
Comments
sunilsourav 12-Sep-13 4:23am    
Hi Thank u for ur Reply.My requirement is binding the datatable to gridview in ProcessIT method that was defined above.Can u Please set a demo regarding this.
Tejas Vaishnav 13-Sep-13 2:10am    
please check out the updated solution it might helpful to you...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900