zoukankan      html  css  js  c++  java
  • 《ASP.NET1200例》高亮显示ListView中的数据行并自动切换图片

    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也不起作用       

  • 相关阅读:
    _CrtSetBreakAlloc(…)来检测内存泄漏+VC使用CRT调试功能检测内存泄漏(转)
    VC 2005 解决方案的目录结构设置和管理
    ArcGIS Engine基础开发教程(转)
    vc++实现avi文件的操作 用于视频解析及录制(转)
    微软免费杀毒软件下周二公测 年底推简体中文版 狼人:
    大量用户升级iPhone3.0系统导致苹果服务器故障 狼人:
    苹果发布45个iPhone和iTouch漏洞补丁 狼人:
    “汉网热血三国”“南方电视台”等网站被挂马 狼人:
    WAPI有望晋身国际标准 最大阻力美国首度支持 狼人:
    前Google员工推云安全服务检测网站挂马 狼人:
  • 原文地址:https://www.cnblogs.com/abc8023/p/3457219.html
Copyright © 2011-2022 走看看