Search code examples
asp.netajaxwebmethod

How to update multiple labels in an WebMethod (Asp:net / Ajax)


I have around 20 labels to update.

In relation to performance and padeload-weight, I choose WebMethods to realize it (Much smaller then updatepanel + timer).

So, how to update a lot of values in the WebMethod?

(My First intention was to access them normaly as I do: lbl1.Text = "1"; lbl2.Text = "2".... but the method is static - no chance).


Solution

  • Create your method something like (choose some more convenient names tho :-)).

    public static object MyMethod()
    {
        return new {
            Value1 = "Label1Value",
            Value2 = "Label2Value",
            ...
        };
    }
    

    And do it in JavaScript like

    <script type="text/javascript">
         var myObj = PageMethods.MyMethod();
    
         document.getElementById('<%=Label1.ID%>').innerHTML = myObj.Value1;
         document.getElementById('<%=Label2.ID%>').innerHTML = myObj.Value2;
         ...
    </script>
    

    Ah well, that'd get you started.