用javascript实现邮件处理中的全部选择与读取选择

类别:.NET开发 点击:0 评论:0 推荐:

    以下代码来实现我们经常遇到的功能:在网页中处理Email时,会有全部选择或取消的checkbox,并把选中的内容加入一个textbox。

      有两种方法可以实现:我们假设邮件列表是用DataList实现的,在ItemTemplate中加入CheckBox1来和书据库绑定。

      用服务器控件:在CheckBox的CheckedChanged事件中实现。

      private void CBSelectAll_CheckedChanged(object sender, System.EventArgs e)

              {

                     if (CBSelectAll.Checked)

                     {

                            CheckBox CurrentCheckBox=new CheckBox();

                            foreach(DataListItem itemChecked in DataList1.Items)

                            {

                                   CurrentCheckBox=(CheckBox)(itemChecked.FindControl("CheckBox1"));

                                   CurrentCheckBox.Checked=true;

                            }

                     }

                     else

                     {

                            CheckBox CurrentCheckBox=new CheckBox();

                            foreach(DataListItem itemChecked in DataList1.Items)

                            {

                                   CurrentCheckBox=(CheckBox)(itemChecked.FindControl("CheckBox1"));

                                   CurrentCheckBox.Checked=false;

                            }

                     }

 

              }

这种方法缺点是反应太慢,不适应客户的需要。

      另一种方法是用javascript:

在HTML中加入

 

<script language="javascript">

                            function selectAll()

                            {

                                  

                                   var arrObj=document.all;

                                   if(document.Form1.checkedAll.checked)

                                   {

                                          for(var i=0;i<arrObj.length;i++)

                                          {

                                                 if(typeof arrObj[i].type != "undefined" && arrObj[i].type=='checkbox')

                                                 {

                                                        arrObj[i].checked =true;

                                                 }

                                          }

                                   }

                                   else

                                   {

                                          for(var i=0;i<arrObj.length;i++)

                                          {

                                             if(typeof arrObj[i].type != "undefined" && arrObj[i].type=='checkbox') arrObj[i].checked =false;

                                          }

                                   }

                            }

 

              </script>

在对应的全部选择checkbox中加入onclick=”selectAll()”,如下:

<INPUT id="checkedAll" onclick="selectAll()" type="checkbox" name="checkedAll">

 

对于选中的列表,我们如何将其选中的内容加入到一个textbox呢?

这里的CheckBoxList的名称为CBLName,TextBox为TextBox1。实现的功能是将选中的CheckBoxList的fieldText加入TextBox,并用分号隔开。

<script language="javascript">

function inputName()

                      {                         

                            var CheckBoxList=document.getElementById("CBLName");

                            var textbox=document.getElementById("TextBox1");

                            textbox.value="";

                            for(i=0;i<CheckBoxList.rows.length;i++)

                            {

                                  if(CheckBoxList.rows[i].cells[0].childNodes(0).checked)

                                  textbox.value+=CheckBoxList.rows[i].cells[0].childNodes(1).innerText+";";

                            }

                                 

                      }

      </script>

本文地址:http://com.8s8s.com/it/it41865.htm