


The previous article roughly talked about some basic knowledge of DataList. Mastering this knowledge will play a big role in future applications. Now we will start to talk about the basic knowledge mentioned in the previous article and make a small example. .
First, there is a person table in the database of my machine, as shown in the figure below.
## Now, we use the DataList control to display the information in the table. And you can edit the tables in the database on the DataList control.
## 1. First create a web application with vs, add a web form, pull in the DataList control in the web form, right-click the control, and select the edit item template. Here we can See four templates, two of which are SelectedItemTemplate and EditItemTemplate. Pull in two LinkButton controls in the ItemTemplate template. One changes the Text to view and the CommandName property to select. The other changes the Text to Edit and its CommandName property. into edit. Then create a SelectedItemTemplate template on the HTML page and bind all the employee's information in the template. (Here is the function of viewing employee details).2. Add two LinkButton controls to the EditItemTemplate template item. The Text properties are save and cancel respectively, and the CommandName properties are update and cancel respectively. Then add a TextBox control for Enter the name to implement the function of modifying the employee's name here.
3. We can also change the style of the table, the color of the font, and the distance of the grid in the attribute generator. I will not go into details here, and finally end Template editing.
The code in the ItemTemplate template (used to display the employee’s name)
<ItemTemplate>
<asp:LinkButton ID="lbtnShowDetails" runat="server" CommandName="select" ForeColor="Red">查看</asp:LinkButton>
<asp:LinkButton ID="lbtnEdit" runat="server" CommandName="edit" ForeColor="Red">编辑</asp:LinkButton>
<%# DataBinder.Eval(Container.DataItem,"personName") %>
</ItemTemplate>
<SelectedItemTemplate>
员工编号: <%# DataBinder.Eval(Container.DataItem,"pID") %>
<br />
员工姓名: <%# DataBinder.Eval(Container.DataItem,"personName") %>
<br />
员工性别: <%# DataBinder.Eval(Container.DataItem,"personSex") %>
</SelectedItemTemplate>
<EditItemTemplate> <asp:LinkButton ID="lbtnupdate" runat="server" CommandName="update">保存</asp:LinkButton> <asp:LinkButton ID="lbtnCancel" runat="server" CommandName="cancel">取消</asp:LinkButton> <br /> 员工编号:<%# DataBinder.Eval(Container.DataItem,"pID") %> <br />姓名:<asp:TextBox ID="txtName" runat="server" <span style="color:#FF0000;">Text='<%# DataBinder.Eval(Container.DataItem,"personName") %>'</span> Width="50px"> </asp:TextBox> </EditItemTemplate>
## Finally, there are the header and footer templates
<HeaderTemplate>
模板的页眉
</HeaderTemplate>
<FooterTemplate>
<br />
模板的页脚
</FooterTemplate>
## 5. The edited front-end interface is as follows
## 6. Writing of background code
6.1. Write the DataList data binding method
private void dataBindToDataList() { SqlConnection con = DB.createConnection(); SqlDataAdapter sda = new SqlDataAdapter(); string sql = "select * from person "; sda.SelectCommand = new SqlCommand(sql, con); DataSet ds = new DataSet(); sda.Fill(ds, "per"); DataList1.DataKeyField = "pID"; //将主键存入到DataKeys集合当中,以便后面对某一条数据进行编辑。 DataList1.DataSource = ds.Tables["per"]; DataList1.DataBind(); }
6.2. Write the Page_Loda event, Determine whether the page is loaded for the first time and bind data when the page is loaded for the first time.
protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this.dataBindToDataList(); } }
6.3. Write the DataList1_ItemCommand event to implement the function of viewing employee details (provided that we have bound the employee details in the SelectedItemTemplate template, now we just call the method Display it)
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)// e表示DataList传递给该函数的信息。 { if (e.CommandName == "select") { this.DataList1.SelectedIndex = e.Item.ItemIndex; this.dataBindToDataList(); } }
## 6.4. Write the DataList1_EditCommand event to implement editing Function to display the information in the EditItemTemplate template.
这时候,编辑模板项的绑定信息就会显示出来,我们可以在这更改姓名,或者取消编辑,效果图如下 最后是取消修改功能的代码、更新功能的代码、删除功能的代码,事件分别为DataList1_CancelCommand、DataList1_UpdateCommand、DataList1_DeleteCommand。 总结 用DataList控件实现对数据库中person表的操作,实现查看详细信息,修改操作,大致流程是先修改DataList控件的各个模板中绑定的数据,然后等待具体的事件使该模板中的内容显示出来,最后再对数据进行操作。当数据适配器DateAdapter对象将数据源中的数据填充到DataSet中后,我么可以用DataList.DataKeyField=“主键字段名” 语句将主键添加到DataList的DataKeys集合中,当我们要修改数据的时候可以再从该集合中取出要编辑的数据项的主键,语句为DataList1.DataKeys[e.Item.ItemIndex]。这样我们就可以随心所欲的修改DataList表中的数据项了。protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)// e表示DataList传递给该函数的信息。
{
this.DataList1.EditItemIndex = e.Item.ItemIndex;//e.Item表示DataList中发生事件的那一项
this.dataBindToDataList();
}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)// e表示DataList传递给该函数的信息。
{
DataList1.EditItemIndex = -1; //当EditItemIndex属性值为-1时,表示不显示EditItemTemplate模板
dataBindToDataList();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
string ID =DataList1.DataKeys[e.Item.ItemIndex].ToString();
string name = ((TextBox)e.Item.FindControl("txtName")).Text ;
SqlConnection con = DB.createConnection();
SqlCommand cmd = new SqlCommand("update person set personName='"+name+"'where pID='"+ID+"'",con);
cmd.ExecuteNonQuery();
DataList1.EditItemIndex = -1;
dataBindToDataList();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
string ID = DataList1.DataKeys[e.Item.ItemIndex].ToString();
SqlConnection con = DB.createConnection();
SqlCommand cmd = new SqlCommand("delete from person where pID='" + ID + "'", con);
cmd.ExecuteNonQuery();
DataList1.EditItemIndex = -1;
dataBindToDataList();
}
The above is the detailed content of 'ASP.NET' Data Binding—Detailed Explanation of the Graphical and Text Code of DataList Practice. For more information, please follow other related articles on the PHP Chinese website!

如何使用MySQL在SwiftUI中实现数据绑定功能在SwiftUI开发中,通过数据绑定可以实现界面与数据的自动更新,提高用户体验。而MySQL作为一款流行的关系型数据库管理系统,可以存储和管理大量的数据。本文将介绍如何使用MySQL在SwiftUI中实现数据绑定功能。我们将利用Swift的第三方库MySQLConnector,它提供了连接和查询MySQL数

Vue是一款开放源代码的JavaScript框架,它主要用于构建用户界面。Vue的核心是数据绑定,它提供了一种方便、高效的方式来实现数据和视图之间的双向绑定。Vue的数据绑定机制是通过一些特殊的函数来处理的。这些函数可以帮助我们将模板中的数据自动与JavaScript对象中的对应属性绑定起来,使得在修改JavaScript对象中的属性时,模板中的数据也会自动

Vue是一个流行的前端JavaScript框架,它提供了许多指令来简化数据绑定的过程,其中一个非常有用的指令是v-once。在这篇文章中,我们将深入探讨v-once指令的用法,以及如何在Vue中实现数据绑定的一次性渲染。什么是v-once指令?v-once是Vue中的一个指令,它的作用是将元素或组件的渲染结果缓存起来,以便于在后续的更新中跳过它们的渲染过程。

Vue报错:无法正确使用v-model进行双向数据绑定,如何解决?引言:在使用Vue进行开发时,双向数据绑定是一项非常常见且强大的功能。然而,有时候我们可能会遇到一个问题,就是当我们尝试使用v-model进行双向数据绑定时,却遭遇到了报错。本文将介绍该问题的原因以及解决方案,并通过代码示例来演示如何解决该问题。问题描述:当我们在Vue中尝试使用v-model

随着前端技术的不断发展,Vue作为一款流行的前端框架,也在不断地更新迭代。其中最新的版本Vue3,引入了许多新特性,使得其在使用方面更加便利和灵活。其中,v-model函数就是Vue3中值得一提的新特性之一。它能够实现双向数据绑定,也就是说,在使用v-model函数时,不仅可以方便地实现父子组件之间的通信,同时还可以自动将用户输入的数据与组件中的数据绑定起来

如何使用Vue进行表单验证和数据绑定引言:随着前端开发的不断发展,用户输入的表单验证成为一个重要的环节。Vue.js作为一个流行的前端框架,提供了一系列的功能来简化表单验证和数据绑定的过程。本文将介绍如何使用Vue进行表单验证和数据绑定,并给出相应的代码示例。一、基本的数据绑定:在Vue中,我们可以使用v-model指令来实现数据的双向绑定。将input元素

随着Java程序越来越庞大,数据绑定也变得更加重要。但是,当Java应用程序遇到数据绑定错误时,这可能会导致不可预料的问题,如系统和资源崩溃,数据极性错误,系统响应时间变慢或停止响应等。因此,本文将介绍一些常见的Java数据绑定错误,以及如何解决和避免这些错误。错误1:空指针异常空指针异常是一种典型的Java错误。它发生在试图使用Null对象时。在数据绑定时

Vue是一种流行的JavaScript框架,它提供了一种方便的方法实现数据的双向绑定。本文将介绍Vue如何实现数据的双向绑定。Vue通过MVVM框架实现双向绑定,MVVM模式由Model-View-ViewModel组成。Model表示数据和业务逻辑,View表示UI界面,ViewModel是Model和View之间的桥梁。在Vue中,数据绑定是根据Vue实


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
