JQuery更改未反映在回发中

时间:2013-07-19 15:35:11

标签: c# jquery html asp.net

我有两个asp:BulletedLists,一个填充在Page_Load上,另一个是空的。用户可以拖放< li>在他们之间,那拖拉的肉是

    function Move(element, source, target) {
        var newLI = document.createElement("li");
        var sourceBL = document.getElementById(source);
        var targetBL = document.getElementById(target);

        newLI.innerHTML = element.innerHTML;
        sourceBL.removeChild(element);
        targetBL.appendChild(newLI);
    }

我创建了一个新元素,使它在asp:BulletedList中对齐,而不是将自己放在释放鼠标的位置。

问题是我需要知道回发的内容是什么,第二个asp:BulletedList总是空的,第一个asp:BulletedList用原始值填充自己,即使我没有清除或重新填充它们。

    foreach (ListItem li in blSelectedDocuments.Items) // .Items is empty
    {

    }

2 个答案:

答案 0 :(得分:0)

过去在ASP.NET WebForms页面上使用jQuery插件,我使用AJAX将更新的数据发送回ASP.NET AJAX页面方法,然后将更改存储到Session缓存中。然后在回发时,Page_Load将查看Session以查看列表中的值的顺序(我有一个拖放列表,用于显示报告的顺序)。

模拟代码示例:

JavaScript的:

function Move(element, source, target) {
    var newLI = document.createElement("li");
    var sourceBL = document.getElementById(source);
    var targetBL = document.getElementById(target);

    newLI.innerHTML = element.innerHTML;
    sourceBL.removeChild(element);
    targetBL.appendChild(newLI);

    // TODO: Serialize source and target lists to JSON to pass to the server
    var serializedData = {};

    // Use jQuery.ajax() to call ASP.NET AJAX Page Method
    $.ajax({
        type: "POST",
        url: "PageName.aspx/UpdateListsInSessionCache",
        data: serializedData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            // Do something here when the AJAX calls completes
        }
    });
}

ASP.NET代码隐藏(C#)

using System.Web.Services;

[WebMethod]
public static void UpdateListsInSessionCache(List<ListItem> source, List<ListItem> target)
{
    Session["SourceList"] = source;
    Session["TargetList"] = target;
}

protected void Page_Load(object sender, EventArgs e)
{
    // Create new lists so we have something empty and not null to work with
    var source = new List<ListItem>();
    var target = new List<ListItem>();

    // Always check for values in Session cache and update if there are values
    if (Session["SourceList"] != null)
    {
        source = Session["SourceList"] as List<ListItem>;
    }

    if (Session["TargetList"] != null)
    {
        target = Session["TargetList"] as List<ListItem>;
    }

    // Do something with source and target lists
}

答案 1 :(得分:0)

可悲的是,这一切都没有奏效。我正在使用SharePoint并且会话未启用(或其他任何),因为asp.config文件位于SharePoint的深层黑暗角落.ViewState也没有以类似的方式工作。也许AJAX的一半会起作用,但我从来没有这么做过。

我开始工作的解决方案是创建一个隐藏的输入字段,将asp:BulletedList的顺序写入该隐藏字段,以通过Submit按钮进行回发。感谢JasonP的序列化小提琴。

注意:我尝试了一些我在网上找到的其他建议,使用带有ViewState的Label / TextBox和/或Readonly属性设置对我不起作用。标签用于更改页面中的文本,但不会在回发时保留。