《ASP.NET1200例》高亮显示ListView中的数据行并自动切换图片
2023-09-14 08:57:49 时间
aspx
<script type="text/javascript"> var oldColor; function SetNewColor(Source) { oldColor = Source.style.backgroudColor; Source.style.backgroudColor = "#FFCCFF"; } function SetOldColor(Source) { Source.style.backgroudColor = oldColor; } function ShowPhoto(url) { document.getElementById("Image1").src= url; } </script> <body> <form id="form1" runat="server"> <div> <asp:ListView ID="ListView1" runat="server" onitemdatabound="ListView1_ItemDataBound"> <ItemTemplate> <table id="myTable" border="1" cellpadding="5" cellspacing="0" runat="server" onmouseover='this.style.backgroundColor="#ff0000"'> <tr > <td> 图片ID: <asp:Label ID="Label1" runat="server" Text=""><%#Eval("id")%></asp:Label> 图片名称: <asp:TextBox ID="txtimageName" runat="server" Text='<%#Eval("imageName")%>'></asp:TextBox> 图片路径: <asp:TextBox ID="txtimageUrl" runat="server" Text='<%#Eval("imageUrl")%>'></asp:TextBox> </td> </tr> </table> </ItemTemplate> </asp:ListView> <asp:Image ID="Image1" runat="server" /> <asp:DataPager ID="DataPager1" PagedControlID="ListView1" runat="server"> </asp:DataPager> </div> </form> </body>
aspx.cs
ShowImageBll ShowImageBll = new BLL.ShowImageBll(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bindDL(); } } public void bindDL() { //绑定数据源 DataSet ds = ShowImageBll.GetList(); ListView1.DataSource = ds; ListView1.DataBind(); } protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) { if (e.Item.ItemType == ListViewItemType.DataItem) //判断目前的项目是否为一个数据项目 { ListViewDataItem curDataItem = (ListViewDataItem)e.Item; //把目前的项目转化为一个ListViewDataItem DataRowView curRow = (DataRowView)curDataItem.DataItem;//把获取的行转化为DataRowView对象 int id = int.Parse(curRow["id"].ToString()); String imageUrl = ShowImageBll.getImageUrlByID(id); HtmlTable myTable = (HtmlTable)curDataItem.FindControl("myTable");//获取ItemTemplate模板中的Table控件 myTable.Attributes.Add("onMouseOver", "SetNewColor(this);ShowPhoto('"+imageUrl+"');"); myTable.Attributes.Add("onMouseOut", "SetOldColor(this);ShowPhoto('image/20131129.jpg');"); } }
总结:
【1】前段HTML的Table控件 定义: <table id="myTable" border="1" cellpadding="5" cellspacing="0" runat="server">
后端获取时:HtmlTable myTable = (HtmlTable)curDataItem.FindControl("myTable");//获取ItemTemplate模板中的Table控件
(此时需要添加引用using System.Web.UI.HtmlControls;)
【2】ListViewDataItem curDataItem = (ListViewDataItem)e.Item; //把目前的项目转化为一个ListViewDataItem
DataRowView curRow = (DataRowView)curDataItem.DataItem;//把获取的ListViewItem对象所绑定的数据转化为DataRowView对象
【3】遗留的问题:背景颜色的切换不起作用。函数SetNewColor(this)或者设置在aspx页面内嵌javascript也不起作用
相关文章
- ASP如何连接MySQL数据库(asp怎么连接mysql)
- 数据ASP 实现MySQL数据库更新(asp更新mysql)
- .net利用MSSQL连接ASP.NET的办法探索(mssql 连接asp)
- 用MSSQL查询ASP:从初学者到高手(mssql查询asp)
- ASP网页MySQL注入及防御研究(asp页面mysql注入)
- 基于ASP的MySQL数据库连接实现(asp连接数mysql)
- ASP将MySQL数据迁移到新环境(asp迁移mysql)
- 库ASP实现MySQL数据库的轻松访问(asp读取mysql数据)
- 用 ASP 管理 MySQL 源码,轻松获取开发效率(asp管理mysql源码)
- ASP操作MySQL数据库实现简单便捷功能(asp用mysql数据库)
- MySQL重新梳理ASP源码转换指南(asp源码转mysql)
- ASP连接MySQL的实现方式(asp文件连接mysql)
- ASP快速连接MySQL解决方案(asp如何连mysql)
- ASP程序开发探索之oracle数据库(asp和oracle)
- asp,VBscript语法错误,史上最全最详细最精确
- asp.net(c#)水仙花数
- Server.Execute方法执行指定的ASP程序
- CODEPAGE列表与asp应用例子
- asp.net提高网站速度及如何利用缓存
- ASP.NET系统关键字及保留字列表整理
- ASP.NET实现验证码以及刷新验证码的小例子
- ASP.NET?001:GridView绑定List、页面返回值具体实现