asp.net ListBox更新

时间:2012-06-11 12:57:41

标签: c# asp.net listbox

我有一个带有一组值的ListBox,如果有数据被添加/删除,我想更新它。

以下是有效的,但每次修改内容时都有办法不必调用它吗?

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

3 个答案:

答案 0 :(得分:1)

是的,如果要添加明确的项目,则不必使用数据源:

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

答案 1 :(得分:1)

  

如果数据被添加/删除,我想更新它

这一切都取决于您的表单UI。

页面是否添加用户并在添加时显示它们?或者是页面的指令,以显示所有当前可用的用户,直到第二个?

如果您的目标是前者,那么每次添加用户时,您只需重新绑定lbUsers ListBox。

以下是第一种情况的示例:

添加用户和展示

<强>标记

<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>"
}

答案 2 :(得分:0)

您需要在每次回发时将数据源显式绑定到更新的数据源。