2012-06-11 55 views
4

我有一個ListBox的一組值,我想要更新它,如果數據被添加/刪除它。asp.net列表框更新

以下工作,但有沒有辦法不必每次修改內容時調用它?

lbUsers.DataSource = myDataSource; 
lbUsers.DataBind(); 
+0

你怎麼知道數據被修改,什麼是「myDataSource」的類型? –

+0

數據源來自數據庫中的存儲過程,並且它位於!isPostBack中。我不介意使用.DataSource和.DataBind(無論如何,只發生在兩個地方)我只是想知道是否有一種方法可以只調用一次。 –

回答

0

您需要將數據源顯式綁定在每一個回發到更新的數據源。

+0

''綁定列表'爲'asp.net'可用嗎? –

1

是的,你不一定必須使用一個數據源,如果你想明確地添加一個項目:

lbUsers.Items.Add(new ListItem("New Item", "NI")); 
+0

列表框會立即更新嗎? – Bastardo

+0

只要此代碼作爲頁面回發的一部分運行,新項目在加載時就會顯示,是的。你可以添加一個客戶端的項目,但它不會對服務器端代碼「可見」。 – Widor

1

我想獲得它更新,如果數據被添加/刪除它

這一切都取決於你的表單UI。

是頁面添加用戶並在添加時顯示它們的要點?或者是頁面的指令顯示所有當前可用的用戶到第二個?

如果您的目標是前者,那麼您每次添加用戶時都會重新綁定lbUsers列表框。

下面是下面的第一種情況的一個示例:

添加用戶和顯示

標記

<asp:TextBox ID="txtUsername" runat="server" /> 
<asp:Button ID="btAdd" runat="server" value="Add" onclick="btAdd_Click" /> 
<asp:ListBox ID="lbUsers" runat="sever" /> 

代碼隱藏

public void AddUser() 
{ 
    string username = txtUsername.Text; 

    // Either update the database with a new user 
    User newUser = User(username); 
    WhateverDataAccessYoureUsing.Add(User); 
    List<User> users = WhateverDataAccessYoureUsing.GetAllUsers(); 
    lbUsers.DataSource = users; 
    lbUsers.DataBind(); 

    // OTHER OPTION 
    // 
    // Or if no database directly bind the user to the ListBox 
    ListItem li = new ListItem(username); 
    lbUsers.Items.Add(li); 
} 

protected void btAdd_Click(object sender, EventArgs e) 
{ 
    AddUser(); 
} 

但是,如果頁面只是顯示所有用戶並在其他位置創建新頁面時顯示新頁面,則您需要在這裏組合AJAX和服務器端代碼。我們將不使用服務器控件,而使用HTML選擇,因爲服務器控件無法在WebMethods中訪問。另外,我們將使用jQuery來撥打電話AJAX

通過AJAX顯示用戶的呼叫

標記

<select id="lbUsers" size="4" multiple="multiple"> 
</select> 

<script> 
    // When the page is ready to be manipulated 
    $(function() { 

     // Call the server every three seconds to get an updated list of users 
     setInterval(function() { 

      $.ajax({ 
       type: "POST", 
       url: "Default.aspx/GetUsers", 
       contentType: "application/json; charset=utf-8", 
       dataType: "json", 
       success: function (result) { 
        // If we are successful, append the results to lbUser 
        $(result.d).appendTo('#lbUser').hide().fadeIn(500); 
       }, 
       error: function() { 
        // If we encounter an error, alert the user 
        alert("There was a problem getting the new users"); 
       } 
      }); 

     },3000); // 3000 is 3000 milliseconds which is 3 seconds 
    }); 
</script> 

代碼隱藏

// This annotation is required for all methods that can be accessed via AJAX calls 
[System.Web.Services.WebMethod] 
public static void GetUsers() 
{ 
    List<User> users = WhateverDataAccessYoureUsing.GetAllUsers(); 

    string listItems = string.Empty; 

    // Loop through the list of users and create the option 
    // that will be displayed inside of lbUsers 
    foreach (User u in users) 
    { 
     listItems += CreateOption(u.Username); 
    } 

    // return the string value that will be appended on to lbUsers 
    return listItems; 
} 

// This creates the html options that will be displayed 
// inside of lbUser 
private string CreateOption(string text) 
{ 
    string option = "<option>" + text + "</option>" 
}