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

来源:互联网 发布:俄罗斯电视台直播软件 编辑:程序博客网 时间:2024/06/05 20:09
以下是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);
    }

}

原创粉丝点击