使用dataset数据集实现ListView简单的增删该查

DataSetPerson.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="DataSetPerson" targetNamespace="http://tempuri.org/DataSetPerson.xsd" xmlns:mstns="http://tempuri.org/DataSetPerson.xsd" xmlns="http://tempuri.org/DataSetPerson.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:annotation>
    <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
      <DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
        <Connections>
          <Connection AppSettingsObjectName="Web.config" AppSettingsPropertyName="HireHourseConnectionString" ConnectionStringObject="" IsAppSettingsProperty="true" Modifier="Assembly" Name="HireHourseConnectionString (Web.config)" ParameterPrefix="@" PropertyReference="AppConfig.System.Configuration.ConfigurationManager.0.ConnectionStrings.HireHourseConnectionString.ConnectionString" Provider="System.Data.SqlClient" />
        </Connections>
        <Tables>
          <TableAdapter BaseClass="System.ComponentModel.Component" DataAccessorModifier="AutoLayout, AnsiClass, Class, Public" DataAccessorName="PersonTableAdapter" GeneratorDataComponentClassName="PersonTableAdapter" Name="Person" UserDataComponentName="PersonTableAdapter">
            <MainSource>
              <DbSource ConnectionRef="HireHourseConnectionString (Web.config)" DbObjectName="HireHourse.dbo.Person" DbObjectType="Table" FillMethodModifier="Public" FillMethodName="Fill" GenerateMethods="Both" GenerateShortCommands="true" GeneratorGetMethodName="GetData" GeneratorSourceName="Fill" GetMethodModifier="Public" GetMethodName="GetData" QueryType="Rowset" ScalarCallRetval="System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="false" UserGetMethodName="GetData" UserSourceName="Fill">
                <DeleteCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="false">
                    <CommandText>DELETE FROM [dbo].[Person] WHERE (([PID] = @Original_PID))</CommandText>
                    <Parameters>
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="Int32" Direction="Input" ParameterName="@Original_PID" Precision="0" ProviderType="Int" Scale="0" Size="0" SourceColumn="PID" SourceColumnNullMapping="false" SourceVersion="Original" />
                    </Parameters>
                  </DbCommand>
                </DeleteCommand>
                <InsertCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="false">
                    <CommandText>INSERT INTO [dbo].[Person] ([PName], [PSex], [PAge], [PJob]) VALUES (@PName, @PSex, @PAge, @PJob)</CommandText>
                    <Parameters>
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@PName" Precision="0" ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="PName" SourceColumnNullMapping="false" SourceVersion="Current" />
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@PSex" Precision="0" ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="PSex" SourceColumnNullMapping="false" SourceVersion="Current" />
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="Int32" Direction="Input" ParameterName="@PAge" Precision="0" ProviderType="Int" Scale="0" Size="0" SourceColumn="PAge" SourceColumnNullMapping="false" SourceVersion="Current" />
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@PJob" Precision="0" ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="PJob" SourceColumnNullMapping="false" SourceVersion="Current" />
                    </Parameters>
                  </DbCommand>
                </InsertCommand>
                <SelectCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="false">
                    <CommandText>SELECT PID, PName, PSex, PAge, PJob FROM dbo.Person</CommandText>
                    <Parameters />
                  </DbCommand>
                </SelectCommand>
                <UpdateCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="false">
                    <CommandText>UPDATE [dbo].[Person] SET [PName] = @PName, [PSex] = @PSex, [PAge] = @PAge, [PJob] = @PJob WHERE (([PID] = @Original_PID))</CommandText>
                    <Parameters>
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@PName" Precision="0" ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="PName" SourceColumnNullMapping="false" SourceVersion="Current" />
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@PSex" Precision="0" ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="PSex" SourceColumnNullMapping="false" SourceVersion="Current" />
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="Int32" Direction="Input" ParameterName="@PAge" Precision="0" ProviderType="Int" Scale="0" Size="0" SourceColumn="PAge" SourceColumnNullMapping="false" SourceVersion="Current" />
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="String" Direction="Input" ParameterName="@PJob" Precision="0" ProviderType="NVarChar" Scale="0" Size="0" SourceColumn="PJob" SourceColumnNullMapping="false" SourceVersion="Current" />
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="Int32" Direction="Input" ParameterName="@Original_PID" Precision="0" ProviderType="Int" Scale="0" Size="0" SourceColumn="PID" SourceColumnNullMapping="false" SourceVersion="Original" />
                    </Parameters>
                  </DbCommand>
                </UpdateCommand>
              </DbSource>
            </MainSource>
            <Mappings>
              <Mapping SourceColumn="PID" DataSetColumn="PID" />
              <Mapping SourceColumn="PName" DataSetColumn="PName" />
              <Mapping SourceColumn="PSex" DataSetColumn="PSex" />
              <Mapping SourceColumn="PAge" DataSetColumn="PAge" />
              <Mapping SourceColumn="PJob" DataSetColumn="PJob" />
            </Mappings>
            <Sources>
              <DbSource ConnectionRef="HireHourseConnectionString (Web.config)" DbObjectName="HireHourse.dbo.usp_selectcount" DbObjectType="StoredProcedure" GenerateShortCommands="true" GeneratorSourceName="fun_selectcount" Modifier="Public" Name="fun_selectcount" QueryType="Scalar" ScalarCallRetval="System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="true" UserGetMethodName="GetDataBy" UserSourceName="fun_selectcount">
                <SelectCommand>
                  <DbCommand CommandType="StoredProcedure" ModifiedByUser="false">
                    <CommandText>dbo.usp_selectcount</CommandText>
                    <Parameters>
                      <Parameter AllowDbNull="true" AutogeneratedName="" DataSourceName="" DataTypeServer="int" DbType="Int32" Direction="ReturnValue" ParameterName="@RETURN_VALUE" Precision="10" ProviderType="Int" Scale="0" Size="4" SourceColumnNullMapping="false" SourceVersion="Current" />
                    </Parameters>
                  </DbCommand>
                </SelectCommand>
              </DbSource>
              <DbSource ConnectionRef="HireHourseConnectionString (Web.config)" DbObjectName="HireHourse.dbo.Person" DbObjectType="Table" GenerateMethods="Get" GenerateShortCommands="true" GeneratorGetMethodName="GetDataByID" GetMethodModifier="Public" GetMethodName="GetDataByID" QueryType="Rowset" ScalarCallRetval="System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="true" UserGetMethodName="GetDataByID" UserSourceName="GetDataByID">
                <SelectCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="true">
                    <CommandText>SELECT PID, PName, PSex, PAge, PJob FROM dbo.Person where PID=@PID</CommandText>
                    <Parameters>
                      <Parameter AllowDbNull="false" AutogeneratedName="PID" ColumnName="PID" DataSourceName="HireHourse.dbo.Person" DataTypeServer="int" DbType="Int32" Direction="Input" ParameterName="@PID" Precision="0" ProviderType="Int" Scale="0" Size="4" SourceColumn="PID" SourceColumnNullMapping="false" SourceVersion="Current" />
                    </Parameters>
                  </DbCommand>
                </SelectCommand>
              </DbSource>
              <DbSource ConnectionRef="HireHourseConnectionString (Web.config)" DbObjectName="" DbObjectType="Unknown" GenerateMethods="Get" GenerateShortCommands="true" GeneratorGetMethodName="GetDataPage" GetMethodModifier="Public" GetMethodName="GetDataPage" QueryType="Rowset" ScalarCallRetval="System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" UseOptimisticConcurrency="true" UserGetMethodName="GetDataPage" UserSourceName="GetDataPage">
                <SelectCommand>
                  <DbCommand CommandType="Text" ModifiedByUser="true">
                    <CommandText>select * from (select *,row_number() over(order by PID) as rownum from Person) p where p.rownum&gt;=@startRowIndex and p.rownum&lt;@startRowIndex+@maximumRows;</CommandText>
                    <Parameters>
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="Int32" Direction="Input" ParameterName="@startRowIndex" Precision="0" ProviderType="Int" Scale="0" Size="0" SourceColumnNullMapping="false" SourceVersion="Current" />
                      <Parameter AllowDbNull="false" AutogeneratedName="" DataSourceName="" DbType="Int32" Direction="Input" ParameterName="@maximumRows" Precision="0" ProviderType="Int" Scale="0" Size="0" SourceColumnNullMapping="false" SourceVersion="Current" />
                    </Parameters>
                  </DbCommand>
                </SelectCommand>
              </DbSource>
            </Sources>
          </TableAdapter>
        </Tables>
        <Sources />
      </DataSource>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="DataSetPerson" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_DataSetName="DataSetPerson" msprop:Generator_UserDSName="DataSetPerson">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Person" msprop:Generator_TableClassName="PersonDataTable" msprop:Generator_TableVarName="tablePerson" msprop:Generator_TablePropName="Person" msprop:Generator_RowDeletingName="PersonRowDeleting" msprop:Generator_UserTableName="Person" msprop:Generator_RowChangingName="PersonRowChanging" msprop:Generator_RowEvHandlerName="PersonRowChangeEventHandler" msprop:Generator_RowDeletedName="PersonRowDeleted" msprop:Generator_RowEvArgName="PersonRowChangeEvent" msprop:Generator_RowChangedName="PersonRowChanged" msprop:Generator_RowClassName="PersonRow">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="PID" msdata:ReadOnly="true" msdata:AutoIncrement="true" msdata:AutoIncrementSeed="-1" msdata:AutoIncrementStep="-1" msprop:Generator_ColumnVarNameInTable="columnPID" msprop:Generator_ColumnPropNameInRow="PID" msprop:Generator_ColumnPropNameInTable="PIDColumn" msprop:Generator_UserColumnName="PID" type="xs:int" />
              <xs:element name="PName" msprop:Generator_ColumnVarNameInTable="columnPName" msprop:Generator_ColumnPropNameInRow="PName" msprop:Generator_ColumnPropNameInTable="PNameColumn" msprop:Generator_UserColumnName="PName">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="50" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="PSex" msprop:Generator_ColumnVarNameInTable="columnPSex" msprop:Generator_ColumnPropNameInRow="PSex" msprop:Generator_ColumnPropNameInTable="PSexColumn" msprop:Generator_UserColumnName="PSex">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="5" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
              <xs:element name="PAge" msprop:Generator_ColumnVarNameInTable="columnPAge" msprop:Generator_ColumnPropNameInRow="PAge" msprop:Generator_ColumnPropNameInTable="PAgeColumn" msprop:Generator_UserColumnName="PAge" type="xs:int" />
              <xs:element name="PJob" msprop:Generator_ColumnVarNameInTable="columnPJob" msprop:Generator_ColumnPropNameInRow="PJob" msprop:Generator_ColumnPropNameInTable="PJobColumn" msprop:Generator_UserColumnName="PJob">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:maxLength value="50" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//mstns:Person" />
      <xs:field xpath="mstns:PID" />
    </xs:unique>
  </xs:element>
</xs:schema>

DataSetPerson.xss:

<?xml version="1.0" encoding="utf-8"?>
<!--<autogenerated>
     This code was generated by a tool to store the dataset designer's layout information.
     Changes to this file may cause incorrect behavior and will be lost if
     the code is regenerated.
</autogenerated>-->
<DiagramLayout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ex:showrelationlabel="False" ViewPortX="0" ViewPortY="62" xmlns:ex="urn:schemas-microsoft-com:xml-msdatasource-layout-extended" xmlns="urn:schemas-microsoft-com:xml-msdatasource-layout">
  <Shapes>
    <Shape ID="DesignTable:Person" ZOrder="1" X="184" Y="265" Height="229" Width="300" AdapterExpanded="true" DataTableExpanded="true" OldAdapterHeight="0" OldDataTableHeight="24" SplitterPosition="121" />
  </Shapes>
  <Connectors />
</DiagramLayout>

Default.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewItem item = e.Item as ListViewItem;
            if (item != null)
            {
                DataRowView data = item.DataItem as DataRowView;
                if (data != null)
                {
                    var person = data.Row as DataSetPerson.PersonRow;
                    if (person != null)
                    {
                        if (person.PAge > 25)
                        {
                            HtmlTableRow rows = e.Item.FindControl("rows") as HtmlTableRow;
                            if (rows != null)
                            {
                                rows.BgColor = "red";
                            }
                        }
                    }
                }
            }
        }
    }
    protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        if (e.CommandName != "sort")
        {
            int index = e.Item.DisplayIndex;
            int pID = Convert.ToInt32(ListView1.DataKeys[index].Value);
            if (e.CommandName == "view")
            {
               Server.Transfer(ResolveClientUrl("~/view.aspx?method=view&PID=" + pID));
            }
            else if (e.CommandName == "Edit")
            {
                Server.Transfer(ResolveClientUrl("~/view.aspx?method=Edit&PID=" + pID));
            }
            else if (e.CommandName == "insert")
            {
                Server.Transfer(ResolveClientUrl("~/view.aspx?method=insert"));
            }
        }
    }
}

Default.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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">
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
        DeleteMethod="Delete" EnablePaging="True" 
        OldValuesParameterFormatString="original_{0}" 
        SelectCountMethod="fun_selectcount" SelectMethod="GetDataPage" 
        TypeName="DataSetPersonTableAdapters.PersonTableAdapter" UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_PID" Type="Int32" />
        </DeleteParameters>
       
        <UpdateParameters>
            <asp:Parameter Name="PName" Type="String" />
            <asp:Parameter Name="PSex" Type="String" />
            <asp:Parameter Name="PAge" Type="Int32" />
            <asp:Parameter Name="PJob" Type="String" />
            <asp:Parameter Name="Original_PID" Type="Int32" />
        </UpdateParameters>
    </asp:ObjectDataSource>
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="PID" 
        DataSourceID="ObjectDataSource1" 
        onitemdatabound="ListView1_ItemDataBound" 
        onitemcommand="ListView1_ItemCommand">
        <AlternatingItemTemplate>
            <tr runat="server" style="" id="rows" >
                <td>
                 <asp:Button ID="view" runat="server" CommandName="view"  Text="查看" />
                 <asp:Button ID="insert" runat="server" CommandName="insert"  Text="添加" />
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
                </td>
                <td>
                    <asp:Label ID="PIDLabel" runat="server" Text='<%# Eval("PID") %>' />
                </td>
                <td>
                    <asp:Label ID="PNameLabel" runat="server" Text='<%# Eval("PName") %>' />
                </td>
                <td>
                    <asp:Label ID="PSexLabel" runat="server" Text='<%# Eval("PSex") %>' />
                </td>
                <td>
                    <asp:Label ID="PAgeLabel" runat="server" Text='<%# Eval("PAge") %>' />
                </td>
                <td>
                    <asp:Label ID="PJobLabel" runat="server" Text='<%# Eval("PJob") %>' />
                </td>
            </tr>
        </AlternatingItemTemplate>
        <EditItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
                </td>
                <td>
                    <asp:Label ID="PIDLabel1" runat="server" Text='<%# Eval("PID") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PNameTextBox" runat="server" Text='<%# Bind("PName") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PSexTextBox" runat="server" Text='<%# Bind("PSex") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PAgeTextBox" runat="server" Text='<%# Bind("PAge") %>' />
                </td>
                <td>
                    <asp:TextBox ID="PJobTextBox" runat="server" Text='<%# Bind("PJob") %>' />
                </td>
            </tr>
        </EditItemTemplate>
        <EmptyDataTemplate>
            <table runat="server" style="">
                <tr>
                    <td>
                        未返回数据。</td>
                </tr>
            </table>
        </EmptyDataTemplate>
       
        <ItemTemplate>
            <tr style=""  runat="server" id="rows" >
                <td>
                  <asp:Button ID="view" runat="server" CommandName="view"  Text="查看" />
                                     <asp:Button ID="insert" runat="server" CommandName="insert"  Text="添加" />
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
                </td>
                <td>
                    <asp:Label ID="PIDLabel" runat="server" Text='<%# Eval("PID") %>' />
                </td>
                <td>
                    <asp:Label ID="PNameLabel" runat="server" Text='<%# Eval("PName") %>' />
                </td>
                <td>
                    <asp:Label ID="PSexLabel" runat="server" Text='<%# Eval("PSex") %>' />
                </td>
                <td>
                    <asp:Label ID="PAgeLabel" runat="server" Text='<%# Eval("PAge") %>' />
                </td>
                <td>
                    <asp:Label ID="PJobLabel" runat="server" Text='<%# Eval("PJob") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <LayoutTemplate>
            <table runat="server">
                <tr runat="server">
                    <td runat="server">
                        <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                            <tr runat="server" style="">
                                <th runat="server">
                                </th>
                                <th runat="server">
                                   <asp:Button runat="server" ID="PID" CommandArgument="PID" Text="PID" CommandName="sort" /> </th>
                                <th runat="server">
                                     <asp:Button runat="server" ID="PName" Text="PName"  CommandArgument="PName" CommandName="sort" /> </th>
                                <th runat="server">
                                    <asp:Button runat="server" ID="PSex" Text="PSex"  CommandArgument="PSex" CommandName="sort" />  </th>
                                <th runat="server">
                                    <asp:Button runat="server" ID="PAge"  Text="PAge" CommandArgument="PAge" CommandName="sort" />  </th>
                                <th runat="server">
                                    <asp:Button runat="server" ID="PJob" Text="PJob"  CommandArgument="PJob" CommandName="sort" />  </th>
                            </tr>
                            <tr ID="itemPlaceholder" runat="server">
                            </tr>
                        </table>
                    </td>
                </tr>
                <tr runat="server">
                    <td runat="server" style="">
                        <asp:DataPager ID="DataPager1" PageSize="5" runat="server">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
                                    ShowNextPageButton="False" ShowPreviousPageButton="true" />
                                <asp:NumericPagerField />
                                <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True" 
                                    ShowNextPageButton="true" ShowPreviousPageButton="False" />
                            </Fields>
                        </asp:DataPager>
                    </td>
                </tr>
            </table>
        </LayoutTemplate>
        <SelectedItemTemplate>
            <tr style="" >
                <td>
                    <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
                    <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
                </td>
                <td>
                    <asp:Label ID="PIDLabel" runat="server" Text='<%# Eval("PID") %>' />
                </td>
                <td>
                    <asp:Label ID="PNameLabel" runat="server" Text='<%# Eval("PName") %>' />
                </td>
                <td>
                    <asp:Label ID="PSexLabel" runat="server" Text='<%# Eval("PSex") %>' />
                </td>
                <td>
                    <asp:Label ID="PAgeLabel" runat="server" Text='<%# Eval("PAge") %>' />
                </td>
                <td>
                    <asp:Label ID="PJobLabel" runat="server" Text='<%# Eval("PJob") %>' />
                </td>
            </tr>
        </SelectedItemTemplate>
    </asp:ListView>
    <div>
    </div>
    </form>
</body>
</html>

view.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="view.aspx.cs" Inherits="view" %>

<!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" DataKeyNames="PID" 
            DataSourceID="ObjectDataSource1" onitemcommand="FormView1_ItemCommand">
            <EditItemTemplate>
                PID:
                <asp:Label ID="PIDLabel1" runat="server" Text='<%# Eval("PID") %>' />
                <br />
                PName:
                <asp:TextBox ID="PNameTextBox" runat="server" Text='<%# Bind("PName") %>' />
                <br />
                PSex:
                <asp:TextBox ID="PSexTextBox" runat="server" Text='<%# Bind("PSex") %>' />
                <br />
                PAge:
                <asp:TextBox ID="PAgeTextBox" runat="server" Text='<%# Bind("PAge") %>' />
                <br />
                PJob:
                <asp:TextBox ID="PJobTextBox" runat="server" Text='<%# Bind("PJob") %>' />
                <br />
                <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                    CommandName="Update" Text="更新" />
                &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="取消" />
            </EditItemTemplate>
            <InsertItemTemplate>
                PName:
                <asp:TextBox ID="PNameTextBox" runat="server" Text='<%# Bind("PName") %>' />
                <br />
                PSex:
                <asp:TextBox ID="PSexTextBox" runat="server" Text='<%# Bind("PSex") %>' />
                <br />
                PAge:
                <asp:TextBox ID="PAgeTextBox" runat="server" Text='<%# Bind("PAge") %>' />
                <br />
                PJob:
                <asp:TextBox ID="PJobTextBox" runat="server" Text='<%# Bind("PJob") %>' />
                <br />
                <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                    CommandName="Insert" Text="插入" />
                &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                    CausesValidation="False" CommandName="Cancel" Text="取消" />
            </InsertItemTemplate>
            <ItemTemplate>
                PID:
                <asp:Label ID="PIDLabel" runat="server" Text='<%# Eval("PID") %>' />
                <br />
                PName:
                <asp:Label ID="PNameLabel" runat="server" Text='<%# Bind("PName") %>' />
                <br />
                PSex:
                <asp:Label ID="PSexLabel" runat="server" Text='<%# Bind("PSex") %>' />
                <br />
                PAge:
                <asp:Label ID="PAgeLabel" runat="server" Text='<%# Bind("PAge") %>' />
                <br />
                PJob:
                <asp:Label ID="PJobLabel" runat="server" Text='<%# Bind("PJob") %>' />
                <br />
                <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                    CommandName="Edit" Text="编辑" />
                &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                    CommandName="New" Text="新建" />
            </ItemTemplate>
        </asp:FormView>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
            DeleteMethod="Delete" InsertMethod="Insert" 
            OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataByID" 
            TypeName="DataSetPersonTableAdapters.PersonTableAdapter" UpdateMethod="Update">
            <DeleteParameters>
                <asp:Parameter Name="Original_PID" Type="Int32" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="PName" Type="String" />
                <asp:Parameter Name="PSex" Type="String" />
                <asp:Parameter Name="PAge" Type="Int32" />
                <asp:Parameter Name="PJob" Type="String" />
            </InsertParameters>
            <SelectParameters>
                <asp:QueryStringParameter Name="PID" QueryStringField="PID" Type="Int32" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="PName" Type="String" />
                <asp:Parameter Name="PSex" Type="String" />
                <asp:Parameter Name="PAge" Type="Int32" />
                <asp:Parameter Name="PJob" Type="String" />
                <asp:Parameter Name="Original_PID" Type="Int32" />
            </UpdateParameters>
        </asp:ObjectDataSource>
    
    </div>
    </form>
</body>
</html>

view.aspx.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataSetPersonTableAdapters;

public partial class view : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string method = Request.QueryString["method"];
            if (method=="view")
            {
                FormView1.ChangeMode(FormViewMode.ReadOnly);
            }
            else if (method=="Edit")
            {
                FormView1.ChangeMode(FormViewMode.Edit);
            }
            else if (method=="insert")
            {
                FormView1.ChangeMode(FormViewMode.Insert);
            }
        }
    }
    protected void FormView1_ItemCommand(object sender, FormViewCommandEventArgs e)
    {
        PersonTableAdapter person = new PersonTableAdapter();
        if (e.CommandName=="Insert")
        {
             Server.Transfer(ResolveClientUrl("~/Default.aspx"));
        }
        else if (e.CommandName=="Update")
        {
            Server.Transfer(ResolveClientUrl("~/Default.aspx"));
        }
       
    }
}

web.config:

<configuration>
    <connectionStrings>
        <add name="HireHourseConnectionString" connectionString="Data Source=.;Initial Catalog=HireHourse;Persist Security Info=True;User ID=sa;Password=liuqiang" providerName="System.Data.SqlClient"/>
    </connectionStrings>
    <system.web>
        <compilation debug="true" targetFramework="4.0"/>
    </system.web>
</configuration>

 

转载于:https://www.cnblogs.com/LoVeSW/articles/3420269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值