看LoveCherry写的ASP.NET第一步之数据绑定

  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Web;
  5using System.Web.Security;
  6using System.Web.UI;
  7using System.Web.UI.WebControls;
  8using System.Web.UI.WebControls.WebParts;
  9using System.Web.UI.HtmlControls;
 10using System.Collections;
 11using System.Data.SqlClient;
 12using System.IO;
 13
 14public partial class _Default : System.Web.UI.Page 
 15{
 16    protected void Page_Load(object sender, EventArgs e)
 17    {
 18        //构造数据源
 19        //字符串数组
 20        string[] DataSource1 = new string[] {"项目1","项目2","项目3" };
 21        //哈西表
 22        Hashtable DataSource2 = new Hashtable(3);
 23        DataSource2.Add("项目1""值1");
 24        DataSource2.Add("项目2""值2");
 25        DataSource2.Add("项目3""值3");
 26        //数据集
 27        DataSet DataSource3 = new DataSet();
 28        string connString="server=.\\sqlexpress;database=Clients;uid=sa;pwd=123456;";
 29        using (SqlConnection conn=new SqlConnection(connString))
 30        {
 31            SqlDataAdapter sda = new SqlDataAdapter("select * from OrderClient",conn);
 32            sda.Fill(DataSource3);
 33        }

 34        //目录集合
 35        DirectoryInfo di = new DirectoryInfo("D:\\");
 36        DirectoryInfo[] DataSource4 = di.GetDirectories();
 37        //动态数组
 38        ArrayList DataSource5 = new ArrayList();
 39        DataSource5.Add(new Link("谷歌","http://www.google.com"));
 40        DataSource5.Add(new Link("百度","http://www.baidu.com"));
 41
 42        if (!IsPostBack)
 43        {
 44            //第一次请求
 45            //绑定数据
 46            DropDownList1.DataSource = DataSource1;
 47            DropDownList1.DataBind();
 48            DropDownList1.Items.Insert(0"请选择");//放在DataBind方法之后,不然会被数据绑定覆盖
 49
 50            ListBox1.DataSource = DataSource2;
 51            ListBox1.DataTextField = "key";
 52            ListBox1.DataValueField = "value";
 53            ListBox1.DataBind();
 54
 55
 56            CheckBoxList1.DataSource = DataSource3;
 57            CheckBoxList1.DataTextField = "ClientName";
 58            CheckBoxList1.DataTextFormatString = "分类;{0}";
 59            CheckBoxList1.DataValueField = "ClientID";
 60            CheckBoxList1.DataBind();
 61
 62            RadioButtonList1.DataSource = DataSource4;
 63            RadioButtonList1.DataTextField = "Name";
 64            RadioButtonList1.DataValueField = "FullName";
 65            RadioButtonList1.DataBind();
 66
 67
 68            BulletedList1.DataSource = DataSource5;
 69            BulletedList1.DataTextField = "Name";
 70            BulletedList1.DataValueField = "Url";
 71            BulletedList1.DataBind();
 72        }

 73        
 74    }

 75    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
 76    {
 77        Response.Write(DropDownList1.SelectedValue);
 78    }

 79    protected void Button1_Click(object sender, EventArgs e)
 80    {
 81        foreach (ListItem li in ListBox1.Items)
 82        {
 83            if (li.Selected)
 84            {
 85                Response.Write(string.Format("{0}:{1}<br>",li.Text,li.Value));
 86            }

 87        }

 88    }

 89    protected void Button2_Click(object sender, EventArgs e)
 90    {
 91        foreach (ListItem li in CheckBoxList1.Items)
 92        {
 93            li.Selected = true;
 94        }

 95    }

 96    protected void Button3_Click(object sender, EventArgs e)
 97    {
 98        foreach (ListItem li in CheckBoxList1.Items)
 99        {
100            li.Selected = false;
101        }

102    }

103    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
104    {
105        Response.Write(RadioButtonList1.SelectedValue);
106    }

107}

108
109
110
111
112
113
114
115
116using System;
117using System.Data;
118using System.Configuration;
119using System.Web;
120using System.Web.Security;
121using System.Web.UI;
122using System.Web.UI.WebControls;
123using System.Web.UI.WebControls.WebParts;
124using System.Web.UI.HtmlControls;
125
126/// <summary>
127/// Link 的摘要说明
128/// </summary>

129public class Link
130{
131    public Link(string Name,string Url)
132    {
133        this.Name = Name;
134        this.Url = Url;
135    }

136
137    private string name;
138
139    public string Name
140    {
141        get return name; }
142        set { name = value; }
143    }

144
145    private string url;
146
147    public string Url
148    {
149        get return url; }
150        set { url = value; }
151    }

152}

153
原文地址:https://www.cnblogs.com/mdy41034264/p/1350688.html