在DataList中单击ImageButton,显示图片详细信息

2024-02-07 08:58

本文主要是介绍在DataList中单击ImageButton,显示图片详细信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

以下是HTML代码:
<% @ Page Language="C#" MasterPageFile="~/MasterPage2.master" AutoEventWireup="true"
    CodeFile
="ListPhoto.aspx.cs" Inherits="ListPhoto" Title="List Photo" 
%>

< asp:Content  ID ="Content1"  ContentPlaceHolderID ="ContentPlaceHolder1"  runat ="Server" >
    
< asp:DataList  ID ="dlPhoto"  runat ="server"  RepeatColumns ="4"  RepeatDirection ="Horizontal"
        CellPadding
="0"  Width ="790px"  OnItemCommand ="dlPhoto_ItemCommand" >
        
< ItemTemplate >
            
< asp:ImageButton  Width ="185"  ID ="ibtnPhoto"  ImageUrl ='<%#  FormatURL(DataBinder.Eval(Container,"DataItem.user_name"),DataBinder.Eval(Container, "DataItem.photo_type_id"),DataBinder.Eval(Container, "DataItem.photo_id")) % > '
                runat="server" AlternateText="click list Details information!" CommandName="Detail" OnClick="ibtnPhoto_Click" />
< br  />
            
< asp:Label  ID ="lblPhotoType"  runat ="server"  Text ='<%#  DataBinder.Eval(Container, "DataItem.photo_type_id")% > '
                Visible="false">
</ asp:Label >< br  />
            
< asp:Label  ID ="lblPhotoName"  runat ="server"  Text ='<%#  DataBinder.Eval(Container, "DataItem.photo_id")% > '
                Visible="false">
</ asp:Label >
        
</ ItemTemplate >
    
</ asp:DataList >
</ asp:Content >
以下是对应的后台代码:
using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  ListPhoto : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if(!Page.IsPostBack)
        
{
            
if(!(Convert.ToBoolean(Session["isLogin"])))
            
{
                Response.Redirect(
"Login.aspx");
            }

            
else
            
{
                System.Data.SqlClient.SqlConnection conn 
= new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DogsManagerConnectionString"].ToString());
                
string code,strSQL;
                code 
= Request.QueryString["code"].ToString();
                
if(code == "A")
                
{
                    strSQL 
= string.Format("select user_name,photo_type_id,photo_id,photo from E_Photo where user_name='{0}'", Session["name"].ToString());
                }

                
else
                
{
                    strSQL 
= string.Format("select user_name,photo_type_id,photo_id,photo from E_Photo where user_name='{0}' AND photo_type_id={1}", Session["name"].ToString(),code);
                }

                System.Data.SqlClient.SqlCommand cmd 
= new System.Data.SqlClient.SqlCommand(strSQL, conn);
                
try
                
{
                    conn.Open();
                    
this.dlPhoto.DataSource = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    
this.dlPhoto.DataBind();
                }

                
catch(System.Data.SqlClient.SqlException sql)
                
{
                    Response.Write(
"wrong information:<br>" + sql.Message);
                }

                
finally
                
{
                    cmd.Dispose();
                    conn.Close();
                }

            }

        }

    }


    
protected string FormatURL(object userName, object typeID, object photoID)
    
{
        
return string.Format("ReadImage.aspx?userName={0}&typeID={1}&photoID={2}",
            userName.ToString(), typeID.ToString(), photoID.ToString());
    }

    
protected void dlPhoto_ItemCommand(object source, DataListCommandEventArgs e)
    
{
        
if(e.CommandName == "Detail")
        
{
            
string userName = Session["name"].ToString();
            
string typeId = ((Label)e.Item.FindControl("lblPhotoType")).Text;
            
string photoId = ((Label)e.Item.FindControl("lblPhotoName")).Text;
            
string Format = string.Format("ListPhotoDetail.aspx?userName={0}&typeID={1}&photoID={2}",
                userName, typeId, photoId);
            
//ImageButton ibtnDetail = (ImageButton)e.Item.FindControl("ibtnPhoto");
            
//ImageButton ibtnDetail = (ImageButton)this.dlPhoto.Items[e.Item.ItemIndex].FindControl("ibtnPhoto");
            
//添加单击事件
            
//ibtnDetail.Click += new ImageClickEventHandler(ibtnDetail_Click);
            respon(Format);
        }

    }

    
/// <summary>
    
/// 在模板列中添加的单击事件
    
/// </summary>    

    //void ibtnDetail_Click(object sender, ImageClickEventArgs e)
    
//{
    
//    Response.Redirect("http://www.baidu.com");
    
//}
    void respon(string url)
    
{
        Response.Write(
"<script language='javascript'>window.open('"+url+"','newWindow');</script>");
    }

    
protected void ibtnPhoto_Click(object sender, ImageClickEventArgs e)
    
{
        
    }

}

 以下是显示详细页面ListPhotoDetail.aspx对应的HTML代码:

 

<% @ Page Language="C#" AutoEventWireup="true" CodeFile="ListPhotoDetail.aspx.cs" Inherits="ListPhotoDetail1"  %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > List Photo Detail </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< table  border ="0"  cellpadding ="0"  cellspacing ="0" >
        
< tr >
            
< td  colspan ="2"  style ="height: 53px" >
                
< span  style ="font-size: 14pt" >< strong > List Details Photo </ strong ></ span ></ td >
        
</ tr >
        
< tr >
            
< td  colspan ="2" >
                
< asp:Image  ID ="imgPhoto"  runat ="server"   /></ td >
        
</ tr >
        
< tr >
            
< td  align ="right"  style ="height: 24px" >
                
< asp:Label  ID ="lblLoadDate"  runat ="server"  Text ="UpLoad Date:" ></ asp:Label ></ td >
            
< td  align ="left"  style ="height: 24px" >
                
< asp:TextBox  ID ="txtLoadDate"  runat ="server"  Width ="164px" ></ asp:TextBox ></ td >
        
</ tr >
        
< tr >
            
< td  align ="right" >
                
< asp:Label  ID ="Label2"  runat ="server"  Text ="Description:" ></ asp:Label ></ td >
            
< td  align ="left" >
                
< asp:TextBox  ID ="txtDesc"  runat ="server"  Height ="248px"  TextMode ="MultiLine"  Width ="401px" ></ asp:TextBox ></ td >
        
</ tr >
    
</ table >
        
< asp:Button  ID ="btnOK"  runat ="server"  Text ="OK"   />
    
</ form >
</ body >
</ html >

以下是对应的后台代码:

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  ListPhotoDetail1 : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
        
if(!Page.IsPostBack)
        
{
            
string username = Request.QueryString["userName"].ToString();
            
string typeid = Request.QueryString["typeID"].ToString();
            
string photoid = Request.QueryString["photoID"].ToString();
            System.Data.SqlClient.SqlConnection conn 
= new System.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DogsManagerConnectionString"].ToString());
            
string strSQL = string.Format("select Photo_Desc,UpLoad_Date from E_Photo where user_name='{0}' and photo_type_id=" +
                
"{1} and photo_id='{2}'", username, typeid, photoid);
            System.Data.SqlClient.SqlCommand cmd 
= new System.Data.SqlClient.SqlCommand(strSQL, conn);
            
try
            
{
                conn.Open();
                System.Data.SqlClient.SqlDataReader reader 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
                
if(reader.Read())
                
{
                    
//设置其他项
                    this.txtDesc.Text = reader["Photo_Desc"].ToString();
                    
this.txtLoadDate.Text = reader["UpLoad_Date"].ToString();
                }

            }

            
catch(System.Data.SqlClient.SqlException sql)
            
{
                
throw sql;
            }

            
finally
            
{
                cmd.Dispose();
                conn.Close();
            }

            
//设置图片的显示
            this.imgPhoto.ImageUrl = FormatURL(username, typeid, photoid);
        }

    }

    
/// <summary>
    
/// 获取格式化地址
    
/// </summary>
    
/// <param name="userName">user name</param>
    
/// <param name="typeID">phto type id</param>
    
/// <param name="photoID">photo number</param>
    
/// <returns></returns>

    protected string FormatURL(string userName, string typeID, string photoID)
    
{
        
return string.Format("ReadImage.aspx?userName={0}&typeID={1}&photoID={2}",
            userName, typeID, photoID);
    }

}

这篇关于在DataList中单击ImageButton,显示图片详细信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/687194

相关文章

关于MongoDB图片URL存储异常问题以及解决

《关于MongoDB图片URL存储异常问题以及解决》:本文主要介绍关于MongoDB图片URL存储异常问题以及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录MongoDB图片URL存储异常问题项目场景问题描述原因分析解决方案预防措施js总结MongoDB图

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

《使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)》PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了... 目录一、引言二、环境与工具三、python 提取PPT背景图片3.1 提取幻灯片背景图片3.2 提取

Python实现图片分割的多种方法总结

《Python实现图片分割的多种方法总结》图片分割是图像处理中的一个重要任务,它的目标是将图像划分为多个区域或者对象,本文为大家整理了一些常用的分割方法,大家可以根据需求自行选择... 目录1. 基于传统图像处理的分割方法(1) 使用固定阈值分割图片(2) 自适应阈值分割(3) 使用图像边缘检测分割(4)

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

使用Node.js制作图片上传服务的详细教程

《使用Node.js制作图片上传服务的详细教程》在现代Web应用开发中,图片上传是一项常见且重要的功能,借助Node.js强大的生态系统,我们可以轻松搭建高效的图片上传服务,本文将深入探讨如何使用No... 目录准备工作搭建 Express 服务器配置 multer 进行图片上传处理图片上传请求完整代码示例

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核