Ajax提供程序返回太多的HTML Asp.Net

时间:2013-02-27 12:03:25

标签: javascript jquery asp.net ajax asp.net-controls

我正在尝试在Asp.net中对页面进行部分更新。 我通过ajax调用我的.aspx页面来构建我需要附加到页面上特定位置的内容,之前这对我有用。

我目前的问题是我得到了完整的html而不仅仅是元素,我无法弄清楚我之前做过什么。

                var dataObj = { 'includeInactive': includeInactive, 'MasterPersonId': masterId, 'sChosenSSN': chosenSSN };

        $.ajax({
            url: 'clientside/providers/MergeJournalProviders/---ShowSomeDuplicateCivilRegistrationNumbers.aspx',
            dataType: 'html',
            data: dataObj,
            cache: false,
            async: true,
            beforeSend: function (xhr) {
            },
            success: function (result) {

                if (result != "" && result != undefined) {

                    //var htmlToAppend = $(result).find("#DuplicatePeople div").html();
                    //her vælger jeg gruppen(group) der skal opdateres
                    var oldDiv = $('#DupPpl');
                    var newDiv = jQuery(result).find('#DupPpl').html()
                    oldDiv.replaceWith(newDiv);
                    //$('#DupPpl').empty();
                    //$('#DupPpl').html(jQuery(result).find('#DuplicatePeople').html());
                    //$('#DupPpl').remove();
                    //$('#DupPpl').append(result);
                    //$('#DupPpl').append(result);


                }
            }
        });

2 个答案:

答案 0 :(得分:0)

这是使用更新面板的不幸后果。它们的简单性带来了更高的成本。所发生的事情是,不仅会回发面板的内容,而且所有视图状态和HTML标题等都会被回发。使用jQuery,页面方法和Web服务可以实现更高效的部分回发。看看这篇文章:http://msdn.microsoft.com/en-us/library/0b283bse%28v=vs.71%29.aspx

答案 1 :(得分:0)

如果您的结果包含来自您的aspx文件的所有html,并且该html中存在id为“DupPpl”的元素,则可以使用jquery内容获取该元素:

尝试:

var oldDiv = $('#DupPpl');
var newDiv = $(result).contents().find('#DupPpl').html();
oldDiv.replaceWith(newDiv);

使用相同的代码获取其他元素:

$(result).contents().find('YOUR_ELEMENT_ID').html();
相关问题