How to get control values and modify them in Page

2019-09-24 11:19发布

问题:

I have couple of controls in the page. I need to modify these value in Page method. How can I do that?. When I modify those values in page method should reflect in page?

Please give me expample.

回答1:

Quick example:

<asp:ScriptManager runat="server" EnablePageMethods="true" />
<!-- or ToolkitScriptManager, but remember to put only one -->

<script type="text/javascript">
    function invokeMethod() {
        x = document.getElementById('<%= TextBox1.ClientID %>').value;
        PageMethods.theMethod(x, OnSuccess, OnFailure);
    }
    function OnSuccess(r) {
        document.getElementById('<%= TextBox1.ClientID %>').value = r;
    }
    function OnFailure(r) {
        alert(r._message);
    }
</script>

    [System.Web.Services.WebMethod()]
    public static string theMethod(string x)
    {
        return x + "!!!";
    }