ASP.NET 数据绑定详解 代码+步骤(上)

简介: ASP.NET 数据绑定详解 代码+步骤

文章目录


1. 数据绑定概述

2. 简单数据绑定

2.1. 属性绑定

2.2. 表达式绑定

2.3. 集合绑定

2.4. 方法绑定

3. 数据控件绑定

3.1. ListControl 控件

3.2. GridView 控件

3.2.2. 使用GridView控件绑定数据源

3.2.3. 自定义GridView控件的列字段名

3.2.4. 使用GridView控件分页显示数据

3.2.5. 选中、编辑和删除GridView数据项

3.3. DataList 控件

3.3.1. DataList 模板

3.3.2. 分页显示DataList控件中的数据

3.4. ListView控件

3.4.1. ListView控件概述

3.4.2. ListView控件的模板

3.4.3. 使用ListView控件对数据进行显示、分页和排序


正文


1. 数据绑定概述


数据绑定指的是从数据源获取数据,或者向数据源写入数据。主要有两种形式:简单数据绑定数据控件绑定

其中简单数据绑定分为属性绑定、表达式绑定、集合绑定和方法绑定;数据控件绑定分为ListControl 控件、GridView 控件、DataList 控件和ListView控件


2. 简单数据绑定


2.1. 属性绑定


基于属性的数据绑定所涉及的属性必须要包含Get访问器,它的基本语法为:

<%# 属性名称 %>

如果在ASP页面中对属性进行了绑定,那么需要调用Page类的DataBind方法才能执行绑定操作,例如下例:

// .aspx文件
<body>
    <form id="form1" runat="server">
        <div>
          <!-- 数据绑定 -->
            <asp:Label ID="Label1" runat="server" Text="图书名称:"><%# BookName %></asp:Label>
        </div>
        <div>
          <!-- 数据绑定 -->
            <asp:Label ID="Label2" runat="server" Text="图书价格:"><%# BookPrice %></asp:Label>
        </div>
    </form>
</body>
// .aspx.cs文件
// BookName属性
public string BookName {
    get {
        return "ASP.NET程序设计";
    }
}
// BookPrice属性
public string BookPrice {
    get {
        return "49";
    }
}
protected void Page_Load(object sender, EventArgs e){
    // 调用DataBind()方法执行绑定
    Page.DataBind();
}


执行结果如下:

6.png


2.2. 表达式绑定


表达式绑定的语法与属性绑定一致,它可以在属性绑定的基础上通过表达式对数据进行处理,它的语法如下:

<%# 表达式 %>

例如在属性绑定的基础上,要查询10本书的总价格,则代码如下:

// .aspx文件
<body>
    <form id="form1" runat="server">
        <div>
          <!-- 数据绑定 -->
            <asp:Label ID="Label1" runat="server" Text="图书名称:"><%# BookName %></asp:Label>
        </div>
        <div>
          <!-- 表达式绑定 -->
            <asp:Label ID="Label2" runat="server" Text="图书价格:"><%# Convert.ToInt32(BookPrice)*10 %></asp:Label>
        </div>
    </form>
</body>
// .aspx.cs文件
// BookName属性
public string BookName {
    get {
        return "ASP.NET程序设计";
    }
}
// BookPrice属性
public string BookPrice {
    get {
        return "49";
    }
}
protected void Page_Load(object sender, EventArgs e){
    // 调用DataBind()方法执行绑定
    Page.DataBind();
}


执行结果如下所示

10.png


2.3. 集合绑定


有一些服务器控件是多记录控件,如DropDownList控件, 这类控件即可使用集合作为数据源对其进行绑定。通常情况下,集合数据源主要包括ArrayList、Hashtabel、 DataView、 DataReader 等。


例如下面代码,实现了对DropDownList控件进行数据绑定:

// .aspx文件
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" Text="图书名称:"></asp:Label>
            <asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
        </div>
    </form>
</body>
protected void Page_Load(object sender, EventArgs e){
    // 构建数据源
    ArrayList arrayList = new ArrayList();
    arrayList.Add("iisexpress");
    arrayList.Add("assembly");
    arrayList.Add("Windows");
  // 指定数据源
    DropDownList1.DataSource = arrayList;
    // 数据绑定
    DropDownList1.DataBind();
}

执行结果如下:


11.png


2.4. 方法绑定


方法的绑定与属性绑定的语法是一致的,只不过把绑定对象更替为了方法,例如下面的代码:

// .aspx文件
<body>
    <form id="form1" runat="server">
        <div>
          <!-- 数据绑定 -->
            <asp:Label ID="Label1" runat="server" Text="图书名称:"><%# BookName %></asp:Label>
        </div>
        <div>
          <!-- 方法绑定 -->
            <asp:Label ID="Label2" runat="server" Text="图书价格:"><%# PriceTest("49") %></asp:Label>
        </div>
    </form>
</body>
// .aspx.cs文件
public string PriceTest(string _price) {
    return _price;
}
protected void Page_Load(object sender, EventArgs e){
    // 调用DataBind()方法执行绑定
    Page.DataBind();
}


执行结果如下:

9.png


3. 数据控件绑定


3.1. ListControl 控件


ListControl控件用来定义所有列表类型控件的所有属性方法和事件,是一个抽象基类,它能够控制的控件主要包括:DropDownList控件、ListBox控件、CheckBoxList控件、RadioButtonList控件。


ListControl控件能够指定能够用来填充列表控件的数据源,其中与数据绑定修改的属性有:


DataSource属性,用来设置数据源

DataTextField属性,用来指定提供文本内容的数据源字段

DataValueField属性,用来指定提供值的数据源字段

例如下面代码,将图书列表以DropDownList控件进行显示,当用户选择了DropDownList控件中的某个值后,页面会进行相应的信息更新。

// .aspx文件
<body>
    <form id="form1" runat="server">
        <div>
          <!-- ListControl控件绑定DropDownList -->
            <asp:Label ID="Label1" runat="server" Text="图书列表:"><%# BookName %></asp:Label>
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
        </div>
        <div>
            <asp:Label ID="Label2" runat="server" Text="图书编码:"></asp:Label>
            <asp:Label ID="Label3" runat="server" Text=""></asp:Label>
        </div>
        <div>
            <asp:Label ID="Label4" runat="server" Text="图书名称:"></asp:Label>
            <asp:Label ID="Label5" runat="server" Text=""></asp:Label>
        </div>
    </form>
</body>
// .aspx.cs文件
protected void Page_Load(object sender, EventArgs e){
    if (!IsPostBack) {
        SqlConnection sqlConnection = new SqlConnection("Server=DEITIVOD;Database=db_LibraryMS;User Id=sa;pwd=admin");
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select book_code, book_name from tb_bookinfo;", sqlConnection);
        // 将查询到的数据放入一个DataSet
        DataSet dataSet = new DataSet();
        sqlDataAdapter.Fill(dataSet);
        // 对DropDownList执行指定数据源和数据绑定
        DropDownList1.DataSource = dataSet;
        DropDownList1.DataTextField = "book_name"; // 指定要显示的字段
        DropDownList1.DataValueField = "book_code"; // 指定要绑定的主键值
        DropDownList1.DataBind();
    }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){
    Label3.Text = DropDownList1.SelectedValue;
    Label5.Text = DropDownList1.SelectedItem.Text;
}


执行效果如下:

数据库中的tb_bookinfo数据表的数据如下所示:


7.png

ASP页面中显示的数据:

8.png

相关文章
|
2月前
|
SQL 数据库连接 数据库
你不知道ADo.Net中操作数据库的步骤【超详细整理】
你不知道ADo.Net中操作数据库的步骤【超详细整理】
|
2月前
|
JSON IDE 前端开发
[.NET开发者的福音]一个方便易用的在线.NET代码编辑工具.NET Fiddle
[.NET开发者的福音]一个方便易用的在线.NET代码编辑工具.NET Fiddle
|
8月前
|
网络协议 算法 Shell
来我们探究一下net/http 的代码流程
来我们探究一下net/http 的代码流程
|
29天前
|
XML 开发框架 .NET
【.NET Core】常见C#代码约定
【.NET Core】常见C#代码约定
21 5
|
5天前
|
开发框架 JavaScript 前端开发
详细解读ASP常用三十三种代码
详细解读ASP常用三十三种代码
|
5天前
|
开发框架 JavaScript 前端开发
详细解读ASP常用三十三种代码
详细解读ASP常用三十三种代码
|
2月前
|
SQL 开发框架 前端开发
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
ASP.NET WEB项目中GridView与Repeater数据绑定控件的用法
49 0
|
2月前
云效静态代码检测可以检测.net吗?
云效静态代码检测可以检测.net吗?
36 1
|
2月前
|
并行计算 算法 调度
(学习笔记)U-net++代码解读
python: 3.10 U-net++结构图
195 0
|
2月前
|
SQL 数据库连接 数据库
VB.NET 中使用SqlConnection类连接到Microsoft SQL Server数据库的详细步骤
VB.NET 中使用SqlConnection类连接到Microsoft SQL Server数据库的详细步骤
146 0