数据绑定(重复值绑定)

       重复值绑定可以讲列表信息绑定到控件上。列表信息可以是自定义的对象集合(如 ArrayList 或 HashTable),也可以是行的集合(如 DataReader 或 DataSet)。

       ASP.NET 带有几个支持重复值绑定的基本列表控件:

  • 所有用 <select> 标签呈现的控件:HtmlSelect、ListBox、DropDownList
  • CheckBoxList(复选框列表) 和 RadioButtonList(单选按钮列表)
  • BulletedList (各种符号的无序列表)

       所有这些控件显示来自数据项的单值字段。这些控件共有的属性如下:

DataSource 它指定要显示的数据对象,该数据对象必须实现 ASP.NET 数据绑定支持的一个接口,通常是 ICollection
DataSourceID 除了编程设置数据对象外,还可以设置该属性链接到数据源控件。
可以随意使用 DataSource 或 DataSourceID,但它们不能同时使用。
DataTextField 指定显示在页面上的值的字段(绑定到行时)或属性(绑定到对象时)
DataTextFormatString 定义一个可选的格式化字符串,控件显示前使用该字符串格式化 DataTextValue 的值。
例如,可以指定数字需要被格式化为货币值
DataValueField 该属性从数据项获得的值不会在页面中显示,相反,它被保存到底层 HTML 标签的 value 特性上。
它允许你在代码中读取这个值,通常用来保存一个唯一的 ID 或 字段的主键。

       看一个小示例:

<select runat="server" id="Select1" datatextfield="value" datavaluefield="key" size="3">
</select>
<select runat="server" id="Select2" datatextfield="value" datavaluefield="key">
</select>
<asp:ListBox ID="ListBox1" runat="server" DataTextField="value" DataValueField="key" Rows="3">
</asp:ListBox>
<asp:DropDownList ID="DropDownList1" runat="server" datatextfield="value" datavaluefield="key">
</asp:DropDownList>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" datatextfield="value" datavaluefield="key">
</asp:RadioButtonList>        
<asp:CheckBoxList ID="CheckBoxList1" runat="server" datatextfield="value" datavaluefield="key">
</asp:CheckBoxList><br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        System.Collections.Hashtable ht = new System.Collections.Hashtable();
        ht.Add("Key1", "AAA");
        ht.Add("Key2", "BBB");
        ht.Add("Key3", "CCC");
 
        Select1.DataSource = ht;
        Select2.DataSource = ht;
        ListBox1.DataSource = ht;
        DropDownList1.DataSource = ht;
        RadioButtonList1.DataSource = ht;
        CheckBoxList1.DataSource = ht;
        this.DataBind();
    }
}
 
protected void Button1_Click(object sender, EventArgs e)
{
    ListItem li;
    li = Select1.Items[Select1.SelectedIndex];
    li = Select2.Items[Select2.SelectedIndex];
    li = ListBox1.SelectedItem;
    li = DropDownList1.SelectedItem;
    li = RadioButtonList1.SelectedItem;
 
    foreach (ListItem tmpLi in CheckBoxList1.Items)
    {
        if (tmpLi.Selected)
        {
            li = tmpLi;
        }
    }
 
    Button1.Text = li.Text;
    Button1.Text = li.Value;        
}

 

       除了简单的列表控件外,ASP.NET 还有支持重复值绑定的富数据控件。两者的差别很大,富数据控件只为数据绑定而设计,它们拥有显示数据项若干属性或字段的能力,一般基于表或用户定义的模版来布局,还有些高级的功能,如编辑,还提供了一些事件让你可以在不同的时间介入控件内部的工作

       富数据控件包括以下几个,以后会详细介绍它们的用法:

  • GridView (显示大型表数据的全能网格,支持编辑、排序、分页)
  • DetailsView (每次显示一条记录的理想控件,支持编辑、允许在一系列记录间浏览)
  • FormView (与 DetailsView 类似,差别在于 FormView 是基于模版的,允许在更为灵活的布局中合并字段而不必依赖表格)

 

       浏览一个 GridView 的示例:

protected void Page_Load(object sender, EventArgs e)
{
    string sonStr = WebConfigurationManager.ConnectionStrings["NorthWind"].ConnectionString;
    SqlConnection conn = new SqlConnection(sonStr);
    string sql = "select EmployeeID,FirstName,LastName,Title,City from employees";
    SqlCommand cmd = new SqlCommand(sql, conn);
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    GridView1.DataSource = reader;
    GridView1.DataBind();
    conn.Close();
}

image

posted on 2012-07-25 11:01  SkySoot  阅读(1150)  评论(0编辑  收藏  举报

导航