簡體   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