4

値のセットを含むリストボックスがあり、データが追加/削除された場合に更新したいと思います。

以下は動作しますが、コンテンツを変更するたびにそれを呼び出す必要がない方法はありますか?

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

3 に答える 3

1

はい。アイテムを明示的に追加する場合は、必ずしもデータソースを使用する必要はありません。

lbUsers.Items.Add(new ListItem("New Item", "NI"));
于 2012-06-11T13:04:16.077 に答える
1

データが追加/削除された場合に更新したいのですが

これはすべて、フォームのUIによって異なります。

ユーザーを追加し、追加されたときに表示するページのポイントはありますか?または、現在利用可能なすべてのユーザーを2番目まで表示するページのディレクティブですか?

目的が前者の場合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ここでサーバー側のコードとの組み合わせが必要です。でサーバーコントロールにアクセスできないため、サーバーコントロールを使用する代わりに、HTMLselectを使用します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>"
}
于 2012-06-11T14:45:28.177 に答える
0

すべてのポストバックでデータソースを更新されたデータソースに明示的にバインドする必要があります。

于 2012-06-11T13:05:34.430 に答える