11.单独页面编辑
- ListView的在位编辑只适合字段比较少、比较简单的场合,复杂数据的编辑、插入、查看等要在单独页面中才可
- 创建一个单独的页面Edit***.aspx,然后在ListView页面中的编辑模板中放一个编辑的超连接,向Edit***.aspx传递?id=1&action=edit.页面顶端增加一个Edit***.aspx?action=addnew的超连接。
- 使用FormView控件进行单条数据的编辑,在Page_Load中判断action,然后使用FormView1.ChangeMode方法切换FormView的模式。
- 强类型DataSet中增加一个GetDataByID方法,在ObjectDataSource中选择这个方法为Select参数,参数源为QueryString,QueryStringField为id.
- 在元素插入、修改完成(Inserted\Updated事件)后重定向到列表页面。
例子:
有两个网页,一个是简单显示数据表中的记录,另一个是单独的编辑页面,在编辑或新增后重新回到第一个浏览页面。 要在数据集中加入一个GetDataByID的方法,主要用到单独页面的编辑操作(传ID)。
第一个页面:
它的源码 为:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ListItem.aspx.cs" Inherits="行命令设置.单独页编辑页面.ListItem" %> <!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:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" TypeName="行命令设置.DAL.DataSetUserTableAdapters.T_UserTableAdapter" UpdateMethod="Update"> <DeleteParameters> <asp:Parameter Name="Original_id" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="age" Type="Int32" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="age" Type="Int32" /> <asp:Parameter Name="Original_id" Type="Int32" /> </UpdateParameters> </asp:ObjectDataSource> <asp:Button ID="Button1" runat="server" Text="新增" onclick="Button1_Click" /> <asp:ListView ID="ListView1" runat="server" DataKeyNames="id" DataSourceID="ObjectDataSource1" InsertItemPosition="None"> <EmptyDataTemplate> <table runat="server" style=""> <tr> <td> 未返回数据。</td> </tr> </table> </EmptyDataTemplate> <ItemTemplate> <tr style=""> <td> <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" /> <a href='EditItem.aspx?action=view&id=<%#Eval("id") %>'>查看</a> <a href='EditItem.aspx?action=edit&id=<%#Eval("id") %>'>编辑</a> </td> <td> <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' /> </td> <td> <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /> </td> <td> <asp:Label ID="ageLabel" runat="server" Text='<%# Eval("age") %>' /> </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"> id</th> <th runat="server"> name</th> <th runat="server"> age</th> </tr> <tr ID="itemPlaceholder" runat="server"> </tr> </table> </td> </tr> <tr runat="server"> <td runat="server" style=""> <asp:DataPager ID="DataPager1" runat="server" PageSize="20"> <Fields> <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" /> </Fields> </asp:DataPager> </td> </tr> </table> </LayoutTemplate> </asp:ListView> </div> </form> </body> </html>
第二个页面的源码(设定它的数据来源方法为GetDataById):
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditItem.aspx.cs" Inherits="行命令设置.单独页编辑页面.EditItem" %> <!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> <link href="../css/jquery-ui-1.10.3.custom.css" rel="stylesheet" type="text/css" /> <script src="../js/jquery-1.10.1.js" type="text/javascript"></script> <script src="../js/jquery-ui-1.10.3.custom.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("#TextBox1").datepicker(); }); </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete" InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetDataById" TypeName="行命令设置.DAL.DataSetUserTableAdapters.T_UserTableAdapter" UpdateMethod="Update"> <DeleteParameters> <asp:Parameter Name="Original_id" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="age" Type="Int32" /> </InsertParameters> <SelectParameters> <asp:QueryStringParameter Name="id" QueryStringField="id" Type="Int32" /> </SelectParameters> <UpdateParameters> <asp:Parameter Name="name" Type="String" /> <asp:Parameter Name="age" Type="Int32" /> <asp:Parameter Name="Original_id" Type="Int32" /> </UpdateParameters> </asp:ObjectDataSource> <asp:FormView ID="FormView1" runat="server" DataKeyNames="id" DataSourceID="ObjectDataSource1" onpageindexchanging="FormView1_PageIndexChanging" oniteminserted="FormView1_ItemInserted" onitemupdated="FormView1_ItemUpdated"> <EditItemTemplate> id: <asp:Label ID="idLabel1" runat="server" Text='<%# Eval("id") %>' /> <br /> name: <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' /> <br /> age: <asp:TextBox ID="ageTextBox" runat="server" Text='<%# Bind("age") %>' /> <br /> <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="更新" /> <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" /> </EditItemTemplate> <InsertItemTemplate> name: <asp:TextBox ID="nameTextBox" runat="server" Text='<%# Bind("name") %>' /> <br /> age: <asp:TextBox ID="ageTextBox" runat="server" Text='<%# Bind("age") %>' /> <br /> <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert" Text="插入" /> <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="取消" /> </InsertItemTemplate> <ItemTemplate> id: <asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' /> <br /> name: <asp:Label ID="nameLabel" runat="server" Text='<%# Bind("name") %>' /> <br /> age: <asp:Label ID="ageLabel" runat="server" Text='<%# Bind("age") %>' /> <br /> </ItemTemplate> </asp:FormView> <br /> <asp:Button ID="btnReturn" runat="server" Text="返回页面" onclick="btnReturn_Click" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </div> </form> </body> </html>
单独编辑页面的cs源码:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace 行命令设置.单独页编辑页面 { public partial class EditItem : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Request["action"] == "add") { FormView1.ChangeMode(FormViewMode.Insert); } else if (Request["action"] == "edit") { FormView1.ChangeMode(FormViewMode.Edit); } else if (Request["action"] == "view") { FormView1.ChangeMode(FormViewMode.ReadOnly); } } } protected void FormView1_PageIndexChanging(object sender, FormViewPageEventArgs e) { } protected void btnReturn_Click(object sender, EventArgs e) { Response.Redirect("~/单独页编辑页面/ListItem.aspx"); } protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e) { Response.Redirect("~/单独页编辑页面/ListItem.aspx"); } protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e) { Response.Redirect("~/单独页编辑页面/ListItem.aspx"); } } }