在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

相关文章

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

Java实现数据库图片上传与存储功能

《Java实现数据库图片上传与存储功能》在现代的Web开发中,上传图片并将其存储在数据库中是常见的需求之一,本文将介绍如何通过Java实现图片上传,存储到数据库的完整过程,希望对大家有所帮助... 目录1. 项目结构2. 数据库表设计3. 实现图片上传功能3.1 文件上传控制器3.2 图片上传服务4. 实现

Java实现数据库图片上传功能详解

《Java实现数据库图片上传功能详解》这篇文章主要为大家详细介绍了如何使用Java实现数据库图片上传功能,包含从数据库拿图片传递前端渲染,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1、前言2、数据库搭建&nbsChina编程p; 3、后端实现将图片存储进数据库4、后端实现从数据库取出图片给前端5、前端拿到

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

Python使用PIL库将PNG图片转换为ICO图标的示例代码

《Python使用PIL库将PNG图片转换为ICO图标的示例代码》在软件开发和网站设计中,ICO图标是一种常用的图像格式,特别适用于应用程序图标、网页收藏夹图标等场景,本文将介绍如何使用Python的... 目录引言准备工作代码解析实践操作结果展示结语引言在软件开发和网站设计中,ICO图标是一种常用的图像