views:

125

answers:

1

I'm am looking at the code in this article on Web methods and I've tried to implement it. The article works as designed but when I swap the HTML button for an asp:button I get different results.

CODE behind:

    [WebMethod]
    public static string GetCurrentDate()
    {          
        return DateTime.Now.ToString();
    }

Aspx page:

js:

<script type="text/javascript">
    function UpdateTime() {
        $get('Label6').innerHTML = PageMethods.GetCurrentDate(OnSucceeded,
                                   OnFailed); 
 }

 function OnSucceeded(result, userContext, methodName) {
   $get('Label6').innerHTML = result; 
 }

 function OnFailed(error, userContext, methodName) {
   $get('Label6').innerHTML = "An error occured.";
 }
</script>

and other markup

<form id="form1" runat="server">
    <asp:ScriptManager EnablePageMethods="true" 
        ID="ScriptManager2" runat="server">
    </asp:ScriptManager>

<div id="usingWebMethods">
        <asp:Label runat="server" 
            Text="Using WebMethods" 
            ID="Label5" />
        <br />

        <asp:Label 
            runat="server" 
            Text="Web Method Update!" 
            ID="Label6" />
        <asp:Button ID="Button3" 
            runat="server" 
            Text="Postback Update" 
            OnClientClick="UpdateTime();" 
            UseSubmitBehavior="False" />      
        <input type="button" 
            id="Button4" 
            value="Web Method Update" 
            onclick="UpdateTime();" />


</div>
</form>

I've noticed 2 things with the button. The button returns an error occurred and even though the label changes the viewstate is not updated.

Why do these 2 issues occur and what can I do to resolve them?

+1  A: 

ViewState only updates on postback. You are using true AJAX. It has no way of knowing what you did.

rick schott