GridView级联显示 与Nettier的合成应用Sample[较乱,附代码]



这是一个财务统驭科目ChartOfAccount 和子帐科目间的数据操作Sample
  1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChartOfAccount.aspx.cs" Inherits="WebModules_CommonMasterData_ChartOfAccount" %>
  2
  3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4
  5<html xmlns="http://www.w3.org/1999/xhtml" >
  6<head runat="server">
  7    <title>无标题页</title>
  8</head>
  9<body>
 10    <form id="form1" runat="server">
 11    <div>
 12        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="ChartID"
 13            DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" OnRowCommand="GridView1_RowCommand" ShowFooter="True" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Width="855px" AllowPaging="True" OnRowEditing="GridView1_RowEditing">
 14            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
 15            <Columns>
 16                <asp:TemplateField ShowHeader="False">
 17                    <ItemTemplate>
 18                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select"
 19                            Text="选中"></asp:LinkButton>
 20                    </ItemTemplate>
 21                </asp:TemplateField>
 22                <asp:TemplateField HeaderText="统驭科目ID" SortExpression="ChartID">
 23                    <EditItemTemplate>
 24                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("ChartID") %>'></asp:Label>
 25                    </EditItemTemplate>
 26                    <FooterTemplate>
 27                        <asp:Button ID="btnCreate" runat="server" CommandName="Insert" Text="新建" />
 28                    </FooterTemplate>
 29                    <ItemTemplate>
 30                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("ChartID") %>'></asp:Label>
 31                    </ItemTemplate>
 32                </asp:TemplateField>
 33                <asp:TemplateField HeaderText="统驭科目名" SortExpression="ChartOfAccountName">
 34                    <EditItemTemplate>
 35                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ChartOfAccountName") %>'></asp:TextBox>
 36                    </EditItemTemplate>
 37                    <FooterTemplate>
 38                        <asp:TextBox ID="newChartOfAccountName" runat="server"></asp:TextBox>
 39                    </FooterTemplate>
 40                    <ItemTemplate>
 41                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ChartOfAccountName") %>'></asp:Label>
 42                    </ItemTemplate>
 43                </asp:TemplateField>
 44                <asp:TemplateField HeaderText="注释" SortExpression="Description">
 45                    <EditItemTemplate>
 46                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Description") %>'></asp:TextBox>
 47                    </EditItemTemplate>
 48                    <FooterTemplate>
 49                        <asp:TextBox ID="newDescription" runat="server"></asp:TextBox>
 50                    </FooterTemplate>
 51                    <ItemTemplate>
 52                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
 53                    </ItemTemplate>
 54                </asp:TemplateField>
 55                <asp:TemplateField HeaderText="统驭科目编码" SortExpression="ChartCode">
 56                    <EditItemTemplate>
 57                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ChartCode") %>'></asp:TextBox>
 58                    </EditItemTemplate>
 59                    <FooterTemplate>
 60                        <asp:TextBox ID="txtChartOfCode" runat="server"></asp:TextBox>
 61                    </FooterTemplate>
 62                    <ItemTemplate>
 63                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ChartCode") %>'></asp:Label>
 64                    </ItemTemplate>
 65                </asp:TemplateField>
 66                <asp:BoundField DataField="CreateDate" HeaderText="创建日期" SortExpression="CreateDate" />
 67                <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
 68                <asp:TemplateField HeaderText="下属科目" Visible="False">
 69                    <EditItemTemplate>
 70                        &nbsp;<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
 71                            BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
 72                            CellPadding="3" DataKeyNames="LengendID" DataSourceID="SqlDataSource3" GridLines="Horizontal"
 73                            Height="50px" Width="125px">
 74                            <FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
 75                            <EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7" />
 76                            <RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
 77                            <PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
 78                            <Fields>
 79                                <asp:BoundField DataField="LegendCode" HeaderText="LegendCode" SortExpression="LegendCode" />
 80                                <asp:BoundField DataField="LegendName" HeaderText="LegendName" SortExpression="LegendName" />
 81                                <asp:BoundField DataField="LengendID" HeaderText="LengendID" InsertVisible="False"
 82                                    ReadOnly="True" SortExpression="LengendID" />
 83                                <asp:BoundField DataField="ChartID" HeaderText="ChartID" SortExpression="ChartID" />
 84                                <asp:BoundField DataField="ChartCode" HeaderText="ChartCode" SortExpression="ChartCode" />
 85                                <asp:BoundField DataField="status" HeaderText="status" SortExpression="status" />
 86                            </Fields>
 87                            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
 88                            <AlternatingRowStyle BackColor="#F7F7F7" />
 89                        </asp:DetailsView>
 90                        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:BLUEBEANSConnectionString %>"
 91                            SelectCommand="SELECT [LegendCode], [LegendName], [LengendID], [ChartID], [ChartCode], [status] FROM [FinanceLegend]">
 92                        </asp:SqlDataSource>
 93                    </EditItemTemplate>
 94                </asp:TemplateField>
 95                <asp:CommandField ShowEditButton="True" />
 96            </Columns>
 97            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
 98            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
 99            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
100            <AlternatingRowStyle BackColor="White" />
101            <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
102            <EditRowStyle BackColor="#2461BF" />
103        </asp:GridView>
104        &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;
105        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BLUEBEANSConnectionString %>"
106            SelectCommand="SELECT [ChartID], [ChartOfAccountName], [ProfitCenterName], [status], [Description], [ProfitCenterID], [EndCode], [BeginCode], [ChartCode], [CreateDate] FROM [FinanceCharOfAccount]">
107        </asp:SqlDataSource>
108        &nbsp;
109        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:BLUEBEANSConnectionString %>"
110            SelectCommand="SELECT [LegendCode], [LegendName], [LengendID], [ChartID], [ChartCode], [status] FROM [FinanceLegend]">
111        </asp:SqlDataSource>
112        <asp:Label ID="Label6" runat="server" Text="子科目代码"></asp:Label>
113        <asp:TextBox ID="txtLengendCode" runat="server"></asp:TextBox>
114        <asp:Label ID="Label5" runat="server" Text="子科目名" Width="57px"></asp:Label>
115        <asp:TextBox ID="txtLengendName" runat="server"></asp:TextBox>
116        <asp:Button ID="btnCreateLengend" runat="server" OnClick="btnCreateLengend_Click"
117            Text="创建子科目" />
118        <asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False"
119            DataSourceID="SqlDataSource4" PageSize="5" OnRowCommand="GridView2_RowCommand" HorizontalAlign="Left">
120            <Columns>
121                <asp:TemplateField HeaderText="子科目代码" SortExpression="LegendCode">
122                    <EditItemTemplate>
123                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LegendCode") %>'></asp:TextBox>
124                    </EditItemTemplate>
125                    <FooterTemplate>
126                        <asp:TextBox ID="newLengendCode" runat="server"></asp:TextBox>
127                    </FooterTemplate>
128                    <ItemTemplate>
129                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("LegendCode") %>'></asp:Label>
130                    </ItemTemplate>
131                </asp:TemplateField>
132                <asp:TemplateField HeaderText="科目名称" SortExpression="LegendName">
133                    <EditItemTemplate>
134                        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("LegendName") %>'></asp:TextBox>
135                    </EditItemTemplate>
136                    <FooterTemplate>
137                        <asp:TextBox ID="newLengendName" runat="server"></asp:TextBox>
138                    </FooterTemplate>
139                    <ItemTemplate>
140                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("LegendName") %>'></asp:Label>
141                    </ItemTemplate>
142                </asp:TemplateField>
143                <asp:BoundField DataField="ChartCode" HeaderText="统驭科目代码" SortExpression="ChartCode" />
144                <asp:BoundField DataField="ChartID" HeaderText="统驭科目ID" SortExpression="ChartID" />
145                <asp:TemplateField HeaderText="状态" SortExpression="status">
146                    <EditItemTemplate>
147                        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("status") %>'></asp:TextBox>
148                    </EditItemTemplate>
149                    <FooterTemplate>
150                        <asp:Button ID="btnCreateLengend" runat="server" CommandName="Insert" Text="创建子科目" />
151                    </FooterTemplate>
152                    <ItemTemplate>
153                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("status") %>'></asp:Label>
154                    </ItemTemplate>
155                </asp:TemplateField>
156            </Columns>
157            <RowStyle HorizontalAlign="Center" />
158        </asp:GridView>
159        &nbsp; &nbsp;
160        <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:BLUEBEANSConnectionString %>"
161            SelectCommand="SELECT [LegendCode], [LegendName], [ChartCode], [ChartID], [status] FROM [FinanceLegend] WHERE ([ChartID] = @ChartID)">
162            <SelectParameters>
163                <asp:ControlParameter ControlID="GridView1" Name="ChartID" PropertyName="SelectedValue"
164                    Type="Int32" />
165            </SelectParameters>
166        </asp:SqlDataSource>
167        <asp:DetailsView ID="DetailsView2" runat="server" Height="50px" Width="125px">
168        </asp:DetailsView>
169    
170    </div>
171    </form>
172</body>
173</html>
174

后台数据:
  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11using BlueBeans.Data;
 12using BlueBeans.Entities;
 13
 14public partial class WebModules_CommonMasterData_ChartOfAccount : System.Web.UI.Page
 15{
 16    int _chartID = 0;
 17    string _chartCode = string.Empty;
 18    protected void Page_Load(object sender, EventArgs e)
 19    {
 20
 21    }

 22    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 23    {
 24        if (e.CommandName.Equals("Insert"))
 25        {
 26            TextBox newChartOfAccountName = (TextBox)this.GridView1.FooterRow.FindControl("newChartOfAccountName");
 27            TextBox newDescription = (TextBox)this.GridView1.FooterRow.FindControl("newDescription");
 28            TextBox txtChartOfCode = (TextBox)this.GridView1.FooterRow.FindControl("txtChartOfCode");
 29
 30            FinanceCharOfAccount coa = new FinanceCharOfAccount();
 31            coa.ChartCode = txtChartOfCode.Text;
 32            coa.Description = newDescription.Text;
 33            coa.ChartOfAccountName = newChartOfAccountName.Text;
 34            coa.CreateDate = DateTime.Now;
 35            coa.Status = 0;
 36
 37            try
 38            {
 39                DataRepository.FinanceCharOfAccountProvider.Save(coa);
 40            }

 41            catch (Exception ex)
 42            {
 43                Response.Write(ex.Message);
 44            }

 45
 46            this.GridView1.DataBind();           
 47
 48        }

 49
 50    }

 51
 52    private void InsertLengendCode()
 53    {
 54        //DataTable dataTable = new DataTable();
 55        //DataColumn col0 = dataTable.Columns.Add("ChartID", typeof(System.Int32));
 56        //col0.AllowDBNull = false;
 57        //DataColumn col1 = dataTable.Columns.Add("ChartOfAccountName", typeof(System.String));
 58        //col1.AllowDBNull = false;
 59        //DataColumn col2 = dataTable.Columns.Add("ProfitCenterName", typeof(System.String));
 60        //col2.AllowDBNull = true;
 61        //DataColumn col3 = dataTable.Columns.Add("status", typeof(System.Int32));
 62        //col3.AllowDBNull = true;
 63        //DataColumn col4 = dataTable.Columns.Add("Description", typeof(System.String));
 64        //col4.AllowDBNull = true;
 65        //DataColumn col5 = dataTable.Columns.Add("CreateDate", typeof(System.DateTime));
 66        //col5.AllowDBNull = true;
 67        //DataColumn col6 = dataTable.Columns.Add("ChartCode", typeof(System.String));
 68        //col6.AllowDBNull = false;
 69        //DataColumn col7 = dataTable.Columns.Add("BeginCode", typeof(System.String));
 70        //col7.AllowDBNull = true;
 71        //DataColumn col8 = dataTable.Columns.Add("EndCode", typeof(System.String));
 72        //col8.AllowDBNull = true;
 73        //DataColumn col9 = dataTable.Columns.Add("ProfitCenterID", typeof(System.Int32));
 74        //col9.AllowDBNull = true;
 75
 76        //bulkCopy.ColumnMappings.Add("ChartID", "ChartID");
 77        //bulkCopy.ColumnMappings.Add("ChartOfAccountName", "ChartOfAccountName");
 78        //bulkCopy.ColumnMappings.Add("ProfitCenterName", "ProfitCenterName");
 79        //bulkCopy.ColumnMappings.Add("status", "status");
 80        //bulkCopy.ColumnMappings.Add("Description", "Description");
 81        //bulkCopy.ColumnMappings.Add("CreateDate", "CreateDate");
 82        //bulkCopy.ColumnMappings.Add("ChartCode", "ChartCode");
 83        //bulkCopy.ColumnMappings.Add("BeginCode", "BeginCode");
 84        //bulkCopy.ColumnMappings.Add("EndCode", "EndCode");
 85        //bulkCopy.ColumnMappings.Add("ProfitCenterID", "ProfitCenterID");
 86
 87        //foreach (BlueBeans.Entities.FinanceCharOfAccount entity in entities)
 88        //{
 89        //    if (entity.EntityState != EntityState.Added)
 90        //        continue;
 91
 92        //    DataRow row = dataTable.NewRow();
 93
 94        //    row["ChartID"] = entity.ChartID;
 95
 96
 97        //    row["ChartOfAccountName"] = entity.ChartOfAccountName;
 98
 99
100        //    row["ProfitCenterName"] = entity.ProfitCenterName;
101
102
103        //    row["status"] = entity.Status.HasValue ? (object)entity.Status : System.DBNull.Value;
104
105
106        //    row["Description"] = entity.Description;
107
108
109        //    row["CreateDate"] = entity.CreateDate.HasValue ? (object)entity.CreateDate : System.DBNull.Value;
110
111
112        //    row["ChartCode"] = entity.ChartCode;
113
114
115        //    row["BeginCode"] = entity.BeginCode;
116
117
118        //    row["EndCode"] = entity.EndCode;
119
120
121        //    row["ProfitCenterID"] = entity.ProfitCenterID.HasValue ? (object)entity.ProfitCenterID : System.DBNull.Value;
122
123
124        //    dataTable.Rows.Add(row);
125            
126        // }
127      
128        
129        }

130    /// <summary>
131    /// 获取主表数据
132    /// </summary>
133    /// <param name="sender"></param>
134    /// <param name="e"></param>

135    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
136    {
137        //Response.Write(this.GridView1.SelectedValue.ToString());
138        this.ViewState["ChartID"= this.GridView1.SelectedValue.ToString();
139        this._chartID = int.Parse(this.GridView1.SelectedValue.ToString());
140        //this._chartCode = this.GridView1.SelectedRow.Cells[3];
141    }

142    /// <summary>
143    /// Insert从表数据
144    /// </summary>
145    /// <param name="sender"></param>
146    /// <param name="e"></param>

147    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
148    {
149        //Response.Write(this.GridView1.SelectedValue.ToString());
150        if (e.CommandName.Equals("Insert"))
151        {
152            TextBox newLengendName = (TextBox)this.GridView2.FooterRow.FindControl("newLengendName");
153            TextBox newLengendCode = (TextBox)this.GridView2.FooterRow.FindControl("newLengendCode");
154
155
156            FinanceLegend lengend = new FinanceLegend();
157            lengend.ChartID =int.Parse( this.GridView1.SelectedValue.ToString());
158
159            FinanceCharOfAccount coa = DataRepository.FinanceCharOfAccountProvider.GetByChartID(int.Parse(this.GridView1.SelectedValue.ToString()));
160            lengend.ChartCode = coa.ChartCode;
161
162            lengend.LegendCode = newLengendCode.Text;
163            lengend.LegendName = newLengendName.Text;
164            
165            lengend.Status = 0;
166
167            try
168            {
169                DataRepository.FinanceLegendProvider.Save(lengend);
170            }

171            catch (Exception ex)
172            {
173                Response.Write(ex.Message);
174            }

175
176            this.GridView2.DataBind();
177
178        }

179    }

180    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
181    {
182
183    }

184    protected void btnCreateLengend_Click(object sender, EventArgs e)
185    {
186
187
188
189            FinanceLegend lengend = new FinanceLegend();
190            lengend.ChartID = int.Parse(this.GridView1.SelectedValue.ToString());
191            //FinanceCharOfAccount coa = new FinanceCharOfAccount();
192            //coa = DataRepository.FinanceCharOfAccountProvider.GetByChartID(lengend.ChartID);
193            FinanceCharOfAccount coa = DataRepository.FinanceCharOfAccountProvider.GetByChartID(int.Parse(this.GridView1.SelectedValue.ToString()));
194            lengend.ChartCode = coa.ChartCode;
195            lengend.LegendCode = this.txtLengendCode.Text;
196            lengend.LegendName = this.txtLengendName.Text;
197            
198            lengend.Status = 0;
199
200            try
201            {
202                DataRepository.FinanceLegendProvider.Save(lengend);
203            }

204            catch (Exception ex)
205            {
206                Response.Write(ex.Message);
207            }

208        //Clear Text
209            this.txtLengendCode.Text = string.Empty;
210            this.txtLengendName.Text = string.Empty;
211
212            this.GridView2.DataBind();
213
214        
215
216    }

217}

218

显示效果:


posted @ 2007-08-02 18:03  Slashout  阅读(734)  评论(0编辑  收藏  举报