2013-09-25 34 views
0

我有一個應用程序與兩個部分。使用Javascript/JQuery從代碼更新下拉列表

頂部有一個dropdownlist,它使用page load上調用的代碼隱藏方法顯示database中的所有成員。

底部有一個網格,您可以add/delete/edit成員使用JQuery。 本節中的更改顯然不會重建頂部部分中的下拉列表。

我想運行JQuery成功方法updates/rebuildsdropdownlist中的一些代碼來反映成員的最新變化。我希望如果可能的話,使用相同的代碼在page_loadpopulate的下拉列表中選中db。

這可能嗎?如果是這樣,我將如何去完成這個?

任何幫助將不勝感激。

[請求代碼]
.aspx文件

<asp:DropDownList ID="director" runat="server"></asp:DropDownList> 
jQuery success() fired on add/delete/update member grid. 

.aspx.cs(代碼隱藏)

private void LoadDirectorOptions(int deptId) 
{ 
      var memberRepo = new MemberRepository(); 
      List<Member> members = memberRepo.GetMembers(deptId); 

      director.DataSource = members; 
      director.DataTextField = "FullName"; 
      director.DataValueField = "Id"; 
      director.DataBind(); 
      director.Items.Insert(0, new ListItem("<Please Select>", "0")); 

} 
+2

如何提供您的相關代碼? –

+0

你會想要使用AJAX,如果你不想重新加載頁面 – Jonesopolis

+0

@Jonesy我知道我想使用AJAX。我只是不知道該怎麼去做。 – Baxter

回答

2

,因爲它是一個服務器端代碼和JQuery代碼發生在客戶端,除非您刷新頁面執行JQuery代碼後,您不能使用方法背後相同的代碼。您可以使用與提供的網絡服務相同的所有成員來更新網格。

下載列表。例如:

$.ajax({ 
     // Updating the grid here and retrun all the members : it will be saved in the response 
     type: "Get", 
     url: url, 
     data: data, 
     dataType: dataType, 
     success: success(data) 
    }); 

    function success(data) 
    { 
     //here you can get the resposne from the server 
     // Iterate through data and add option elements 
     $(".members").append("<option value=? text=?/>"); 

    } 

最大的可能是你的數據必須是JSON已經在你的服務器成員數據庫表中的所有成員。那麼您可以將CSS類(成員)添加到您的下拉列表中,並使用JQuery選擇器來選擇下拉列表。

希望這是有幫助的。

+0

這正是我正在尋找的。謝謝。 – Baxter

1

如果要刷新下拉而無需刷新整個頁面,你需要將它們包裹在UpdatePanel之內。當底部部分中的網格發生更改時,請更新UpdatePanel,以便重新綁定下拉列表。要強制在UpdatePanel在Javascript來更新您可以使用GetPostBackEventReference方法來強制回發是這樣的:

<%= Page.ClientScript.GetPostBackEventReference(updatePanelID, "")%> 
+0

我可以使用Javascript/JQuery更新更新面板嗎? 如果是這樣,我該怎麼做呢? – Baxter

+0

@nmat希望我可以降級你甚至建議更新panel.Please轉到我建議的鏈接,並學習真正的Ajax。 – idlehands23

+0

@Baxter是的,你可以強制在javascript中回傳,然後重新綁定頁面加載中的下拉菜單。查看更新後的答案。如果你想要更多的細節,你需要提供一些你的應用程序的相關代碼 – nmat

1

應該使用AJAX在您的成功調用調用方法。 http://api.jquery.com/jQuery.ajax/

+0

我在想,控制可能是解決方案。我只是不確定如何使用它來使用代碼隱藏方法重建頁面控件。你能詳細解釋一下嗎? – Baxter