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
友情链接管理:
效果:
QQkC0Dd1CpZAMpCkyzf54SULEd7NIFkAaib91w5umAAnoomWb7LCSlZjvZoAskCAACwN5pkyY2c3ds/UgAAAC9NULIAAAAA5AOSBQAAAAoAyQIAAAAFgGQBAACAAmD57TbsHykAAICXRpMs/+bEWUiWoz2aQLIAAADsjSZZ/skJKVmO9mgCyQJQN0f/YlyQowsG4KlokuXvnJCS5WiPJpAsAHXzzg/2A2SAJln+9xhN06w+KpGSZZ0Poeua9GbOCt8AoDIcyRJd3KboDP0okgVAQZMsf8Uw47qzbY5KM/uQnRJFSpaUXNIHm5ClTFzk28jQ8Q+p1TF0i/7kVP6pu3UeqkVJSMmiSBAnRd/dWLIsrKJLufSddfKhO6gWX/p2UQsKNbjiG2JSQVz6Nus/Z16EJln+jNE0TfQztCu3daRkSXQvdCHbH4Ozm+ielCyXvr1Vo/FfvYtuFTByE6EPN/3beagTJRGSLN7JlcMky1ZVNEgWimXsUx9vQcU3xK0Koig0yRJtME3TRD9Dic5JokjJstQ9yVIfEn27XofeatdLvxBAvmz1Fbb4L3cvh1eyOM+ANpIsn96++fhZOfr2yewsmmWZyY259kglC8UyOrJNp1p8Q3y90UWTLL/HaJom+jlumG2za4heZURKlhT3bB9SLL3KZqlvLsU3C7iDZHlV9AdDIe3iGKQtefn0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tVtvMjGufQMoeuOCsbZNazzba9wwXMhslVyiGRRZ1Zs3j+Mdq5quacjWV4AQhliS8mSIkeeKVnMtUIYs6XCK+rbXuGC50Jkq2TDtSy2BNH1ivOpIlWLR69Ir6if1UAoQ2iS5ZcExnY0bpgUO13a28bpyMaZ6J73cnaKbeNVNit82ytc8FyIbJVkL1muc9Xi1yvSK+pnNRDKEJpk+RpjbEdmO7Rh23u3U5CNMz2vV4g4njj+LHKPjqNWiGyV7PGSc1SveDdUvnz7H6vDEnpFekX9rAZCGeIhyfJ1LlDssd+RBVIHPFmyRD1xlJZXx6T7tle44LkQ2Sp55kvOoWmYZarFp1ekV9TPaiCUITTJ8iWBpmmiG95db4qCbJzp7nlnWWyb9LtI9G2vcMFzIbJV8rRZFnNIEsoi+PLtt369Ir2iflYDoQyhSZafE2iaxmxIpFkoexTZOBf5plzXa6bkTfFtr3DBcyGyVbLhG0POxImuQtI0ynqvqJ/VQChDaJLlpxhjEzXb8mjokNdMRzbOlFzeS9huex1wDFb4tle44LkQ2SrZ6qfkvJb6oaXXXeQV9bMaCGUITbL8mBOycR7t0QQdR60Q2SrZfC3LUVA/a4VQhtAkyw85IRvn0R5N0HHUCpGtEiQLZA6hDIFk2QA6jlohslWCZIHMIZQhNMnyfU7Ixnm0RxN0HLVCZKtknWTJkwfvC/KEUIbQJMt3OSEb59EeTUjfoBqIbJVs241mwtGFCltydG3KFE2y5EbO7u0fKQAAgJcmKFkAAAAA8gHJAgAAAAWAZAEAAIACQLIAAABAASBZAAAAoACQLAAAAFAASBYAAAAoACQLAAAAFACSBQAAAAoAyQIAAAAFgGQBAACAAkCyAAAAQAEgWQAAAKAAkCwAAABQAEgWAAAAKAAkCwAAABQAkgUAAAAK4P+DdhmPTxaNpQAAAABJRU5ErkJggg==" alt="" />
存在问题总结:
(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>