来自 澳门金莎娱乐手机版 2019-09-19 11:52 的文章
当前位置: 金沙澳门官网网址 > 澳门金莎娱乐手机版 > 正文

listBbox控件用法,net与Javascript调整的日期控件

控件效果如下:

ListBox基本功用使用方法  

2011-06-09 13:23:16|  分类: .NET/C# |  标签:listbox基本作用使用形式   |举报 |字号大中小 订阅

 

 

  1. ListBox基本功效使用方法

 

ListBox基本作用首先是列表项的增加,客商端实现代码增添在listbox实例化代码中间,比如:

<asp:ListItem Value="value" Selected=True>Text</asp:ListItem>

若在服务器端达成,为制止每回加载时实践增加列表项,上述代码满含在底下代码中:

if(!IsPostBack)

{

}

WebForm页面上须增加2个listbox(listbox1和lixtbox2)和2个指令按键,listbox1不为空。列表项从listbox1增添到listbox2须在Button1单击事件中调用Add方法:

ListBox2.Items.Add(ListBox1.SelectedValue);

若要从listbox第22中学删去列表项的话须在Button2单击事件中调用Remove方法:

ListBox2.Items.Remove(ListBox2.SelectedValue);

列表项从listbox1增加到listbox2后,列表项从listbox第11中学删去:

int i=0;

while(i<ListBox1.Items.Count)

{

if(ListBox1.Items[i].Selected==true)

{

ListBox2.Items.Add(ListBox1.Items[i]);

ListBox1.Items.Remove(ListBox1.Items[i]);

}

else

i+=1;

}

那般只好促成单项增多,想要完结多项增多,首先设置ListBox1的SelectionMode属性值Multiple,ListBox1允过多项选中。

在Button1单击事件中增加

foreach(ListItem MyItem in ListBox1.Items)

if(MyItem.Selected==true)

ListBox2.Items.Add(MyItem);

想要一回清空ListBox第22中学享有选项可在Button2单击事件中调用clear方法,

ListBox2.Items.Clear();

若列表项已经拉长,不容许三回增多,Button1单击事件中的代码包涵在:

if(ListBox2.Items.FindByValue(ListBox1.SelectedValue)==null)

{

}

ListBox与数据库绑定便是点名他的DataSource和DataTextField属性,

ListBox2.DataSource=数据源;

ListBox2.DataTextField="字段名";

ListBox2.DataBind();

 


 

<script type="text/javascript">
        function SelectAll()
        {
            var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
            var length = lst1.options.length;
             var string = window.document.getElementById("<%=hf_NewName.ClientID %>")
            for(var i=0;i<length;i++)
            {
                var v = lst1.options[i].value;
                var t = lst1.options[i].text;             
                var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
                lst2.options[i] = new Option(t,v,true,true);
                string.value+=v;
            }
        }
        
        function DelAll()
        {
            var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
            var length = lst2.options.length;
            for(var i=length;i>0;i--)
            {
                lst2.options[i-1].parentNode.removeChild(lst2.options[i-1]);
            }
        }
        
        function SelectOne()
        {
          var string = window.document.getElementById("<%=hf_NewName.ClientID %>")
            var lst1=window.document.getElementById("<%=lb_Sourse.ClientID %>");
            var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
            var lstindex=lst1.selectedIndex;
            var length = lst2.options.length;
            var isExists = false;
            if(lstindex<0)
                return;
            else if(length != null)
            {
                for(var i=0;i < length; i++)
                {
                     if(lst2.options[i].text == lst1[lstindex].text&&lst2.options[i].value == lst1[lstindex].value)
                     {
                        isExists = true;
                     }
                }
            }
            else
            {
                return;
            }
            if (isExists == false)
            {
                var v = lst1.options[lstindex].value;
                var t = lst1.options[lstindex].text;
                lst2.options[lst2.options.length] = new Option(t,v,true,true);
                string.value+=v;
            }
            else
            {
                alert("所选条款已经存在");
                return false;
            }
        }
        
        function DelOne()
        {
            var lst2=window.document.getElementById("<%=lb_NewName.ClientID %>");
            var lstindex=lst2.selectedIndex;
            if(lstindex>=0)
            {
                var v = lst2.options[lstindex].value+";";
                lst2.options[lstindex].parentNode.removeChild(lst2.options[lstindex]);
            }
        }
</script>

图片 1内需表明的是出于JS脚本是在顾客端推行的,因而服务器端控件是不能调用JS的,由于ID不可能被找到,但用<%=lb_NewName.ClientID %>的格局就高明的消除得该难题,是asp控件具备顾客端id,那样就可以调用了。

希望对大家具备帮助!

 


 

ASP.NET中增添控件ListBox , 属性设为 Multiple , 则可进展多选.
就以多个listbox之间多选增添项目为例.
四个控件为listboxleft , listboxright 定义了叁个动态数组用于中间存储arrRight .具体代码如下:

//读取侧边选中项目
   ArrayList arrRight = new ArrayList();
   foreach(ListItem item in this.ListBoxRight.Items) //按类型listitem读取listbox中选定项
   {
    if(item.Selected) //判定是还是不是选中
    {
     arrRight.Add(item);
    }
   }  

   //侧面移除选定项目 左边增多
   foreach(ListItem item in arrRight)
   {
    this.ListBoxLeft.Items.Add(item);
    this.ListBoxRight.Items.Remove(item);
   }
不能够将item的增进删除间接写在if(item.Selected){}内,因为品种remove后会出现谬误


 

增加四个listbox (ListBoxAll , ListBoxUser)    八个按键( ButtonListDel >> , ButtonListAdd <<)

开关的代码为:

private void ButtonListDel_Click(object sender, System.EventArgs e)
  {
   //listbox >> 删除listboxuser选中项目 将其添加入listboxall
   if(this.ListBoxUser.SelectedIndex != -1)
   {
    this.ListBoxAll.Items.Add(this.ListBoxUser.SelectedItem.Value);
    this.ListBoxUser.Items.Remove(this.ListBoxUser.SelectedItem.Value); 
   }
  }

  private void ButtonListAdd_Click(object sender, System.EventArgs e)
  {
   //listbox << 
   if(this.ListBoxAll.SelectedIndex != -1)
   {
    this.ListBoxUser.Items.Add(this.ListBoxAll.SelectedItem.Value);
    this.ListBoxAll.Items.Remove(this.ListBoxAll.SelectedItem.Value);
   }
  }

1 为了确定保障增加不会重新 填充listbox时使两侧无重复项目.

姣好listbox里项指标丰硕、删除的基本点代码:

1.通过AddRange方法增添项目:this.lbyx.Items.AddRange(new object[] {"北京","上海","天津","成都","广州","深圳","武汉"});

2.添加items:this.lbbx.Items.Add(this.lbyx.Text);

3.清空驶列车表内的兼具items:this.lbbx.Items.Clear();

4.脚下所选用的数码获取:this.lbbx.SelectedIndex

5.剔除某项:this.lbbx.Items.RemoveAt(this.lbbx.SelectedIndex);

 


 

在从叁个ListBox选取内容copy到其他一个ListBox时候用下边包车型地铁主意:

if (ListBox2.Items.IndexOf(ListBox1.SelectedItem) == -1)
        {
            ListBox2.Items.Add(new ListItem(ListBox1.SelectedValue));
            //ListBox2.Items.Add(List博克斯1.SelectedItem);  <--用那一个会记录状态,ListBox2不协理Multiple就出错了
}

===================================================================

<asp:listbox width="100px" runat=server>
                    <asp:listitem>Roman</asp:listitem>
                    <asp:listitem>Arial Black</asp:listitem>
                    <asp:listitem>Garamond</asp:listitem>
                    <asp:listitem>Somona</asp:listitem>
                    <asp:listitem>Symbol</asp:listitem>
                 </asp:listbox>

 void RemoveAllBtn_Click(Object Src, EventArgs E) {

            while (InstalledFonts.Items.Count != 0) {

               AvailableFonts.Items.Add(new ListItem(InstalledFonts.Items[0].Value));
               InstalledFonts.Items.Remove(InstalledFonts.Items[0].Value);
            }
        }
当数码源改为

<asp:listbox width="100px" runat=server>
                    <asp:listitem value="1">Roman</asp:listitem>
                    <asp:listitem value="bbb">Arial Black</asp:listitem>
                    <asp:listitem value="333">Garamond</asp:listitem>
                    <asp:listitem value="4">Somona</asp:listitem>
                    <asp:listitem value="5">Symbol</asp:listitem>
                 </asp:listbox>

listbox.items.count会平昔为总的数量,不会顺while循环的调换,能够修改为假如艺术:

  #region button

  private void btnRemoveAll_Click(object sender, System.EventArgs e)
  {
   while (lstSelDpt.Items.Count != 0) 
   {
    lstAllDpt.Items.Add(lstSelDpt.Items[lstSelDpt.Items.Count-1]);
    lstSelDpt.Items.RemoveAt(lstSelDpt.Items.Count-1);
   }

  }

  private void btnRemove_Click(object sender, System.EventArgs e)
  {
   while(lstSelDpt.SelectedIndex != -1) 
   {
    lstAllDpt.Items.Add(lstSelDpt.Items[lstSelDpt.SelectedIndex]);
    lstSelDpt.Items.Remove(lstSelDpt.Items[lstSelDpt.SelectedIndex]);
   }
  }

  private void btnAdd_Click(object sender, System.EventArgs e)
  {   
   while (lstAllDpt.SelectedIndex != -1) 
   {
    lstSelDpt.Items.Add(lstAllDpt.Items[lstAllDpt.SelectedIndex]);
    lstAllDpt.Items.Remove(lstAllDpt.Items[lstAllDpt.SelectedIndex]);
   }
  }

  private void btnAddAll_Click(object sender, System.EventArgs e)
  {
   while (lstAllDpt.Items.Count != 0) 
   {
    lstSelDpt.Items.Add(lstAllDpt.Items[lstAllDpt.Items.Count-1]);
    lstAllDpt.Items.Remove(lstAllDpt.Items[lstAllDpt.Items.Count-1]);
   }

  }

  #endregion

 


 

用dotnet做二个项指标进程中,蒙受了一个List博克斯的难点:通过在多个ListBox中双击,把选中的项增添到另一个ListBox中,但List博克斯控件本人并未该事件,那么怎样促成呢?小编就想到了顾客端脚本javascrit,通过翻看有关资料,终于把这么些主题素材消除了,以往写出来与我们大饱眼福,希望能对我们享有帮衬。
        这里有多个难题:
        第一:双击所要实践的javascript代码是何等?
                    注意:javascript代码的语法要科学,即每一行都要以“;”结尾;
                    function change()
                        {
                             var addOption=document.createElement("option");
                             var index1;
                             if(document.Form1.ListBox1.length==0)return(false);
                              index1=document.Form1.ListBox1.selectedIndex; 
                             if(index1<0)return(false);
                              addOption.text=document.Form1.ListBox1.options(index1).text;
                              addOption.value=document.Form1.ListBox1.value;
                             document.Form1.ListBox2.add(addOption);
                             document.Form1.ListBox1.remove (index1);
                         }
        第二:怎么样将 javascript 代码转变为C#代码?
                    public static void ListBox_DblClick(Page page,System.Web.UI.WebControls.WebControl webcontrol,string                                 SourceControlName,string TargetControlName)
                     {
                           SourceControlName = "document.Form1." +  SourceControlName;
                           TargetControlName = "document.Form1." +  TargetControlName;
                           string js = "<script language=javascript> function change(SourceControlName,TargetControlName)";
                           js += "{";
                           js +=     "var addOption=document.createElement('option'); n";
                           js += "  var index1; n";
                           js += "if(SourceControlName.length==0)return(false);n";
                           js += "  index1=SourceControlName.selectedIndex; n ";
                           js += "  if(index1<0)return(false);n";
                           js += " addOption.text=SourceControlName.options(index1).text; n";
                           js += "addOption.value=SourceControlName.value; n";
                           js += "TargetControlName.add(addOption); n";
                           js += "SourceControlName.remove (index1) n";

                           js +="}";
                           js += "</script>";
                            //注册该 javascript ;
                           page.RegisterStartupScript("",js);
                            //为控件增加双击事件;
                           webcontrol.Attributes.Add("onDblClick","change("

  • SourceControlName + "," + TargetControlName +                                 ");");
                          }
                        在该措施中,SourceControlName是要绑定双击事件的控件,TargetControlName是收到双击事件选定项的控件。    
                        这里有一个主题素材,怎样让对象作为参数字传送给javascript的change函数,笔者那边运用的是用  SourceControlName  ,TargetControlName 来传递两个ListBox的Name, 然后与“document.Form1.“组合成多少个串来传递给javascript的change函数,即 
                                SourceControlName = "document.Form1." +  SourceControlName;
                               TargetControlName = "document.Form1." +  TargetControlName;

        第三:怎么着为控件增加双击事件?
                    用ControlName.Attributes.Add(“属性名称”,“函数名称或代码”);

图片 2

从左到右:month,day,year

.cs文件开端化那八个下拉列表

复制代码 代码如下:

本文由金沙澳门官网网址发布于澳门金莎娱乐手机版,转载请注明出处:listBbox控件用法,net与Javascript调整的日期控件

关键词: