繁体   English   中英

为什么单击 Aspx 页面中的 ImageButton 仅在第二次有效?

[英]Why clicking an ImageButton in an Aspx page works only at second time?

我的目标是在单击后在 ImageButton 内显示图像。 这仅在我第二次单击按钮时才有效。 我的标记:

   <%@ Page Title="" Language="C#" MasterPageFile="Site.Master" AutoEventWireup="true" 
              CodeBehind="Images.aspx.cs" Inherits="Monitas.Images" %>
     <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
     <style media="all">

       .Sheet {position: relative; left: 15%; top:15%; background: white; width: 800px; 
             height: 900px; border:3px solid #000;}

       .HeaderLogo {position: relative; background: yellow; width:240px; height:100px; left: 
             500px; top: 1px;  border:1px solid #000;}

        </style>

        </asp:Content>
        <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

        <asp:Panel ID="FOGLIO" class="Sheet" runat="server">

            <asp:ImageButton class="HeaderLogo" ID="HEADERLOGO" alternateText="Header Logo" 
                    runat="server" ImageAlign="Middle" OnClick="OnImageHeaderClick" />

       </asp:Panel>

   </asp:Content>

和后面的代码:(OnImageHeaderClick 似乎发送一个 PostBack,但不分配 HeadImage 仅在我第二次单击时,它分配变量)

   protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) // Only second PostBack displays the HeadImage assigned by 
                OnImageHeaderClick
        {
            HEADERLOGO.ImageUrl = HeadImage; // HeadImage is static Global
            Response.Write(HEADERLOGO.ImageUrl);
        }

    }
     // This seems not to be triggered a first time, despite it is declared in the Markup
    protected void OnImageHeaderClick(object sender, ImageClickEventArgs e)
    {
        HeadImage = "Images/FooterLogo.png"; // assign image
    }

我发现了避免第二次点击的方法。 实际上,在 OnImageHeaderClick 期间分配 HeaderLogo.ImageUrl 就足够了,如下代码片段所示:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            HEADERLOGO.ImageUrl = HeadImage;
        }

    }
    protected void OnImageHeaderClick(object sender, ImageClickEventArgs e)
    {
        HEADERLOGO.ImageUrl = HeadImage = "Images/FooterLogo.png"; // use another image for experiment
    }

暂无
暂无

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

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