locked
IHttpModule replace image src not work. RRS feed

  • Question

  • User-27405682 posted

    I debug saw on control the image src contain the value which I want, and i able to retrieve the user Control value. But i not sure why when display out it not able to work. even i try profile_img.Controls.Clear();

     <div class="profile-btn" data-toggle="modal" data-target="#modal-login" id="btnProfile" runat="server" >
          <img id="profile_img" runat="server" src="#" alt="" title="login" style="width: 44px;border-radius:50%;">
          
       </div>
     public void Init(HttpApplication context)
            {
                context.PostAcquireRequestState += new EventHandler(context_PostAcquireRequestState);
            }
    
    void context_PostAcquireRequestState(object sender, EventArgs e)
            {
                HttpApplication application = (HttpApplication)sender;
     Page page = application.Context.Handler as Page;
                    page.PreRender += OnPreRender;
    }
    
    
     private void OnPreRender(object sender, EventArgs args)
            {
    
                Page page = sender as Page;
              
    
                var myControl = (login)((Page)HttpContext.Current.Handler).LoadControl("~\\user_control\\Login.ascx");
    //Able to get the control
                HtmlImage profile_img = myControl.FindControl("profile_img") as HtmlImage;
    profile_img.Src = "asdsdasdasdasd";
    
    // error when get the control
                HtmlGenericControl btnProfile = myControl.FindControl("btnProfile") as HtmlGenericControl;
    }

    Tuesday, January 5, 2021 3:38 PM

All replies

  • User-1545767719 posted

    Can the the User Control myControl be obtained at the code var myControl = ... in the event handler OnPreRender?

    If so, did you add the myControl to the page? I cannot see such code in your OnPreRender method.

    Wednesday, January 6, 2021 2:26 AM
  • User-939850651 posted

    Hi FookWah,

    Based on your description and provided code, I am not sure what the page structure looks like?

    Have you tried adding controls to the page?

    I created a simple demo, but I am not sure if this is what you expect:

    Login.ascx
    
    <div class="profile-btn" data-toggle="modal" data-target="#modal-login" id="btnProfile" runat="server">
        <input type="text" name="name" value="some value" />
        <img id="profile_img" runat="server" src="#" alt="" title="login" style="width: 44px; border-radius: 50%;">
    </div>
    Module
    
    public class CustomLoginHttpModule : IHttpModule
        {
            public void Dispose()
            {
    
            }
    
            public void Init(HttpApplication context)
            {
                context.PostAcquireRequestState += new EventHandler(context_PostAcquireRequestState);
            }
    
            void context_PostAcquireRequestState(object sender, EventArgs e)
            {
                HttpApplication application = (HttpApplication)sender;
                Page page = application.Context.Handler as Page;
                if (page != null)
                {
                    page.PreRender += OnRender;
                }
            }
    
            private void OnRender(object sender, EventArgs args)
            {
                Page page = sender as Page;
                if (!page.IsPostBack)
                {
                    var myControl = (Login)((Page)HttpContext.Current.Handler).LoadControl("~\\Login.ascx");
                    //Able to get the control
                    HtmlImage profile_img = myControl.FindControl("profile_img") as HtmlImage;
                    profile_img.Src = "asdsdasdasdasd";
    
                    // error when get the control
                    HtmlGenericControl btnProfile = myControl.FindControl("btnProfile") as HtmlGenericControl;
    
                    page.Controls.Add(myControl);
                }
            }
        }

    I use an empty page for testing and result:

    If I misunderstood something, could you provide sample code for the page?

    Best regards,

    Xudong Peng

    Wednesday, January 6, 2021 7:03 AM
  • User-1545767719 posted

    See the following codes as a sample. I confirmed that these work in my environment to show the image as expected.

    Http Module

    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1.HttpModules
    {
        public class MyModule1 : IHttpModule
        {
            
            public void Dispose()
            {
            }
    
            public void Init(HttpApplication context)
            {
                context.PostAcquireRequestState += Context_PostAcquireRequestState;
            }
    
            private void Context_PostAcquireRequestState(object sender, EventArgs e)
            {
                HttpRequest request = HttpContext.Current.Request;
    
                if (request.CurrentExecutionFilePath.ToLower() != "/myapp/about.aspx")
                {
                    return;
                }
    
                HttpApplication application = (HttpApplication)sender;
                Page page = application.Context.Handler as Page;
                if (page != null)
                {
                    page.PreRender += Page_PreRender;
                }
            }
    
            private void Page_PreRender(object sender, EventArgs e)
            {
                Page page = sender as Page;
                if (page != null)
                {
                    Control myControl = page.LoadControl("WebUserControl1.ascx");
                    HtmlImage profile_img = myControl.FindControl("profile_img") as HtmlImage;
                    if (profile_img != null)
                    {
                        profile_img.Src = "~/images/sample.jpg";
                    }
    
                    // the following code cannot find the PlaceHolder
                    //Control ctrl = page.FindControl("PlaceHolder1");
    
                    Control ctrl = FindControlRecursive(page, "PlaceHolder1");                
                    PlaceHolder placeHolder = ctrl as PlaceHolder;
                    if (placeHolder != null)
                    {
                        placeHolder.Controls.Add(myControl);
                    }
                }           
            }
    
            protected Control FindControlRecursive(Control root, string id)
            {
                if (root.ID == id)
                {
                    return root;
                }
    
                foreach (Control ctrl in root.Controls)
                {
                    Control foundCtrl = FindControlRecursive(ctrl, id);
                    if (foundCtrl != null)
                    {
                        return foundCtrl;
                    }
                }
                return null;
            }
        }
    }

    web.config

    <system.webServer>
        <modules>
            <remove name="MyModule1"/>
            <add name="MyModule1" type="WebApplication1.HttpModules.MyModule1"/>
        </modules>
    <system.webServer>

    User Control sample (WebUserControl1.ascx)

    <%@ Control Language="C#" AutoEventWireup="true" 
        CodeBehind="WebUserControl1.ascx.cs" 
        Inherits="WebApplication1.WebUserControl1" %>
    
    <h3>WebUserControl1</h3>
    
    <div class="profile-btn" data-toggle="modal" data-target="#modal-login" id="btnProfile" runat="server" >
        <img id="profile_img" runat="server" src="#" alt="" title="login" style="width: 44px;border-radius:50%;">      
    </div>
    
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" />

    Sample aspx page (About.aspx in Site.master)

    <%@ Page Title="About" Language="C#" MasterPageFile="~/Site.Master" 
        AutoEventWireup="true" CodeBehind="About.aspx.cs" 
        Inherits="WebApplication1.About" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
        <h2><%: Title %>.</h2>
        <h3>Your application description page.</h3>
        <p>Use this area to provide additional information.</p>
        
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </asp:Content>

    Wednesday, January 6, 2021 7:09 AM