FormView display, update, insert, and delete database operations [ASP. NET source code] (1)

Source: Internet
Author: User

 

FormView can display data in a table by page. Each page only shows one item in the table. Its biggest feature is the ability to edit templates freely, which is generally used to display the details of a product. FormView has three editable templates: ItemTemplate, EditItemTemplate, and InsertItemTemplate. It is commonly used to manage database table data and display, edit, insert, and delete data items in the table.
1. Use the FormView control to display values in the SqlDataSource Control


1. Set the FormView control. Pay attention to the DataKeyNames = "ItemID" item.
2. Set the SqlDataSource attribute. Because two inline tables are to be queried, both tables have a Name field, so aliases are used.
3. Edit the ItemTemplate. The "edit", "delete", and "new" buttons are added. The "edit" and "new" buttons have CommandName attributes, edit and New, respectively. Click to enter EditItemTemplate, InsertItemTemplate, and template respectively. The CommandName attribute of the Delete button is Delete. Click to execute the DeleteCommand command in SqlDataSource, you can also issue commands such as OnItemDeleting to complete some functions before deletion.
4. The Form file code is as follows:
[Html]
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "FormViewDemo1.aspx. cs" Inherits = "FormViewDemo1" %>
 
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> KFC ordering system </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<H3> display, update, insert, and delete databases in FormView <Asp: FormView ID = "fvwItem" performanceid = "sdsItem" runat = "server"
AllowPaging = "True"
DataKeyNames = "ItemID"
EmptyDataText = "the database does not have any data temporarily">

<RowStyle BackColor = "Yellow" Wrap = "False"/>
<InsertRowStyle BackColor = "GreenYellow" Wrap = "False"/>
<EditRowStyle BackColor = "LightPink" Wrap = "false"/>

<ItemTemplate>
<Table border = "0" cellpadding = "0" cellspacing = "0" width = "420">
<Tr>
<Td colspan = "6" height = "30" width = "420" align = "center">
<H4> FormView ItemTemplate </Td>
</Tr>
<Tr>
<Td width = "30">
</Td>
& Lt; td rowspan = "4" width = "120" & gt;
<Asp: Image Width = "120" Height = "120" ID = "imgItem" ImageUrl = '<% # Eval ("Image ") %> 'alternatetext = '<% # Eval ("Name") %>'
Runat = "server"/> </td>
<Td width = "30">
</Td>
<Td width = "60">
</Td>
<Td width = "60">
</Td>
<Td width = "60">
</Td>
<Td width = "60">
</Td>
</Tr>
<Tr>
<Td width = "30">
</Td>
<Td width = "30">
</Td>
<Td width = "60">
Category: </td>
<Td colspan = "2">
<% # Eval ("CategoryName") %>
</Td>
<Td width = "60">
</Td>
</Tr>
<Tr>
<Td width = "30">
</Td>
<Td width = "30">
</Td>
<Td width = "60">
Name: </td>
<Td colspan = "2">
<% # Eval ("Name") %>
</Td>
<Td width = "60">
</Td>
</Tr>
<Tr>
<Td width = "30">
</Td>
<Td width = "30">
</Td>
<Td width = "60">
Price:
</Td>
<Td colspan = "2">
<% # Eval ("Price") %>
</Td>
<Td width = "60">
</Td>
</Tr>
<Tr>
<Td height = "30" width = "30">
</Td>
& Lt; td height = "30" width = "120" & gt;
</Td>
<Td height = "30" width = "30">
</Td>
<Td height = "30" width = "60">
</Td>

<Td height = "30" width = "60">
<Asp: Button ID = "btnEdit" runat = "server" Text = "Edit" CommandName = "Edit"/> </td>
<Td height = "30" width = "60">
<Asp: Button ID = "btnDelete" runat = "server" Text = "Delete" CommandName = "Delete"/> </td>
<Td height = "30" width = "60">
<Asp: Button ID = "btnNew" runat = "server" Text = "New" CommandName = "New"/> </td>
</Tr>
</Table>
</ItemTemplate>
</Asp: FormView>
<Asp: SqlDataSource ID = "sdsItem" runat = "server" ConnectionString = "<% $ ConnectionStrings: NetShopConnString %>"
SelectCommand = "SELECT Item. itemId AS ItemId, Item. categoryId AS CategoryId, Item. name AS Name, Item. price AS Price, Item. image AS Image, Category. name As CategoryName FROM Item inner join Category ON Item. categoryId = Category. categoryId ">
</Asp: SqlDataSource>
</Form>
</Body>
</Html>

 
5. The code page does not require any code. You can view the running diagram in the browser, as shown in 1.

 

From ASP. NET Technical Exchange

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.