利用GridView显示主细表并一次编辑明细表所有数据的例子

前几天写了一个利用GridView显示主细表的例子,有网友询问如何一次编辑明细表数据所有数据?其实,如果对ASP.NET或者HTML比较熟悉的话,这种批量编辑数据的功能很容易实现。下面的例子使用隐藏表单域记录数据的标识id,对每个输入框的名字附加id信息,这样的话,数据对应起来比较简单。

例子中使用的数据库来自《ASP.NET 2.0 应用开发技术》一书。换成其它数据库也类似,看懂代码,怎么换就无妨了。

aspx:

<%@ Page Language="C#" ValidateRequest="false" AutoEventWireup="true" EnableViewState="true"<br />CodeFile="Default2.aspx.cs" Inherits="Default2" %><br /><!doctype html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /> <head id="Head1" runat="server"><br /> <title>利用GridView显示主细表并一次编辑明细表所有数据的例子</title><br /> <style type="text/css"><br /> td,div,a {font-size:12px}<br /> </style><br /> <script type="text/javascript"> //<![cdata[ function ShowHidden(sid,ev) { ev = ev || window.event; var target = ev.target || ev.srcElement; var oDiv = document.getElementById("div" + sid); oDiv.style.display = oDiv.style.display == "none"?"block":"none"; target.innerHTML = oDiv.style.display == "none"?"显示":"隐藏"; } //]]> </script><br /> </head><br /> <body><br /> <form id="form1" runat="server"><br /> <asp:gridview ID="MasterGridView" runat="server" AutoGenerateColumns="false" Width="780px"<br /> BorderWidth="1" OnRowDataBound="MasterGridView_RowDataBound" DataKeyNames="id"<br /> ShowHeader="false"><br /> <columns><br /> <asp:templatefield><br /> <itemtemplate><br /> <div style="width: 100%; padding: 2px; font-weight: bold; background-color: #DEDEDE;<br /> float: left"><br /> <span style="float: left">栏目名称:<%#eval("title") %></span><span style="float: right;<br /> color: Red; cursor: pointer" onclick="ShowHidden('<%#eval("id") %>',event)">隐藏</span></div><br /> <div style="background-color: #FFF; padding-left: 60px; clear: both" id="div<%#eval("id") %>"><br /> <asp:gridview ID="DetailGridView" runat="server" AutoGenerateColumns="false" ShowHeader="true"<br /> HorizontalAlign="left" DataKeyNames="id" OnRowCommand="DetailGridView_RowCommand"<br /> OnRowDeleting="DetailGridView_RowDeleting" Width="720px"><br /> <headerstyle BackColor="#9999FF" /><br /> <columns><br /> <asp:templatefield HeaderText="文章名称" HeaderStyle-Width="540px"><br /> <itemtemplate><br /> <input name="guid" value="<%#eval("id") %>" type="hidden" /><br /> <input name="Title<%#eval("id") %>" value="<%#eval("title") %>" style="width: 100%;border:1px solid gray" /><br /> </itemtemplate><br /> </asp:templatefield><br /> <asp:templatefield HeaderText="发布日期" HeaderStyle-Width="100px" ItemStyle-HorizontalAlign="Center"><br /> <itemtemplate><br /> <input name="Date<%#eval("id") %>" value="<%# ((DateTime)Eval("CreateDate")).ToString("yyyy-MM-dd") %>" style="border:1px solid gray"/><br /> </itemtemplate><br /> </asp:templatefield><br /> <asp:commandfield DeleteText="删除" ShowDeleteButton="true" ButtonType="Button" HeaderStyle-Width="40px" /><br /> </columns><br /> </asp:gridview><br /> </div><br /> </itemtemplate><br /> </asp:templatefield><br /> </columns><br /> </asp:gridview><br /> <asp:button ID="Button1" runat="server" OnClick="Button1_Click" Text="编辑所有数据" /><br /> </form><br /> </body><br /></html><br />

C#:

using System;<br />using System.Data;<br />using System.Configuration;<br />using System.Collections;<br />using System.Web;<br />using System.Web.Security;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.Data.OleDb;<br />using System.Web.UI.HtmlControls;<br />public partial class Default2 : System.Web.UI.Page<br />{<br />string ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\aspxWeb.mdb;Persist Security Info=True";<br />OleDbConnection cn1;<br />protected void Page_Load( object sender, EventArgs e )<br />{<br />if (!Page.IsPostBack)<br />{<br />OleDbConnection cn = new OleDbConnection(ConnectionString);<br />cn.Open();<br />cn1 = new OleDbConnection(ConnectionString);<br />cn1.Open();<br />OleDbCommand cmd = new OleDbCommand("select * from [Subject]", cn);<br />OleDbDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);<br />MasterGridView.DataSource = dr;<br />MasterGridView.DataBind();<br />dr.Close();<br />cmd.Dispose();<br />cn.Dispose();<br />cn1.Dispose();<br />cn = cn1 = null;<br />}<br />}<br />protected void MasterGridView_RowDataBound( object sender, GridViewRowEventArgs e )<br />{<br />if (e.Row.RowType == DataControlRowType.DataRow)<br />{<br />GridView oGridView = (GridView)e.Row.FindControl("DetailGridView");<br />if (oGridView != null)<br />{<br />OleDbCommand cmd = new OleDbCommand("select top 10 * from Document Where pid = " + MasterGridView.DataKeys[e.Row.RowIndex].Value, cn1);<br />OleDbDataReader dr1 = cmd.ExecuteReader();<br />oGridView.DataSource = dr1;<br />oGridView.DataBind();<br />dr1.Close();<br />cmd.Dispose();<br />}<br />}<br />}<br />protected void DetailGridView_RowDeleting( object sender, GridViewDeleteEventArgs e )<br />{<br />GridView a = (GridView)sender;<br />Response.Write("您要删除的记录是:<font color='red'>" + a.DataKeys[e.RowIndex].Value.ToString() + "</font>    TODO:执行删除动作");<br />// TODO:执行删除动作<br />}<br />protected void DetailGridView_RowCommand( object sender, GridViewCommandEventArgs e )<br />{<br />}<br />protected void Button1_Click( object sender, EventArgs e )<br />{<br />string Guids = Request.Form["guid"];<br />if (Guids == null || Guids.Equals(string.Empty))<br />{<br />Response.Write("没有数据可以修改。");<br />return;<br />}<br />string sql;<br />OleDbCommand cmd;<br />OleDbConnection cn = new OleDbConnection();<br />cn.ConnectionString = ConnectionString;<br />cn.Open();<br />string[] ArrGuid = Guids.Split(',');<br />for (int i = 0 ; i <arrguid.length ; i++)<br />{<br />sql = "UPDATE Document Set Title = @Title,CreateDate = @CreateDate Where id = @id";<br />cmd = new OleDbCommand(sql, cn);<br />cmd.Parameters.AddWithValue("@Title",Request.Form["Title" + ArrGuid[i].Trim()]);<br />cmd.Parameters.AddWithValue("@CreateDate", Request.Form["Date" + ArrGuid[i].Trim()]);<br />cmd.Parameters.AddWithValue("@id",ArrGuid[i].Trim());<br />cmd.ExecuteNonQuery();<br />Response.Write("<br /><li>更新:id= " + ArrGuid[i] + " "+ Request.Form["Title" + ArrGuid[i].Trim()]);<br />}<br />cn.Close();<br />cn.Dispose();<br />}<br />}<br />

你可能感兴趣的:(GridView)