ASP.NET笔记之 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 using ListView.DAL.DataSetLinksTableAdapters;
 9 
10 namespace ListView
11 {
12     public partial class LinksMer : System.Web.UI.Page
13     {
14         protected void Page_Load(object sender, EventArgs e)
15         {
16 
17         }
18 
19         protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
20         {
21             if(e.Item.ItemType== ListViewItemType.DataItem)
22             {
23                 //ListViewItem lvi = (ListViewItem)e.Item.DataItem;
24                 DataRowView drv = (DataRowView)e.Item.DataItem;
25                 if(drv!=null)
26                 {
27                    var ll=(ListView.DAL.DataSetLinks.T_LinksRow)drv.Row;
28                    DropDownList DropDownList1 = (DropDownList)e.Item.FindControl("DropDownList1");
29                    DropDownList1.SelectedValue = ll.LinkType.ToString();
30                 }
31             }
32             
33         }
34 
35         protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
36         {
37             DropDownList DropDownList1 = (DropDownList)e.Item.FindControl("DropDownList1");
38             e.Values["LinkType"] = DropDownList1.SelectedValue;
39             DataRowView drv = (DataRowView)e.Item.DataItem;
40         }
41 
42         protected void ListView1_ItemUpdating(object sender, ListViewUpdateEventArgs e)
43         {
44             DropDownList DropDownList1 = (DropDownList)ListView1.Items[e.ItemIndex].FindControl("DropDownList1");
45             e.NewValues["LinkType"]= DropDownList1.SelectedValue;
46         }
47 
48         protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
49         {
50             //对于Insert、Item界面的初始化在ItemCreated比在ItemDataBound更好
51             if (e.Item.ItemType == ListViewItemType.DataItem || e.Item.ItemType == ListViewItemType.InsertItem)
52             {
53                 DropDownList DropDownList1 = (DropDownList)e.Item.FindControl("DropDownList1");
54                 TextBox LogoURLTextBox = (TextBox)e.Item.FindControl("LogoURLTextBox");
55                 if (DropDownList1 != null&&LogoURLTextBox!=null)
56                 {
57                     LogoURLTextBox.Attributes["myid"] = LogoURLTextBox.ClientID;
58                     DropDownList1.Attributes["onchange"] = "onLinkTypeChange(this,'" + LogoURLTextBox.ClientID + "')";
59                     if (DropDownList1.SelectedValue == "Text")
60                     {
61                         //LogoURLTextBox.Visible = false;
62                         LogoURLTextBox.Style["display"] = "none";
63                     }
64                 }
65             }
66         }
67     }
68 }

LinkUrl_Admin.aspx

  1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LinksMer.aspx.cs" Inherits="ListView.LinksMer" %>
  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="js/jquery-1.4.2.min.js" type="text/javascript"></script>
  9     <script type="text/javascript">
 10         function onLinkTypeChange(src,logoTextId) {
 11                 if($(src).val()=="Text") {
 12                     $("input:text[myid=" + logoTextId+"]").hide();
 13                     //                   alert(logotextid);获取控件失败,待处理
 14                     //alert("input:text[myid=" + logoTextId + "]");
 15                 }
 16                 else {
 17                     $("input:text[myid=" + logoTextId + "]").show();
 18                 }
 19         }        
 20     </script>
 21 </head>
 22 <body>
 23     <form id="form1" runat="server">
 24     <div>
 25     
 26         <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
 27             DeleteMethod="Delete" InsertMethod="Insert" 
 28             OldValuesParameterFormatString="original_{0}" SelectMethod="GetData" 
 29             TypeName="ListView.DAL.DataSetLinksTableAdapters.T_LinksTableAdapter" 
 30             UpdateMethod="Update">
 31             <DeleteParameters>
 32                 <asp:Parameter Name="Original_Id" Type="Int64" />
 33             </DeleteParameters>
 34             <InsertParameters>
 35                 <asp:Parameter Name="SeqNo" Type="Int32" />
 36                 <asp:Parameter Name="SiteName" Type="String" />
 37                 <asp:Parameter Name="LinkType" Type="String" />
 38                 <asp:Parameter Name="SiteURL" Type="String" />
 39                 <asp:Parameter Name="LogoURL" Type="String" />
 40             </InsertParameters>
 41             <UpdateParameters>
 42                 <asp:Parameter Name="SeqNo" Type="Int32" />
 43                 <asp:Parameter Name="SiteName" Type="String" />
 44                 <asp:Parameter Name="LinkType" Type="String" />
 45                 <asp:Parameter Name="SiteURL" Type="String" />
 46                 <asp:Parameter Name="LogoURL" Type="String" />
 47                 <asp:Parameter Name="Original_Id" Type="Int64" />
 48             </UpdateParameters>
 49         </asp:ObjectDataSource>
 50         <asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" 
 51             DataSourceID="ObjectDataSource1" InsertItemPosition="LastItem" 
 52             onitemdatabound="ListView1_ItemDataBound" 
 53             oniteminserting="ListView1_ItemInserting" 
 54             onitemupdating="ListView1_ItemUpdating" 
 55             onitemcreated="ListView1_ItemCreated">
 56             <EditItemTemplate>
 57                 <tr style="">
 58                     <td>
 59                         <asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="更新" />
 60                         <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="取消" />
 61                     </td>
 62                     <td>
 63                         <asp:TextBox ID="SeqNoTextBox" runat="server" Text='<%# Bind("SeqNo") %>' />
 64                     </td>
 65                     <td>
 66                         <asp:TextBox ID="SiteNameTextBox" runat="server" 
 67                             Text='<%# Bind("SiteName") %>' />
 68                     </td>
 69                     <td>
 70                         <asp:DropDownList ID="DropDownList1" runat="server">
 71                             <asp:ListItem Value="Text">文本</asp:ListItem>
 72                             <asp:ListItem Value="Pic">图片</asp:ListItem>
 73                         </asp:DropDownList>
 74                     </td>
 75                     <td>
 76                         <asp:TextBox ID="SiteURLTextBox" runat="server" Text='<%# Bind("SiteURL") %>' />
 77                     </td>
 78                     <td>
 79                         <asp:TextBox ID="LogoURLTextBox" runat="server" Text='<%# Bind("LogoURL") %>' />
 80                     </td>
 81                 </tr>
 82             </EditItemTemplate>
 83             <EmptyDataTemplate>
 84                 <table runat="server" style="">
 85                     <tr>
 86                         <td>
 87                             未返回数据。</td>
 88                     </tr>
 89                 </table>
 90             </EmptyDataTemplate>
 91             <InsertItemTemplate>
 92                 <tr style="">
 93                     <td>
 94                         <asp:Button ID="InsertButton" runat="server" ValidationGroup="insert" CommandName="Insert" Text="插入" />
 95                         <asp:Button ID="CancelButton" runat="server" ValidationGroup="insert" CommandName="Cancel" Text="清除" />
 96                     </td>
 97                     <td>
 98                         <asp:TextBox ID="SeqNoTextBox" runat="server" ValidationGroup="insert" Text='<%# Bind("SeqNo") %>' />
 99                         <asp:RequiredFieldValidator
100                             ID="RequiredFieldValidator1" runat="server" ValidationGroup="insert" ErrorMessage="*" ControlToValidate="SeqNoTextBox" Display="Dynamic"></asp:RequiredFieldValidator>
101                             <asp:CompareValidator
102                                 ID="CompareValidator1" runat="server" ValidationGroup="insert" ErrorMessage="必须为整数" ControlToValidate="SeqNoTextBox" Type="Integer" Operator="DataTypeCheck" Display="Dynamic"></asp:CompareValidator>
103                     </td>
104                     <td>
105                         <asp:TextBox ID="SiteNameTextBox" runat="server" ValidationGroup="insert"  MaxLength="50"
106                             Text='<%# Bind("SiteName") %>' />
107                             <asp:RequiredFieldValidator ID="RequiredFieldValidator2"
108                                 runat="server" ValidationGroup="insert"  ErrorMessage="*" ControlToValidate="SiteNameTextBox"></asp:RequiredFieldValidator>
109                     </td>
110                     <td>
111                         <asp:DropDownList ID="DropDownList1" ValidationGroup="insert"   runat="server" AutoPostBack="False">
112                             <asp:ListItem Value="Text">文本</asp:ListItem>
113                             <asp:ListItem Value="Pic">图片</asp:ListItem>
114                         </asp:DropDownList>
115                     </td>
116                     <td>
117                         <asp:TextBox ID="SiteURLTextBox" ValidationGroup="insert"  runat="server" Text='<%# Bind("SiteURL") %>' /><asp:RequiredFieldValidator
118                             ID="RequiredFieldValidator3" ValidationGroup="insert"  runat="server" ErrorMessage="*" ControlToValidate="SiteURLTextBox"></asp:RequiredFieldValidator>
119                     </td>
120                     <td>
121                         <asp:TextBox ID="LogoURLTextBox" ValidationGroup="insert"   runat="server" Text='<%# Bind("LogoURL") %>' />
122                     </td>
123                 </tr>
124             </InsertItemTemplate>
125             <ItemTemplate>
126                 <tr style="">
127                     <td>
128                         <asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="删除" />
129                         <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="编辑" />
130                     </td>
131                     <td>
132                         <asp:Label ID="SeqNoLabel" runat="server" Text='<%# Eval("SeqNo") %>' />
133                     </td>
134                     <td>
135                         <asp:Label ID="SiteNameLabel" runat="server" Text='<%# Eval("SiteName") %>' />
136                     </td>
137                     <td>
138                         <asp:DropDownList ID="DropDownList1" runat="server" Enabled="false">
139                             <asp:ListItem Value="Text">文本</asp:ListItem>
140                             <asp:ListItem Value="Pic">图片</asp:ListItem>
141                         </asp:DropDownList>
142                     </td>
143                     <td>
144                         <asp:Label ID="SiteURLLabel" runat="server" Text='<%# Eval("SiteURL") %>' />
145                     </td>
146                     <td>
147                         <asp:Label ID="LogoURLLabel" runat="server" Text='<%# Eval("LogoURL") %>' />
148                     </td>
149                 </tr>
150             </ItemTemplate>
151             <LayoutTemplate>
152                 <table runat="server">
153                     <tr runat="server">
154                         <td runat="server">
155                             <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
156                                 <tr runat="server" style="">
157                                     <th runat="server">
158                                     </th>
159                                     <th runat="server">
160                                         序号</th>
161                                     <th runat="server">
162                                         网站名</th>
163                                     <th runat="server">
164                                         类型</th>
165                                     <th runat="server">
166                                         连接地址</th>
167                                     <th runat="server">
168                                         Logo地址</th>
169                                 </tr>
170                                 <tr ID="itemPlaceholder" runat="server">
171                                 </tr>
172                             </table>
173                         </td>
174                     </tr>
175                     <tr runat="server">
176                         <td runat="server" style="">
177                         </td>
178                     </tr>
179                 </table>
180             </LayoutTemplate>
181         </asp:ListView>
182     
183     </div>
184     </form>
185 </body>
186 </html>

 

posted @ 2013-11-24 14:55  lechenging  阅读(2116)  评论(0编辑  收藏  举报