c#操作asp.net listBox控制項

來源:互聯網
上載者:User
<%@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    //增加建立項    protected void Button1_Click(object sender, EventArgs e)    {        ListItem newItem = new ListItem();        newItem.Text = this.TextBox3.Text;        newItem.Value = this.TextBox4.Text;               this.ListBox1.Items.Add(newItem);    }    //得到設定最大數量    //原Capacity的容量為8,預設的情況下會隨著新增項(ListItem)的數量增多會成2倍的增長8 16 32..    //但如果設定了Capacity的大小,就不會自動成長了    protected void Button2_Click(object sender, EventArgs e)    {        Response.Write("原大小:"+this.ListBox1.Items.Capacity.ToString());        this.ListBox1.Items.Capacity = 6;        Response.Write("更改後:"+this.ListBox1.Items.Capacity.ToString());            }    //清空items集合中所有的項    protected void Button3_Click(object sender, EventArgs e)    {        this.ListBox1.Items.Clear();    }    //判斷集合中是否包含指定項    protected void Button4_Click(object sender, EventArgs e)    {        ListItem item = new ListItem(this.TextBox1.Text, this.TextBox2.Text);        if (this.ListBox1.Items.Contains(item))        {            Response.Write("yes"); ;        }        else        {            Response.Write("no");        }            }    //將items中的所有項複製到listItemCollection集合中    protected void Button5_Click(object sender, EventArgs e)    {        ListItem[] items=new ListItem[this.ListBox1.Items.Count];        this.ListBox1.Items.CopyTo(items, 0);                ListBox listBox2 = new ListBox();        listBox2.ID = "ListBox2";        listBox2.Items.AddRange(items);                //得到ListBox1的位置,並將listBox2放到ListBox1後面        int position = 0;        foreach (Control c in this.form1.Controls)        {            position++;            if (c.ID == "ListBox1")            {                break;            }        }        this.form1.Controls.AddAt(position,listBox2);            }    //得到集合中項的數量    protected void Button6_Click(object sender, EventArgs e)    {        Response.Write(this.ListBox1.Items.Count.ToString());    }    //根據文本尋找,如果找到則返回此項,如果找不到則返加null    protected void Button7_Click(object sender, EventArgs e)    {        ListItem item= this.ListBox1.Items.FindByText(this.TextBox1.Text);        if (item != null)        {            Response.Write(item.Text+":"+item.Value);        }            }    //根據值尋找,如果找到則返回此項,如果找不到則返加null    protected void Button8_Click(object sender, EventArgs e)    {        ListItem item = this.ListBox1.Items.FindByText(this.TextBox2.Text);        if (item != null)        {                        Response.Write(item.Text + ":" + item.Value);        }              }    //所尋找項的索引    protected void Button9_Click(object sender, EventArgs e)    {        ListItem item = this.ListBox1.Items.FindByText(this.TextBox1.Text);        if (item != null)        {            int position = this.ListBox1.Items.IndexOf(item);            Response.Write("所尋找項的索引:"+position.ToString());        }    }    //插入建立項    protected void Button10_Click(object sender, EventArgs e)    {        ListItem newItem=new ListItem(this.TextBox1.Text,this.TextBox2.Text);        this.ListBox1.Items.Insert(0, newItem);    }    //刪除節點    protected void Button11_Click(object sender, EventArgs e)    {        ListItem item = new ListItem(this.TextBox1.Text, this.TextBox2.Text);        this.ListBox1.Items.Remove(item);    }    //根據索引位置刪除    protected void Button12_Click(object sender, EventArgs e)    {        int index = this.ListBox1.Items.IndexOf(new ListItem(this.TextBox1.Text, this.TextBox2.Text));        this.ListBox1.Items.RemoveAt(index);    }    //選中項    protected void Button13_Click(object sender, EventArgs e)    {        this.TextBox1.Text = this.ListBox1.SelectedItem.Text;        this.TextBox2.Text = this.ListBox1.SelectedItem.Value;    }    //修改    protected void Button14_Click(object sender, EventArgs e)    {        this.ListBox1.SelectedItem.Text = this.TextBox3.Text;        this.ListBox1.SelectedItem.Value = this.TextBox4.Text;    }</script><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title>無標題頁</title></head><body>    <form id="form1" runat="server">    <div>        <asp:ListBox ID="ListBox1"  runat="server">            <asp:ListItem Value="0">張三</asp:ListItem>            <asp:ListItem Value="1">李四</asp:ListItem>            <asp:ListItem Selected="True" Value="2">王五</asp:ListItem>            <asp:ListItem Value="3">趙六</asp:ListItem>            <asp:ListItem  Value="4">王七</asp:ListItem>        </asp:ListBox>        <br />        <br />        oldText           <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />        oldValue         <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />        <br />        newText         <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br />        newValue<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br />        <br />        <asp:Button ID="Button1" runat="server" Text="add" OnClick="Button1_Click"  /><br />        <asp:Button ID="Button2" runat="server" Text="get(Set)Capacity" OnClick="Button2_Click" /><br />        <asp:Button ID="Button3" runat="server" Text="Clear" OnClick="Button3_Click" /><br />        <asp:Button ID="Button4" runat="server" Text="Contains" OnClick="Button4_Click" /><br />        <asp:Button ID="Button5" runat="server" Text="copyTo" OnClick="Button5_Click" /><br />        <asp:Button ID="Button6" runat="server" Text="count" OnClick="Button6_Click" />        <br />        <asp:Button ID="Button7" runat="server" OnClick="Button7_Click" Text="findByText" /><br />        <asp:Button ID="Button8" runat="server" Text="findByValue" OnClick="Button8_Click" /><br />        <asp:Button ID="Button9" runat="server" Text="indexOf" OnClick="Button9_Click" />        <br />        <asp:Button ID="Button10" runat="server" Text="insert" OnClick="Button10_Click" />        <br />        <asp:Button ID="Button11" runat="server" Text="remove" OnClick="Button11_Click" />        <br />        <asp:Button ID="Button12" runat="server" Text="removeAt" OnClick="Button12_Click" />        <br />        <asp:Button ID="Button13" runat="server" Text="select" OnClick="Button13_Click" />        <br />        <asp:Button ID="Button14" runat="server" Text="edit" OnClick="Button14_Click" /></div>    </form></body></html>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.