在后台写上代码getID = "SELECT * FROM [信息表] WHERE ID="+Request.QueryString["ID"].ToString(); 然后在SOURCE那里写上SelectCommand="<%#getID%>",于是我从A页面登陆,A页面发送了一个ID值(该值为1)到B页面,我在B页面获得该ID值后,DetailsView根据ID值获得了所需的数据,但是点击编辑,修改里面的数据后,按update退出编辑状态,但值没有改变。但是如果我将SOURCE中的SelectCommand="SELECT * FROM [信息表] WHERE ID=1" 后却可以,但是这样不可以按照我登陆的ID来修改指定的记录啊。 后台用SqlDataSource1.SelectCommand="SELECT * FROM [信息表] WHERE ID=1"又不行。 哎呀说不清楚,上代码 前台:<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ziliao.aspx.cs" Inherits="zuoye.ziliao" %> <!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:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="SqlDataSource1" Height="50px" Width="125px"> <Fields> <asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" /> <asp:BoundField DataField="性别" HeaderText="性别" SortExpression="性别" /> <asp:BoundField DataField="年龄" HeaderText="年龄" SortExpression="年龄" /> <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" Visible="False" /> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" /> </Fields> </asp:DetailsView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:sjk %>" DeleteCommand="DELETE FROM [信息表] WHERE [ID] = @ID" InsertCommand="INSERT INTO [信息表] ([姓名], [性别], [年龄], [ID]) VALUES (@姓名, @性别, @年龄, @ID)" SelectCommand="<%#getID%>" //改成SELECT * FROM [信息表] WHERE ID=1就行 UpdateCommand="UPDATE [信息表] SET [姓名] = @姓名, [性别] = @性别, [年龄] = @年龄 WHERE [ID] = @ID"> <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="姓名" Type="String" /> <asp:Parameter Name="性别" Type="String" /> <asp:Parameter Name="年龄" Type="String" /> <asp:Parameter Name="ID" Type="Int32" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="姓名" Type="String" /> <asp:Parameter Name="性别" Type="String" /> <asp:Parameter Name="年龄" Type="String" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <br /> </div> </form> </body> </html> 后台:using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace zuoye { public partial class ziliao : System.Web.UI.Page { public string getID = ""; protected void Page_Load(object sender, EventArgs e) { string i = Request.QueryString["ID"].ToString(); getID = "SELECT [姓名], [性别], [年龄] FROM [信息表] WHERE ID="+Request.QueryString["ID"].ToString(); Page.DataBind(); } } } 数据库就一表:表名:信息表 姓名 性别 年龄 ID
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ziliao.aspx.cs" Inherits="zuoye.ziliao" %> <!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:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="ID" DataSourceID="SqlDataSource1" Height="50px" Width="125px"> <Fields> <asp:BoundField DataField="姓名" HeaderText="姓名" SortExpression="姓名" /> <asp:BoundField DataField="性别" HeaderText="性别" SortExpression="性别" /> <asp:BoundField DataField="年龄" HeaderText="年龄" SortExpression="年龄" /> <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" SortExpression="ID" Visible="False" /> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" /> </Fields> </asp:DetailsView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:sjk %>" DeleteCommand="DELETE FROM [信息表] WHERE [ID] = @ID" InsertCommand="INSERT INTO [信息表] ([姓名], [性别], [年龄], [ID]) VALUES (@姓名, @性别, @年龄, @ID)" SelectCommand="<%#getID%>" //改成SELECT * FROM [信息表] WHERE ID=1就行 UpdateCommand="UPDATE [信息表] SET [姓名] = @姓名, [性别] = @性别, [年龄] = @年龄 WHERE [ID] = @ID"> <DeleteParameters> <asp:Parameter Name="ID" Type="Int32" /> </DeleteParameters> <InsertParameters> <asp:Parameter Name="姓名" Type="String" /> <asp:Parameter Name="性别" Type="String" /> <asp:Parameter Name="年龄" Type="String" /> <asp:Parameter Name="ID" Type="Int32" /> </InsertParameters> <UpdateParameters> <asp:Parameter Name="姓名" Type="String" /> <asp:Parameter Name="性别" Type="String" /> <asp:Parameter Name="年龄" Type="String" /> <asp:Parameter Name="ID" Type="Int32" /> </UpdateParameters> </asp:SqlDataSource> <br /> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace zuoye { public partial class ziliao : System.Web.UI.Page { public string getID = ""; protected void Page_Load(object sender, EventArgs e) { string i = Request.QueryString["ID"].ToString(); getID = "SELECT [姓名], [性别], [年龄] FROM [信息表] WHERE ID="+Request.QueryString["ID"].ToString(); Page.DataBind(); } } }
补充:.NET技术 , ASP.NET
上一个:不同读卡器读同一个ID卡数据不同下一个:怎样利用dropdownlist insert DateTime 到access 中? ASP.NET C#