代码:(来自《asp.net 2.0 揭秘》)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="9_3.aspx.cs" Inherits="_9_3" %>
<!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>留言板</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server" DataSourceID="SqlDataSource1" DefaultMode="Insert">
<InsertItemTemplate>
<br />
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
</asp:TextBox><br />
Comments:
<asp:TextBox ID="CommentsTextBox" runat="server" Text='<%# Bind("Comments") %>' Columns="60" Rows="8" TextMode="MultiLine"></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" CommandName="Insert" Text="submit" />
</InsertItemTemplate>
<EditItemTemplate>
<br />
</EditItemTemplate>
<ItemTemplate>
Id:
<asp:Label ID="IdLabel" runat="server" Text='<%# Eval("Id") %>'></asp:Label><br />
IPAddress:
<asp:Label ID="IPAddressLabel" runat="server" Text='<%# Bind("IPAddress") %>'></asp:Label><br />
Name:
<asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>'></asp:Label><br />
Comments:
<asp:Label ID="CommentsLabel" runat="server" Text='<%# Bind("Comments") %>'></asp:Label><br />
EntryDate:
<asp:Label ID="EntryDateLabel" runat="server" Text='<%# Bind("EntryDate") %>'></asp:Label><br />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="新建">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True"
SortExpression="Id" />
<asp:BoundField DataField="IPAddress" HeaderText="IPAddress" SortExpression="IPAddress" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
<asp:BoundField DataField="EntryDate" HeaderText="EntryDate" SortExpression="EntryDate" />
</Columns>
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:GuestBookDBConnectionString %>"
InsertCommand="insert GuestBook (IPAddress,Name,Comments) values (@IPAddress,@Name,@Comments)"
SelectCommand="SELECT [Id], [IPAddress], [Name], [Comments], [EntryDate] FROM [GuestBook] ORDER BY [Id]">
<InsertParameters>
<asp:ControlParameter Name="IPAddress" ControlID="_page" PropertyName="IPAddress" />
<asp:Parameter Name="Name" />
<asp:Parameter Name="Comments" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
|
注意:控件参数的ControlID属性值为_page,这个值为Page类自动生成的ID。而PropertName属性的值为IpAddress,这个值在同一页面中定义的。