2010-08-02 57 views
0

我的页面上有一个文本框控件和一个复选框列表控件。使用ASP.NET和AJAX填充复选框列表中的文本框

我首先使用代码隐藏中的员工对象列表填充我的复选框列表。员工有一个ID和一个名字。两者都显示在复选框列表如下所示:

  • 吉姆(1)
  • 克斯(2)
  • 加里(3)

当用户检查箱之一,员工的姓名需要填入文本框中。所以如果选择了Jim,那么文本框的值是“Jim”。它还需要支持多个值,所以如果选择Jim和Gary,则文本框的值是“Jim,Gary”。

此外,如果用户在文本框中输入有效值,则应检查正确的员工。这需要支持名称和ID。因此,如果我在文本框中输入“1,Alex”,然后在文本框外单击,则应选择Jim和Alex。

我正在使用ASP.NET,我需要使用AJAX来做到这一点,但我没有使用jQuery和AJAX的经验。有人能告诉我一个简单的例子吗?

回答

2

这是我通过一起,可以帮助你开始。它没有经过测试,也没有完成,但我现在没有时间去做,所以认为这可能会有所帮助。肯定需要进行更多的检查和配合工作,我遗漏了您的实施。

$(function() { 
    // gets all checkbox items by a common class you will put on all of them 
    $('.checkboxlistclass').click(function(e) { 
     var txt = $(this).text(); 
     var employeeName = txt; // change this to parse out the name part and remove the id 

     var currentTxtVal = $('#textboxid').val(); 
     var newVal = currentTxtVal + ',' + employeeName; // check if you need the comma or not 

     $('#textboxid').val(newVal); 
    }); 

    // textboxid is the id of the textbox 
    $('#textboxid').change(function(e) { 
     var textboxVal = $(this).val(); 

     // split on the comma and get an array of items 
     // this is dummy data 
     var items = []; 
     items.push('Jim'); 
     items.push(2); 

     // go through all the items and check if it's a number or not 
     // if it's a number do a selection based on the id in parenthesis 
     // if not then check first part for name match 
     for (var i=0; i < items.length; i++) { 
      if (isNaN(parseInt(items[i])) { 
       $(".checkboxlistclass:contains('" + items[i] + " ('").attr('checked', true); 
      } 
      else { 
       $(".checkboxlistclass:contains(' (" + items[i] + ")'").attr('checked', true); 
      } 
     } 
    }); 
});