ASP.NET 学习笔记_010 ListView 和 DropDownList

1、Repeater用来显示数据、ListView用来操作数据

InsertItemTemplate和updateItemTemplate
**Eval(显示数据)和Bind(双向绑定:不仅是需要展现,更需要把数据绑定到数据库中)

ItemPlaceholderID:占位符,决定占位,把头部(之上)和尾部(之下)分隔开
ItemTemplate:展示功能

自动生成的ListView需要调整的地方
(1、生成的样式要提到style中,不要用内联的方式
(2、ItemTemplate里面一半没必要用<asp:Label>展示只读数据,所以可以直接输出
<%#Eval("id")%>
(3、LayoutTemplate中必须有一个ItempPlaceholderID 的服务端控件
(4、LayoutTemplate中表头的位置要汉化,所有template中的不需显示的字段需删除或更改位置

2、事件
流程同Repeater:

//首先判断数据行的类型
e.Item.ItemType==ListViewItemType.DataItem

//把e.Item转化成ListViewDataItem才能拿到DataItem
ListViewDataItem lvDataItem=(ListViewDataItem)e.Item;
DataRowView rowView=(DataRowView)lvDataItem.DataItem;
//获得某一列
var xRow=(...DAL.DataSet1.T_UserRow)rowVIew.Row;
//获得某一列的值
xRow.Age、xRow.sName...etc.

3、具体注意
(1、设定相应的按钮、控件、Validator为童颜的ValidationGroup,
防止不同模板中的Validator互相干扰,
(2、将Cancel按钮中的CausesValidation="false"使得插入修改数据时
可以取消操作,这样即使在同一个分组内也可以不互相影响

4、给InsertItemplate增加默认值
//在ItemCreate属性中进入函数
if(e.Item.ItemType==ListViewItemType.InsertItem){
TextBox AgeText=(TextBox)e.Item.FindControl("AgeID");
AgeText.Text="20";
}

5、主键Guid:插入到数据库

(1、ListView的ItemInserting属性:
//要插入到数据库之前的数据的键值对
e.values["id"]=Guid.NewGuid();

(2、ListView的ItemUpdateing属性:
e.ItemIdex
e.OldValues//更新前的值
e.NewValues["Age"]//更新后的值
e.Cancel=true;//取消非法数据插入

ObjectDataSource
绑定id为guid 类型的时候

 6、DropDrownList


(1、
//包含在DropDrownList中的项
<asp:ListItem value="man">男</asp:ListItem>

(2、
**后台代码:更新的时候
//找到ListView
//ListView1.Item[e.ItemIndex].FindControl("ID");
//它是一个DropViewList
DropDrownList d=(DropDrownList)listView1.Item[e.ItemIndex].FindControl("ID");
//赋值
e.NewValues=["字段"]=d.SelectedValue;

(3、
**后台代码:实现编辑时显示原先的数据
//有数据行
if(e.Item.ItemType==ListVIewDataList.DataItem){
//取控件
DropDownList d=(DropDownLIst)e.Item.FindControl("ID");

if(d!=null){
//取到这一行绑定的数据
ListViewDataItem lv=(ListViewDataItem)e.Item;
DataRowItem row=(dataRowItem)lv.DataItem;
//如果这一行有数据
if(row!=null){
//读取数据库该Row的值
var myRow=(项目名称.DAL.DataSetUsers.T_Users)row.Row;

//将读取打偶的Row值设置为下拉菜单中的选项
d.SelectedValue=myRow.字段;
}
}
}

(4、 可以看不可以用 Enabled="false

 

友情链接管理:

效果:

存在问题总结:

(1、警告 1 元素“ListView”不是已知元素。原因可能是网站中存在编译错误,或者缺少 web.config 文件。 E:\code\Projects\WebSite_zzl01\友情链接\LinkUrl_Admin.aspx 39 10 友情链接

(2、onLinkTypeChange(this,'" + logoID.ClientID + "') 中传给前台javascript的ID不是客户端的ID,会导致显示和隐藏的功能无法实现,所以增加一个myID

:   logoID.Attributes["myid"] = logoID.ClientID; 来传递参数

LinkUrl_Admin.aspx.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 using System.Data;
 8 
 9 namespace 友情链接
10 {
11     public partial class LinkUrl_Admin : System.Web.UI.Page
12     {
13         protected void Page_Load(object sender, EventArgs e)
14         {
15 
16         }
17 
18         protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
19         {
20             //ListView1的属性ItemDataBound数据绑定每一行
21             //显示数据
22             if (e.Item.ItemType == ListViewItemType.DataItem) {
23                 DropDownList ddlsLinkType = (DropDownList)e.Item.FindControl("ddlsLinkType");
24                 ListViewDataItem dataitem = (ListViewDataItem)e.Item;
25                 DataRowView myrow = (DataRowView)dataitem.DataItem;
26 
27                 if (ddlsLinkType != null && myrow != null) {
28                     var sUrl = (友情链接.ADL.DataSet1.T_LinksRow)myrow.Row;
29                     ddlsLinkType.SelectedValue = sUrl.sLinkType;
30                 }
31             }
32 
33         }
34 
35         protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
36         {
37             //插入数据
38             DropDownList ddlsLinkType = (DropDownList)e.Item.FindControl("ddlsLinkType");
39             e.Values["sLinkType"] = ddlsLinkType.SelectedValue;    
40         }
41 
42         protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
43         {
44             //更新数据
45             DropDownList ddlsLinkType = (DropDownList)ListView1.Items[e.ItemIndex].FindControl("ddlsLinkType");
46             e.NewValues["sLinkType"] = ddlsLinkType.SelectedValue; 
47         }
48 
49         protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
50         {
51             if (e.Item.ItemType == ListViewItemType.DataItem || e.Item.ItemType ==
52                 ListViewItemType.InsertItem) {
53                DropDownList ddlsLinkType = (DropDownList)e.Item.FindControl("ddlsLinkType");
54                TextBox logoID = (TextBox)e.Item.FindControl("LogoUrlTextBox");
55                 if (ddlsLinkType != null&&logoID!=null) {
56                     //onchange是html中select的属性
57                     //onLinkTypeChange是后台代码调用前台javascript中自定义的jQuery函数
58 
59                     logoID.Attributes["myid"] = logoID.ClientID;
60 
61                     ddlsLinkType.Attributes["onchange"] = "onLinkTypeChange(this,'" + logoID.ClientID + "')";
62                     if(ddlsLinkType.SelectedValue=="Text"){
63                         logoID.Style["display"] = "none";
64                     }
65                 } 
66             }
67         }
68     }
69 }

LinkUrl_Admin.aspx

  1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LinkUrl_Admin.aspx.cs" Inherits="友情链接.LinkUrl_Admin" %>
  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     <script src="Scripts/jquery-1.4.2.js" type="text/javascript"></script>
  9     <script type="text/javascript">
 10         function onLinkTypeChange(urlType, logoID) {
 11             if ($(urlType).val() == "Text") {
 12                 $("input:text[myid=" + logoID + "]").hide();
 13                 //$("#" + logoID).hide(); //传到到客户端不是客户端的id
 14                 //$("#ListView1_LogoURLTextBox").hide();//真正的id
 15             }
 16             else {
 17                 $("input:text[myid=" + logoID + "]").show();
 18                 //$("#" + logoID).show();
 19             }
 20         }
 21     </script>
 22 </head>
 23 <body>
 24     <form id="form1" runat="server">
 25     <div>
 26     
 27         <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
 28             DeleteMethod="Delete" InsertMethod="Insert" 
 29             OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
 30             TypeName="友情链接.ADL.DataSet1TableAdapters.T_LinksTableAdapter" 
 31             UpdateMethod="Update">
 32             <DeleteParameters>
 33                 <asp:Parameter Name="Original_ID" Type="Int64" />
 34             </DeleteParameters>
 35             <InsertParameters>
 36                 <asp:Parameter Name="SeoNo" Type="Int32" />
 37                 <asp:Parameter Name="SiteName" Type="String" />
 38                 <asp:Parameter Name="sLinkType" Type="String" />
 39                 <asp:Parameter Name="SiteUrl" Type="String" />
 40                 <asp:Parameter Name="LogoUrl" Type="String" />
 41             </InsertParameters>
 42             <UpdateParameters>
 43                 <asp:Parameter Name="SeoNo" Type="Int32" />
 44                 <asp:Parameter Name="SiteName" Type="String" />
 45                 <asp:Parameter Name="sLinkType" Type="String" />
 46                 <asp:Parameter Name="SiteUrl" Type="String" />
 47                 <asp:Parameter Name="LogoUrl" Type="String" />
 48                 <asp:Parameter Name="Original_ID" Type="Int64" />
 49             </UpdateParameters>
 50         </asp:ObjectDataSource>
 51     
 52     </div>
 53     <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" 
 54         DataSourceID="ObjectDataSource1" InsertItemPosition="LastItem" 
 55         onitemdatabound="ListView1_ItemDataBound" 
 56         oniteminserting="ListView1_ItemInserting" 
 57         onitemupdating="ListView1_ItemUpdating" 
 58         onitemcreated="ListView1_ItemCreated">
 59         
 60         <EditItemTemplate>
 61             <tr style="background-color: #999999;">
 62                 <td>
 63                     <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
 64                     <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
 65                 </td>
 66                 <td>
 67                     <asp:TextBox ID="SeoNoTextBox" runat="server" Text='<%# Bind("SeoNo") %>' />
 68                 </td>
 69                 <td>
 70                     <asp:TextBox ID="SiteNameTextBox" runat="server" 
 71                         Text='<%# Bind("SiteName") %>' />
 72                 </td>
 73                 <td>
 74                    <asp:DropDownList ID="ddlsLinkType" runat="server">
 75                      <asp:ListItem Value="Text">文本</asp:ListItem>
 76                      <asp:ListItem Value="Pic">图片</asp:ListItem>
 77                    </asp:DropDownList>
 78                 </td>
 79                 <td>
 80                     <asp:TextBox ID="SiteUrlTextBox" runat="server" Text='<%# Bind("SiteUrl") %>' />
 81                 </td>
 82                 <td>
 83                     <asp:TextBox ID="LogoUrlTextBox" runat="server" Text='<%# Bind("LogoUrl") %>' />
 84                 </td>
 85             </tr>
 86         </EditItemTemplate>
 87         <EmptyDataTemplate>
 88             <table runat="server" 
 89                 style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
 90                 <tr>
 91                     <td>
 92                         未返回数据。</td>
 93                 </tr>
 94             </table>
 95         </EmptyDataTemplate>
 96         <InsertItemTemplate>
 97             <tr style="">
 98                 <td>
 99                     <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" ValidationGroup="Insert" />
100                     <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
101                 </td>
102                 <td>
103                     <asp:TextBox ID="SeoNoTextBox" ValidationGroup="Insert" runat="server" Text='<%# Bind("SeoNo") %>' />
104                     <asp:RequiredFieldValidator ValidationGroup="Insert" ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate="SeoNoTextBox">
105                     </asp:RequiredFieldValidator>
106                     <asp:CompareValidator ValidationGroup="Insert" ID="CompareValidator1" runat="server" ErrorMessage="序号必须为整数" ControlToValidate="SeoNoTextBox" Operator="DataTypeCheck" Type="Integer">
107                     </asp:CompareValidator>
108                 </td>
109                 <td>
110                     <asp:TextBox ID="SiteNameTextBox" ValidationGroup="Insert" runat="server" MaxLength="50"
111                         Text='<%# Bind("SiteName") %>' />
112                     <asp:RequiredFieldValidator ValidationGroup="Insert" ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate="SiteNameTextBox">
113                     </asp:RequiredFieldValidator>
114                 </td>
115                 <td>
116                    <asp:DropDownList ID="ddlsLinkType" ValidationGroup="Insert" runat="server" >
117                      <asp:ListItem Value="Text">文本</asp:ListItem>
118                      <asp:ListItem Value="Pic">图片</asp:ListItem>
119                    </asp:DropDownList>
120                 </td>
121                 <td>
122                     <asp:TextBox ID="SiteUrlTextBox" ValidationGroup="Insert" runat="server" Text='<%# Bind("SiteUrl") %>' />
123                     <asp:RequiredFieldValidator ValidationGroup="Insert" ID="RequiredFieldValidator3" runat="server" ErrorMessage="*" ControlToValidate="SiteUrlTextBox">
124                     </asp:RequiredFieldValidator>
125                 </td>
126                 <td>
127                     <asp:TextBox ID="LogoUrlTextBox" ValidationGroup="Insert" runat="server" Text='<%# Bind("LogoUrl") %>' />
128                 </td>
129             </tr>
130         </InsertItemTemplate>
131         <ItemTemplate>
132             <tr style="background-color: #E0FFFF;color: #333333;">
133                 <td>
134                     <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
135                     <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
136                 </td>
137                 <td>
138                     <asp:Label ID="SeoNoLabel" runat="server" Text='<%# Eval("SeoNo") %>' />
139                 </td>
140                 <td>
141                     <asp:Label ID="SiteNameLabel" runat="server" Text='<%# Eval("SiteName") %>' />
142                 </td>
143                 <td>
144                    <asp:DropDownList ID="ddlsLinkType" runat="server" Enabled="false">
145                      <asp:ListItem Value="Text">文本</asp:ListItem>
146                      <asp:ListItem Value="Pic">图片</asp:ListItem>
147                    </asp:DropDownList>
148                 </td>
149                 <td>
150                     <asp:Label ID="SiteUrlLabel" runat="server" Text='<%# Eval("SiteUrl") %>' />
151                 </td>
152                 <td>
153                     <asp:Label ID="LogoUrlLabel" runat="server" Text='<%# Eval("LogoUrl") %>' />
154                 </td>
155             </tr>
156         </ItemTemplate>
157         <LayoutTemplate>
158             <table runat="server">
159                 <tr runat="server">
160                     <td runat="server">
161                         <table ID="itemPlaceholderContainer" runat="server" border="1" 
162                             style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
163                             <tr runat="server" style="background-color: #E0FFFF;color: #333333;">
164                                 <th runat="server">
165                                 </th>
166                                 <th runat="server">
167                                     序号</th>
168                                 <th runat="server">
169                                     网站名称</th>
170                                 <th runat="server">
171                                     链接类型</th>
172                                 <th runat="server">
173                                     网站网址</th>
174                                 <th runat="server">
175                                     logo网址</th>
176                             </tr>
177                             <tr ID="itemPlaceholder" runat="server">
178                             </tr>
179                         </table>
180                     </td>
181                 </tr>
182                 <tr runat="server">
183                     <td runat="server" 
184                         style="text-align: center;background-color: #5D7B9D;font-family: Verdana, Arial, Helvetica, sans-serif;color: #FFFFFF">
185                         <asp:DataPager ID="DataPager1" runat="server">
186                             <Fields>
187                                 <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" 
188                                     ShowLastPageButton="True" />
189                             </Fields>
190                         </asp:DataPager>
191                     </td>
192                 </tr>
193             </table>
194         </LayoutTemplate>
195     </asp:ListView>
196     </form>
197 </body>
198 </html>

 

 

转载于:https://www.cnblogs.com/daomul/archive/2013/04/07/3001670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值