在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利用PIL进行图片压缩

《Python利用PIL进行图片压缩》有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所以本文为大家介绍了Python中图片压缩的方法,需要的可以参考下... 有时在发送一些文件如PPT、Word时,由于文件中的图片太大,导致文件也太大,无法发送,所有可以对文件中的图

java获取图片的大小、宽度、高度方式

《java获取图片的大小、宽度、高度方式》文章介绍了如何将File对象转换为MultipartFile对象的过程,并分享了个人经验,希望能为读者提供参考... 目China编程录Java获取图片的大小、宽度、高度File对象(该对象里面是图片)MultipartFile对象(该对象里面是图片)总结java获取图片

Java实战之自助进行多张图片合成拼接

《Java实战之自助进行多张图片合成拼接》在当今数字化时代,图像处理技术在各个领域都发挥着至关重要的作用,本文为大家详细介绍了如何使用Java实现多张图片合成拼接,需要的可以了解下... 目录前言一、图片合成需求描述二、图片合成设计与实现1、编程语言2、基础数据准备3、图片合成流程4、图片合成实现三、总结前

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas

css实现图片旋转功能

《css实现图片旋转功能》:本文主要介绍了四种CSS变换效果:图片旋转90度、水平翻转、垂直翻转,并附带了相应的代码示例,详细内容请阅读本文,希望能对你有所帮助... 一 css实现图片旋转90度.icon{ -moz-transform:rotate(-90deg); -webkit-transfo

C#实现添加/替换/提取或删除Excel中的图片

《C#实现添加/替换/提取或删除Excel中的图片》在Excel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更加美观,下面我们来看看如何在C#中实现添加/替换/提取或删除E... 在Excandroidel中插入与数据相关的图片,能将关键数据或信息以更直观的方式呈现出来,使文档更

如何设置vim永久显示行号

《如何设置vim永久显示行号》在Linux环境下,vim默认不显示行号,这在程序编译出错时定位错误语句非常不便,通过修改vim配置文件vimrc,可以在每次打开vim时永久显示行号... 目录设置vim永久显示行号1.临时显示行号2.永www.chinasem.cn久显示行号总结设置vim永久显示行号在li

C#中图片如何自适应pictureBox大小

《C#中图片如何自适应pictureBox大小》文章描述了如何在C#中实现图片自适应pictureBox大小,并展示修改前后的效果,修改步骤包括两步,作者分享了个人经验,希望对大家有所帮助... 目录C#图片自适应pictureBox大小编程修改步骤总结C#图片自适应pictureBox大小上图中“z轴

使用Python将长图片分割为若干张小图片

《使用Python将长图片分割为若干张小图片》这篇文章主要为大家详细介绍了如何使用Python将长图片分割为若干张小图片,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. python需求的任务2. Python代码的实现3. 代码修改的位置4. 运行结果1. Python需求

电脑显示hdmi无信号怎么办? 电脑显示器无信号的终极解决指南

《电脑显示hdmi无信号怎么办?电脑显示器无信号的终极解决指南》HDMI无信号的问题却让人头疼不已,遇到这种情况该怎么办?针对这种情况,我们可以采取一系列步骤来逐一排查并解决问题,以下是详细的方法... 无论你是试图为笔记本电脑设置多个显示器还是使用外部显示器,都可能会弹出“无HDMI信号”错误。此消息可能