简体   繁体   English

通过在jQuery中单击按钮加载Pagemethods

[英]Load the Pagemethods by button click in jQuery

<script type="text/javascript">
    $(document).ready(function() {
        $('#loadbtn').click(function() {    // can 't load  
            opts = {
                title: 'ABCD',
                series: [{
                    neighborThreshold: 0
                }],
                axes: {
                    xaxis: {
                        //smethg
                    },
                    yaxis: {

                        //smethg
                    }
                },

            };
            PageMethods.LoadAsset(LoadSucc, LoadFail); //Want to load by button click event
            function LoadSucc(obj) {
                 goog = obj;
                //Something
                alert("Data loaded");
            }

            function LoadFail() {
                alert("Data missing");
            }
        });
    });
</script>

In my C# code 在我的C#代码中

[System.Web.Services.WebMethod]
public static Array LoadAssetAssignView()
{
    //something
    return something;
}

ASP: ASP:

<asp:Button runat="server" ID="loadbtn" Text="Load"   /> 

Actually the function starts working in the document ready function. 实际上,该功能开始在文档准备功能中工作。 I want to load the function when my button is clicked. 我想在单击按钮时加载该功能。 How do I do this? 我该怎么做呢?

I think you missing to declare the method in html. 我认为您缺少在html中声明该方法的方法。 Example: 例:

<div>
Your Name :
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
<input id="btnGetTime" type="button" value="Show Current Time"
    onclick = "ShowCurrentTime()" />
</div>

then call the jQuery: 然后调用jQuery:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function ShowCurrentTime() {
    $.ajax({
        type: "POST",
        url: "CS.aspx/GetCurrentTime",
        data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}
</script>

And last the pagemethod. 最后是pagemethod。 You can reference by this link: http://www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx 您可以通过以下链接进行引用: http : //www.aspsnippets.com/Articles/Call-ASPNet-Page-Method-using-jQuery-AJAX-Example.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM