2013-07-19 68 views
0

我有兩個asp:BulletedLists,一個填充在Page_Load中,另一個是空的。用戶可以拖動它們之間拖放< LI>的,即拖即正降肉JQuery更改不反映在回發

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 
    { 

    } 
+1

你在'Page_Load'函數中有'if(!Page.IsPostBack)'嗎? – user1

+0

我相信在每個回傳中,所有未註冊的控件都會丟失。你通過JS添加的。 – melancia

+0

是的,絕對不會重置我的代碼中的兩個列表 –

回答

0

速度驚人,沒有奏效。我在SharePoint上,由於asp.config文件位於SharePoint的深層黑暗角落,因此未啓用會話(或其他)。ViewState也沒有以類似的方式工作。也許AJAX的一半工作,但我從來沒有那麼遠。

我開始工作的解決方案是創建一個隱藏的輸入字段,將asp:BulletedList的順序寫入該隱藏字段以通過提交按鈕進行回發。謝謝JasonP的序列化小提琴。

注意:我嘗試了一些其他建議,我在網上發現,使用帶有ViewState的標籤/文本框和/或只讀屬性設置對我無效。標籤工作,以改變頁面內的文字,但沒有堅持回發。

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 
} 
+0

你必須幫我一個代碼示例在那裏交配:) –

+0

@ColinSteel - 看到更新的答案,這是關閉我的頭頂上,所以請原諒我,如果它不是100%完整或準確。 –