Tuesday, April 5, 2011

How to persist attributes of ASP.NET User controls

I have a ASP.NET user control which hosts a 'HtmlImage'. The src attribute is successfully set at run-time, but adding the rendered control to another container causes a loss of the src attribute.

The rendered control is stored in Session (I know this is not ideal). Then a redirect is done to another page which uses the control in Session.

Perhaps it's because the url is not encoded?

Code:

<img id="ctlImage" runat="server" style="border-style: none;" />

ctlImage.Src = String.Format("..\image.aspx?{0}", "...")
From stackoverflow
  • Have you tried using an asp:Image control instead of a plain old img tag?

    vanslly : I did not really want to head down the road of using the asp:Image control. I seem to now be forced to, but still I'm keen to find the reason I'm experience the problem described. To answer your question: Yes I have and it works.
  • It could be because the attributes of HtmlControls (including HtmlImage) are stored in ViewState. It seems reasonable that ViewState wouldn't get sent along to the other page.

    Instead of storing the control in Session, why not just store the generated html code? Much more lightweight.

    vanslly : Why is it reasonable for ViewState to be dropped? Because this behavior is not observed with using an asp:Image? The reason for not streaming the generated html is because the page which uses the control needs to massage the controls for printing. I do see the alternative of pre-messaging :)

0 comments:

Post a Comment