• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETListView控件基本操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1、前台代码

View Code
  1 <asp:ListView ID="lvDepartment" runat="server" DataKeyNames="F_DepartmentID" EnableModelValidation="True" 
2 InsertItemPosition="LastItem" oniteminserting="lvDepartment_ItemInserting"
3 onitemediting="lvDepartment_ItemEditing"
4 onitemcanceling="lvDepartment_ItemCanceling"
5 onitemdeleting="lvDepartment_ItemDeleting"
6 onitemupdating="lvDepartment_ItemUpdating">
7 <AlternatingItemTemplate>
8 <tr style="">
9 <td>
10 <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" onclientclick="return confirm('确认删除?')"/>
11 <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
12 </td>
13 <td>
14 <asp:Label ID="F_DepartmentNameLabel" runat="server"
15 Text='<%# Eval("F_DepartmentName") %>' />
16 </td>
17 <td>
18 <asp:Label ID="F_OrderLabel" runat="server" Text='<%# Eval("F_Order") %>' />
19 </td>
20 </tr>
21 </AlternatingItemTemplate>
22 <EditItemTemplate>
23 <tr style="">
24 <td>
25 <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
26 <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
27 </td>
28 <td>
29 <asp:TextBox ID="F_DepartmentNameTextBox" runat="server" Text='<%# Bind("F_DepartmentName") %>' CssClass="wenben" />
30 </td>
31 <td>
32 <asp:TextBox ID="F_OrderTextBox" runat="server" Text='<%# Bind("F_Order") %>' CssClass="order"
33 onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;" />
34 </td>
35 </tr>
36 </EditItemTemplate>
37 <EmptyDataTemplate>
38 <table runat="server" style="">
39 <tr>
40 <td>未返回数据。</td>
41 </tr>
42 </table>
43 </EmptyDataTemplate>
44 <InsertItemTemplate>
45 <tr style="">
46 <td>
47 <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
48 <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
49 </td>
50 <td>
51 <asp:TextBox ID="F_DepartmentNameTextBox" runat="server" Text='<%# Bind("F_DepartmentName") %>' CssClass="wenben" />
52 </td>
53 <td>
54 <asp:TextBox ID="F_OrderTextBox" runat="server" Text='<%# Bind("F_Order") %>' CssClass="order"
55 onkeypress="if (event.keyCode < 48 || event.keyCode >57) event.returnValue = false;" />
56 </td>
57 </tr>
58 </InsertItemTemplate>
59 <ItemTemplate>
60 <tr style="">
61 <td>
62 <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" onclientclick="return confirm('确认删除?')" />
63 <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
64 </td>
65 <td>
66 <asp:Label ID="F_DepartmentNameLabel" runat="server" Text='<%# Eval("F_DepartmentName") %>' />
67 </td>
68 <td>
69 <asp:Label ID="F_OrderLabel" runat="server" Text='<%# Eval("F_Order") %>' />
70 </td>
71 </tr>
72 </ItemTemplate>
73 <LayoutTemplate>
74 <table runat="server">
75 <tr runat="server">
76 <td runat="server">
77 <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
78 <tr runat="server" style="">
79 <td runat="server" align="left"></td>
80 <td runat="server" align="left"><strong>部门名称</strong></td>
81 <td runat="server" align="left"><strong>顺序</strong></td>
82 </tr>
83 <tr ID="itemPlaceholder" runat="server" align="left">
84 </tr>
85 </table>
86 </td>
87 </tr>
88 <tr runat="server">
89 <td runat="server" style="">
90 <asp:DataPager ID="DataPager1" runat="server">
91 <Fields>
92 <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
93 ShowNextPageButton="False" ShowPreviousPageButton="False" />
94 <asp:NumericPagerField />
95 <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True"
96 ShowNextPageButton="False" ShowPreviousPageButton="False" />
97 </Fields>
98 </asp:DataPager>
99 </td>
100 </tr>
101 </table>
102 </LayoutTemplate>
103 <SelectedItemTemplate>
104 <tr style="">
105 <td>
106 <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
107 <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
108 </td>
109 <td>
110 <asp:Label ID="F_DepartmentNameLabel" runat="server" Text='<%# Eval("F_DepartmentName") %>' />
111 </td>
112 <td>
113 <asp:Label ID="F_OrderLabel" runat="server" Text='<%# Eval("F_Order") %>' />
114 </td>
115 </tr>
116 </SelectedItemTemplate>
117 </asp:ListView>

2、后台代码

View Code
 1  public partial class department : Mis.PageBase
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 if (!IsPostBack&&configcompany!=null)
6 {
7 lvDepartmentBind();
8 }
9 }
10
11 protected void lvDepartment_ItemInserting(object sender, ListViewInsertEventArgs e)
12 {
13 TextBox txDepart = (TextBox)e.Item.FindControl("F_DepartmentNameTextBox");
14 TextBox txOrder = (TextBox)e.Item.FindControl("F_OrderTextBox");
15 if (txDepart.Text == "")
16 {
17 lbMessage.Text = "部门不能为空";
18 }
19 else if (txOrder.Text == "")
20 {
21 lbMessage.Text = "没有填写顺序";
22 }
23 else
24 {
25 bool bl = Mis.GetUserInfo.InsertDepartment(txDepart.Text, Convert.ToInt32(txOrder.Text), configcompany.CompanyID);
26 lvDepartmentBind();
27 HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
28 }
29 }
30
31 protected void lvDepartment_ItemEditing(object sender, ListViewEditEventArgs e)
32 {
33 lvDepartment.EditIndex = e.NewEditIndex;
34 lvDepartmentBind();
35 HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
36 }
37
38 protected void lvDepartment_ItemDeleting(object sender, ListViewDeleteEventArgs e)
39 {
40 int KeyId = Convert.ToInt32(lvDepartment.DataKeys[e.ItemIndex].Value);
41 bool bl = Mis.GetUserInfo.DeleteDepartment(KeyId);
42 lvDepartmentBind();
43 HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
44 }
45
46 protected void lvDepartment_ItemUpdating(object sender, ListViewUpdateEventArgs e)
47 {
48 int KeyId = Convert.ToInt32(lvDepartment.DataKeys[e.ItemIndex].Value);
49 TextBox txDepart =(TextBox)lvDepartment.Items[e.ItemIndex].FindControl("F_DepartmentNameTextBox");
50 TextBox txOrder =(TextBox)lvDepartment.Items[e.ItemIndex].FindControl("F_OrderTextBox");
51 if (txDepart.Text=="")
52 lbMessage.Text ="部门不能为空";
53 else if(txOrder.Text=="")
54 lbMessage.Text="顺序不能为空";
55 else
56 {
57 string depart =Server.HtmlEncode(txDepart.Text);
58 int order = Convert.ToInt32(txOrder.Text);
59 bool bl = Mis.GetUserInfo.UpdateDepartment(depart,order,KeyId);
60 lvDepartment.EditIndex = -1;
61 lvDepartmentBind();
62 HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
63 }
64 }
65
66 protected void lvDepartment_ItemCanceling(object sender, ListViewCancelEventArgs e)
67 {
68 //取消编辑
69 if (e.CancelMode == ListViewCancelMode.CancelingEdit)
70 {
71 //e.Cancel = true;
72 lvDepartment.EditIndex = -1;
73 lvDepartmentBind();
74 HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
75 }
76 else if (e.CancelMode == ListViewCancelMode.CancelingInsert)
77 {
78 lvDepartmentBind();
79 HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
80 return;
81 }
82 }
83 protected void lvDepartmentBind()
84 {
85 lvDepartment.DataSource = Mis.GetUserInfo.ReadDepartmentList(configcompany.CompanyID);
86 lvDepartment.DataBind();
87
88 }
89 }

注释:Mis.PageBase:封装了页面登陆判断等一些信息;具体数据的处理,像插入数据库库、数据判断等在其他封装类里实现。

这两段代码只是listview控件的一些基本的操作!


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET Core WebApi如何动态生成树形Json格式数据发布时间:2022-07-10
下一篇:
ASP.NETCore入门(1)(搭建环境CentOS)发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap