【问题标题】:ASP.NET - Clicking on an image should redirect to a specific page with image urlASP.NET - 单击图像应重定向到具有图像 url 的特定页面
【发布时间】:2019-05-29 23:53:30
【问题描述】:

我基本上是为我的大学项目制作一个网站,基本上是一个摄影和壁纸网站 我已将图像保存在文件夹中(名为 ImageStorage),我正在通过数据库中的图像链接检索图像 我在我的页面(image-viewer.aspx)中使用了 datalist 来将我的所有图像显示为 imagebutton。我基本上想要的是,当用户单击图像时,它会重定向到另一个页面(image-detail.aspx),并且还会发送该图像的 url 或 id,以便我可以使用它在 (image-detail.aspx) 上显示该图像。 aspx) 页面

这是我的 image_viewer.aspx 代码:

<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" CellPadding="15" CellSpacing="10" DataSourceID="SqlDataSource1" GridLines="Both" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" RepeatColumns="3" RepeatDirection="Horizontal" style="margin-left: 0px" >
<ItemTemplate>
<br />
<asp:ImageButton ID="Image1"  style="width:335px ;height:210px" runat="server" ImageUrl='<%# Eval("ImagePath") %>' CommandName="imgClick"/>
</ItemTemplate>
</asp:DataList>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:photo_stock_viewConnectionString %>" SelectCommand="SELECT * FROM [ImageInfo1]"></asp:SqlDataSource>
<br />
</div>
</form>

image_detail.aspx 中用于显示图像的 HTML 代码:

<img class="img-fluid mt-4" src="ImageStorage/image-detail.jpg"/>

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    你需要后面的代码来处理图片点击事件

    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "imgClick")
        {
           ImageButton btn = e.CommandSource as ImageButton;
           //Do what you need to do here
           string imgUrl = btn.ImageUrl;
           Response.Redirect(imgUrl,true);
    }
    

    如果您需要设置更多信息,例如 imageID,您也可以使用 CommandArgument 作为参数,在 html 中设置并从后面的代码中获取 这是网址how to know which image button I click in datalist

    【讨论】:

    猜你喜欢
    • 2020-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 2021-03-15
    • 2011-11-21
    • 1970-01-01
    相关资源
    最近更新 更多