0

I have got the following code snippet in my page

<asp:UpdatePanel runat="server" ID="UpdatePanel1">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="testButton" />
    </Triggers>
    <ContentTemplate>
        <asp:Image ID="testImage" runat="server" ImageUrl="~/triangle.png" />
    </ContentTemplate>
</asp:UpdatePanel>

<asp:Button runat="server" ID="testButton" Text="Change Image" OnClick="testButton_Click" />

and have got the following in my code behind

protected void testButton_Click ( object sender , EventArgs e ) {
    testImage.ImageUrl = "";
}

When I click on the button, the image is vanished since its URL has been set to empty string in code behind - I understand that. But when I view the page source, I see the original image URL as initialized in the asp page where I badly need URL to be set to empty string.

4
  • 1
    what browser are you using? the view page source button is showing you a new copy of the source from the server most likely (not your current source) Commented May 29, 2016 at 22:38
  • @wal google chrome Commented May 29, 2016 at 22:40
  • 2
    use F12 to view the current source Commented May 29, 2016 at 22:48
  • 1
    A quite similar solution you can find in the below link - stackoverflow.com/questions/37408742/… Commented May 30, 2016 at 2:41

2 Answers 2

1

I have changed the ImageUrl value of your asp image in your markup as below:

<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<Triggers>
    <asp:AsyncPostBackTrigger ControlID="testButton" />
</Triggers>
<ContentTemplate>
    <asp:Image ID="testImage" runat="server" ImageUrl="<%#changeableImageUrl %>" />
</ContentTemplate>

Code Behind:

public string changeableImageUrl;
protected void Page_Load ( object sender , EventArgs e ) {
    changeableImageUrl= "~/triangle.png"; // initial image URL
    this.DataBind (); // since data have been bound in the page by data binding syntax
                      // <%#changeableImageUrl %>
}
protected void testButton_Click ( object sender , EventArgs e ) {
    changeableImageUrl= ""; // to set html-rendered src attribute of img element to ""
    this.DataBind(); //bind data to the page
}

changeableImageUrl must be declared public within the page. Now, you can change changeableImageUrl to change the image URL.

Sign up to request clarification or add additional context in comments.

Comments

1

This is caused because the browser does not reload all source of page but if you change parameter in ScriptManager, In IDE, (not runtime). If you set to false EnablePartialRendering = false; this may solve your problem and always have new html code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.