简体   繁体   English

单击条件后如何更改ImageButton的图像URL?

[英]How to change the Image URL of an ImageButton after clicking them based on conditions?

I am working on seating layout for a bus booking system using ASP.NET (C#). 我正在使用ASP.NET(C#)进行公交预订系统的座位布局工作。 I want to allow user to select only one seat in a session. 我想允许用户在会话中仅选择一个座位。

I have a list of Image buttons. 我有一个图像按钮列表。 I want that when I click a button then that click event should change its image URL, let's say from "aseat.png" to "sseat.png". 我希望当我单击按钮时,该单击事件应更改其图像URL,例如,从“ aseat.png”更改为“ sseat.png”。 But when I click any of the other buttons then the image URL of the previously clicked button should be back to what it was before and the newly clicked button's image URL should be changed now. 但是,当我单击任何其他按钮时,以前单击的按钮的图像URL应该恢复到以前的状态,现在应更改新单击的按钮的图像URL。

I have created the layout already and when I click on an image button then I am able to change its image URL and change it back again if the same button is clicked, but not been able to make it work like the way I have just described above. 我已经创建了布局,当我单击图像按钮时,我可以更改其图像URL,如果单击同一按钮,则可以再次将其更改回原来的位置,但无法使其像我刚才描述的那样工作以上。

A snippet from my front end code for the layout: 我的前端代码段的布局:

<ul class="row1">    
    <li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat44" ToolTip ="44" Width="25" Height="25"/></li>           
    <li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat39" ToolTip="39" Width="25" Height="25"/></li>
    <li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat34" ToolTip="34" Width="25" Height="25"/></li>
    <li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat29" ToolTip="29" Width="25" Height="25"/></li>
    <li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat24" ToolTip="24" Width="25" Height="25"/></li>
    <li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat19" ToolTip="19" Width="25" Height="25"/></li>
    <li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat14" ToolTip="14" Width="25" Height="25"/></li>
    <li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat9" ToolTip="9" Width="25" Height="25"/></li>
    <li><asp:ImageButton runat="server" ImageUrl="Images/rseat.png"  OnClick="seat1_Click" ID="seat4" ToolTip="4" Width="25" Height="25"/></li>
    <li><asp:ImageButton runat="server" ImageUrl="Images/rseat.png"  OnClick="seat1_Click" ID="seat1"  ToolTip="1" Width="25" Height="25"/></li>
</ul>

C# code: C#代码:

protected void seat1_Click(object sender, ImageClickEventArgs e)
{   
  ImageButton button = (ImageButton)sender;

  if(button.ImageUrl == "Images/aseat.png")
  {
    button.ImageUrl = "Images/sseat.png";
  }
  else
  {
     button.ImageUrl = "Images/aseat.png";
  }
}

Any help would be appreciated. 任何帮助,将不胜感激。

Please try this. 请尝试这个。

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="seat._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<ul class="row1" runat="server" id="Seats">
<li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png" originalUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat44" ToolTip ="44" Width="25" Height="25"/></li>           
<li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png" originalUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat39" ToolTip="39" Width="25" Height="25"/></li>
<li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png" originalUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat34" ToolTip="34" Width="25" Height="25"/></li>
<li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png" originalUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat29" ToolTip="29" Width="25" Height="25"/></li>
<li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png" originalUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat24" ToolTip="24" Width="25" Height="25"/></li>
<li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png" originalUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat19" ToolTip="19" Width="25" Height="25"/></li>
<li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png" originalUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat14" ToolTip="14" Width="25" Height="25"/></li>
<li><asp:ImageButton runat="server" ImageUrl="Images/aseat.png" originalUrl="Images/aseat.png"  OnClick="seat1_Click" ID="seat9" ToolTip="9" Width="25" Height="25"/></li>
<li><asp:ImageButton runat="server" ImageUrl="Images/rseat.png" originalUrl="Images/rseat.png"  OnClick="seat1_Click" ID="seat4" ToolTip="4" Width="25" Height="25"/></li>
<li><asp:ImageButton runat="server" ImageUrl="Images/rseat.png" originalUrl="Images/rseat.png"  OnClick="seat1_Click" ID="seat1"  ToolTip="1" Width="25" Height="25"/></li>
</ul>
</asp:Content>

Default.aspx.cs Default.aspx.cs

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace seat
{
public partial class _Default : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
protected void seat1_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton button = (ImageButton)sender;
        foreach (var item in Seats.Controls)
        {
            if (item.GetType() == typeof(ImageButton) && ((ImageButton)item).ImageUrl == "Images/sseat.png" && ((ImageButton)item).ID != button.ID)
            { 
                ((ImageButton)item).ImageUrl = ((ImageButton)item).Attributes["originalUrl"];
                break;
            }
        }
        if (button.ImageUrl == "Images/sseat.png")
        {
            button.ImageUrl = button.Attributes["originalUrl"];
        }
        else
        {
            button.ImageUrl = "Images/sseat.png";
        }
    }
   }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM