jquery方法仅在第一次调用webmethod,并绕过其他任何时间

jquery method only calls webmethod the 1st time and bypasses any other time it's called
2020-11-24
  •  译文(汉语)
  •  原文(英语)

我有一个web方法,它返回一个json字符串并填充一个jstree,并且在文本框的第一次单击中都可以正常工作.每次用户单击不同的文本框时,它都会在jquery中传递不同的值,但是第一次调用webmethod实际上不会被调用.所以我想知道我需要清除什么以便每次都调用webmethod.

function showtree1(thisID, line) {
    $('#jstree_demo_div').jstree.empty;
    $('#jstree_demo_div').jstree({
        "core": {

            "data": //data - jquery
              {
                  type: "POST"
                  , dataType: "json"
                  , contentType: "application/json; charset=utf-8"

                  , url: "FlowCycleReport.aspx/populateTree"

                  //, data: "{'imgID':'" + thisID + "'}"
                  , data: JSON.stringify({ imgID: thisID, line: line })
                  //, "data": function (node) {
                  //    //return "{'id':'" + $(node).attr("id") + "'}";
                  //    return node.d;

                  //}
                  , success: function (node) {
                      var ReturnString = node.d;
                     // alert(ReturnString);
                      return { "id": node.id };
                  }///success


                  , error: function (msg) {

                      alert("Error: " + msg.responseText);

                  }///error
              }//data
        }///core
,
        "plugins": ["themes", "json_data", "search", "contextmenu", "types"]

    });
}

WebMethod:

[WebMethod]
    //public static string populateTree(string imgID, int node)
    public static string populateTree(string imgID, string line)
    {
        DAL_Flow DL = new DAL_Flow();
        //string line = "278";

        string nodevalues = DL.JsonNav(imgID, line);

        return nodevalues;

    }

onclick事件:

$(function () {
        $(".textbox").click(function (e) {

            var thisID = $(e.target).attr("ID");

            var csID = thisID.substr(thisID.lastIndexOf("_") + 1);
            $('#posDetailDiv').hide();


            var line = $('#dlLine').val();

            // Reset the header:
            $('#PartHeaderDiv').empty();
            $('#PartHeaderDiv').append('Line Number: ' + line + ': Control Station ' + csID);

            showtree1(csID, line);

            $('#posDetailDiv').show();


        });
    });

脚本管理器:

   <asp:toolkitscriptmanager runat="server" EnablePageMethods="true" ></asp:toolkitscriptmanager>

每次单击时,标题上的CSID和line值都会更改,但treeview值不会更改.

我在网络方法上设置了一个断点,以验证网络方法没有受到攻击.

我确定我只是想念一些东西.

有人可以帮忙吗?

速聊1:
为什么每次单击用户输入时都会完全清除树?该插件具有用于管理数据的强大API.
速聊2:
每次b / c我传递不同的参数来填充树时,都需要刷新树.延迟加载不适用于我们正在使用的浏览器,这就是为什么我每次都加载它.我实际上已经使用$('#jstree_demo_div').jstree("refresh");进行了刷新.所以真棒.但是现在,当它第二次触碰到我的web方法时,尽管来自jquery的参数是正确的,但是web方法仍然具有传递的第一个参数.
速聊3:
当插件可以为您完成所有操作(参数或没有参数)时,没有任何意义
速聊4:
你有例子吗?
解决过程1

我能想到的第一件事是,由于您擦除了所有内容并在每次单击它时对其进行了重建,因此您的.click事件也被擦除了,因为它不是动态绑定的.因此,与其做:

$(".textbox").click(function (e) {

你可以做:

$(".textbox").on('click', function (e) {
    //click event code goes here
});

因此,即使对于.textbox以后添加的动态元素,您的click事件也会一直被注册.

I have a webmethod that returns a json string and populates a jstree and all works perfectly the 1st click of a text box. Each time a user clicks a different text box, it passes different values in the jquery but the webmethod actually doesn't get called after the 1st time it's called. So I'm wondering what I need to clear in order for it do call the webmethod each time.

function showtree1(thisID, line) {
    $('#jstree_demo_div').jstree.empty;
    $('#jstree_demo_div').jstree({
        "core": {

            "data": //data - jquery
              {
                  type: "POST"
                  , dataType: "json"
                  , contentType: "application/json; charset=utf-8"

                  , url: "FlowCycleReport.aspx/populateTree"

                  //, data: "{'imgID':'" + thisID + "'}"
                  , data: JSON.stringify({ imgID: thisID, line: line })
                  //, "data": function (node) {
                  //    //return "{'id':'" + $(node).attr("id") + "'}";
                  //    return node.d;

                  //}
                  , success: function (node) {
                      var ReturnString = node.d;
                     // alert(ReturnString);
                      return { "id": node.id };
                  }///success


                  , error: function (msg) {

                      alert("Error: " + msg.responseText);

                  }///error
              }//data
        }///core
,
        "plugins": ["themes", "json_data", "search", "contextmenu", "types"]

    });
}

WebMethod:

[WebMethod]
    //public static string populateTree(string imgID, int node)
    public static string populateTree(string imgID, string line)
    {
        DAL_Flow DL = new DAL_Flow();
        //string line = "278";

        string nodevalues = DL.JsonNav(imgID, line);

        return nodevalues;

    }

onclick event:

$(function () {
        $(".textbox").click(function (e) {

            var thisID = $(e.target).attr("ID");

            var csID = thisID.substr(thisID.lastIndexOf("_") + 1);
            $('#posDetailDiv').hide();


            var line = $('#dlLine').val();

            // Reset the header:
            $('#PartHeaderDiv').empty();
            $('#PartHeaderDiv').append('Line Number: ' + line + ': Control Station ' + csID);

            showtree1(csID, line);

            $('#posDetailDiv').show();


        });
    });

script manager:

   <asp:toolkitscriptmanager runat="server" EnablePageMethods="true" ></asp:toolkitscriptmanager>

The values for CSID and line changes on my header each click but the treeview values don't.

I've put a breakpoint on the webmethod to verify that the webmethod isn't getting hit.

I'm sure I'm just missing something.

Can anyone help?

Talk1:
why do you completely wipe out the tree each time a user input is clicked? the plugin has a robust API for managing data.
Talk2:
I need to refresh the tree each time b/c I am passing different parameters to populate the tree. Lazy loading doesn't work with the browswer we are using which is why I load it each time. I've actually got the refresh piece working with $('#jstree_demo_div').jstree("refresh"); so that's aweome. But now, when it's hitting my webmethod the 2nd time, although the params from the jquery are correct, the webmethod still has the 1st params that were passed.
Talk3:
non of that makes any sense when the plugin can do it all for you, params or no params
Talk4:
Do yo u have an example?
Solutions1

First thing i can think of is that since you wipe out everything and rebuild it every time it is clicked, your .click event gets wiped out too since it is not dynamically bonded. So instead of doing:

$(".textbox").click(function (e) {

you can do:

$(".textbox").on('click', function (e) {
    //click event code goes here
});

so your click event would be registered all the time even for the dynamic .textbox elements that are added later on.

转载于:https://stackoverflow.com/questions/28157906/jquery-method-only-calls-webmethod-the-1st-time-and-bypasses-any-other-time-its

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入