NHibernate+GridView构建添加,删除,修改程序

我们在写项目的时候经常会遇到要做一些比较简单的基础信息,这些东西如果也用表单的话就显得比较麻烦,这里介绍一下用GridView方法的简单实例,示例仅供参考.

1. 数据模型
 
2. 页面代码
 
 1 <%@ Control Language="C#" AutoEventWireup="True" CodeBehind="RolesList.ascx.cs" Inherits="ZhuJi.UUMS.WebUI.RolesList" %>
 2 <asp:GridView ID="gvList" runat="server" AutoGenerateColumns="False" OnRowCancelingEdit="gvList_RowCancelingEdit"
 3  OnRowDeleting="gvList_RowDeleting" OnRowEditing="gvList_RowEditing" OnRowUpdating="gvList_RowUpdating"
 4  Width="100%" ShowFooter="True">
 5  <Columns>
 6   <asp:BoundField DataField="Id" HeaderText="标识" ReadOnly="True">
 7    <ItemStyle HorizontalAlign="Center" Width="48px" />
 8   </asp:BoundField>
 9   <asp:TemplateField HeaderText="角色名称">
10    <EditItemTemplate>
11     <asp:TextBox ID="txtRoleName" runat="server" Text='<%# Bind("RoleName") %>' Width="300px"></asp:TextBox>
12    </EditItemTemplate>
13    <ItemTemplate>
14     <asp:Label ID="lblRoleName" runat="server" Text='<%# Bind("RoleName") %>'></asp:Label>
15    </ItemTemplate>
16    <FooterTemplate>
17    <asp:TextBox ID="txtRoleName" runat="server" Width="300px"></asp:TextBox>
18    </FooterTemplate>
19   </asp:TemplateField>
20   <asp:TemplateField HeaderText="排序">
21    <EditItemTemplate>
22     <asp:TextBox ID="txtOrderBy" runat="server" Text='<%# Bind("OrderBy") %>' Width="100%"></asp:TextBox>
23    </EditItemTemplate>
24    <ItemStyle HorizontalAlign="Center" Width="48px" />
25    <ItemTemplate>
26     <asp:Label ID="lblOrderBy" runat="server" Text='<%# Bind("OrderBy") %>'></asp:Label>
27    </ItemTemplate>
28    <FooterStyle HorizontalAlign="Center" Width="48px" />
29    <FooterTemplate>
30    <asp:TextBox ID="txtOrderBy" runat="server" Width="48px"></asp:TextBox>
31    </FooterTemplate>
32   </asp:TemplateField>
33   <asp:TemplateField ShowHeader="False">
34    <EditItemTemplate>
35     <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
36      Text="更新"></asp:LinkButton>
37     <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
38      Text="取消"></asp:LinkButton>
39     <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"
40      Text="删除"></asp:LinkButton>
41    </EditItemTemplate>
42    <ItemStyle HorizontalAlign="Center" Width="100px" />
43    <ItemTemplate>
44     <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
45      Text="编辑"></asp:LinkButton>
46     <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False" CommandName="Delete"
47      Text="删除"></asp:LinkButton>
48    </ItemTemplate>
49    <FooterStyle HorizontalAlign="Center" Width="100px" />
50    <FooterTemplate>
51    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" OnClick="gvList_RowInserting"
52      Text="新建"></asp:LinkButton>
53    </FooterTemplate>
54   </asp:TemplateField>
55  </Columns>
56  <HeaderStyle Height="30px" />
57 </asp:GridView>
3. CS代码
 
  1 using System;
  2 using System.Data;
  3 using System.Configuration;
  4 using System.Collections;
  5 using System.Web;
  6 using System.Web.Security;
  7 using System.Web.UI;
  8 using System.Web.UI.WebControls;
  9 using System.Web.UI.WebControls.WebParts;
 10 using System.Web.UI.HtmlControls;
 11 
 12 namespace ZhuJi.UUMS.WebUI
 13 {
 14     public partial class RolesList : ZhuJi.Portal.WebUI.BaseWebControl
 15     {
 16         protected void Page_Load(object sender, EventArgs e)
 17         {
 18             if (!Page.IsPostBack)
 19             {
 20                 BindGrid();
 21             }
 22         }
 23 
 24         /// <summary>
 25         /// 初始化查询列表
 26         /// </summary>
 27         public void BindGrid()
 28         {
 29             try
 30             {
 31                 ZhuJi.UUMS.Service.Roles roles = ZhuJi.AOP.Operator.WrapInterface(typeof(ZhuJi.UUMS.Service.Roles)) as ZhuJi.UUMS.Service.Roles;
 32                 gvList.DataSource = roles.GetObjects(base.Where, base.OrderNo);
 33                 gvList.DataBind();
 34             }
 35             catch (Exception ex)
 36             {
 37                 ShowMessage(ex);
 38             }
 39         }
 40 
 41         protected void gvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
 42         {
 43             try
 44             {
 45                 ZhuJi.UUMS.Domain.Roles domainRoles = new ZhuJi.UUMS.Domain.Roles();
 46 
 47                 domainRoles.Id = int.Parse(gvList.Rows[e.RowIndex].Cells[0].Text);
 48 
 49                 ZhuJi.UUMS.Service.Roles roles = ZhuJi.AOP.Operator.WrapInterface(typeof(ZhuJi.UUMS.Service.Roles)) as ZhuJi.UUMS.Service.Roles;
 50                 roles.Delete(domainRoles);
 51 
 52                 gvList.EditIndex = -1;
 53                 BindGrid();
 54             }
 55             catch (Exception ex)
 56             {
 57                 throw ex;
 58             }
 59         }
 60 
 61         protected void gvList_RowEditing(object sender, GridViewEditEventArgs e)
 62         {
 63             gvList.EditIndex = e.NewEditIndex;
 64             BindGrid();
 65         }
 66 
 67         protected void gvList_RowUpdating(object sender, GridViewUpdateEventArgs e)
 68         {
 69             try
 70             {
 71                 ZhuJi.UUMS.Domain.Roles domainRoles = new ZhuJi.UUMS.Domain.Roles();
 72                 TextBox txtRoleName = (TextBox)gvList.Rows[e.RowIndex].FindControl("txtRoleName");
 73                 TextBox txtOrderBy = (TextBox)gvList.Rows[e.RowIndex].FindControl("txtOrderBy");
 74 
 75                 domainRoles.Id = int.Parse(gvList.Rows[e.RowIndex].Cells[0].Text);
 76                 domainRoles.RoleName = txtRoleName.Text.Trim();
 77                 domainRoles.OrderBy = int.Parse(txtOrderBy.Text.Trim());
 78 
 79                 ZhuJi.UUMS.Service.Roles roles = ZhuJi.AOP.Operator.WrapInterface(typeof(ZhuJi.UUMS.Service.Roles)) as ZhuJi.UUMS.Service.Roles;
 80                 roles.Update(domainRoles);
 81 
 82                 gvList.EditIndex = -1;
 83                 BindGrid();
 84             }
 85             catch (Exception ex)
 86             {
 87                 ShowMessage(ex);
 88             }
 89         }
 90 
 91         protected void gvList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
 92         {
 93             gvList.EditIndex = -1;
 94             BindGrid();
 95         }
 96 
 97         protected void gvList_RowInserting(object sender, EventArgs e)
 98         {
 99             try
100             {
101                 ZhuJi.UUMS.Domain.Roles domainRoles = new ZhuJi.UUMS.Domain.Roles();
102                 TextBox txtRoleName = (TextBox)gvList.FooterRow.FindControl("txtRoleName");
103                 TextBox txtOrderBy = (TextBox)gvList.FooterRow.FindControl("txtOrderBy");
104 
105                 domainRoles.RoleName = txtRoleName.Text.Trim();
106                 domainRoles.OrderBy = int.Parse(txtOrderBy.Text.Trim());
107 
108                 ZhuJi.UUMS.Service.Roles roles = ZhuJi.AOP.Operator.WrapInterface(typeof(ZhuJi.UUMS.Service.Roles)) as ZhuJi.UUMS.Service.Roles;
109                 roles.Insert(domainRoles);
110 
111                 gvList.EditIndex = -1;
112                 BindGrid();
113             }
114             catch (Exception ex)
115             {
116                 ShowMessage(ex);
117             }
118         }
119     }
120 }
121 
122 

 

 

 

 

posted on 2008-01-06 13:10  逐迹工作室  阅读(1103)  评论(7编辑  收藏  举报

导航