Input loss src

If I use the following code without runat = "server", the src input works fine and I see how the image goes.

<div><input id="testButton" type="image" src="<%=TestButtonImageUrl %>" onserverclick="RedirectTest" /></div>

url https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image

But if I put runat = "server", for some reason I get this for the URL:

<div><input id="testButton" type="image" src="<%=TestButtonImageUrl %>" onserverclick="RedirectTest" runat="server" /></div>

url http: // localhost /% 3C% = lButtonImageUrl% 20 %% 3E

+1
source share
4 answers

The syntax <%= %>cannot be used with server controls (including standard HTML c elements runat="server"). You have two options:

  • Access the control in the code behind (as HtmlInputControl) and assign the src attribute using the Attributesproperty:imageControl.Attributes["src"] = value;
  • (src="<%# %>") imageControl.DataBind()
+12

, - . runat .

+2

runat = " html, Asp.Net HtmlControl - HtmlInputImage. , :

<%= testButton.GetType() %>

, , Src, , , inline aspx - :

<%
    testButton.Src = "/content/logo.png";
%>
<input id="testButton" type="image" runat="server" src="" onserverclick="RedirectTest" />

You need to set the Src property BEFORE the actual input, which is a little unintuitive, the reason is that the code is executed during rendering, so if the Src-property parameter is after the control, it's too late.

+1
source

if jQuery is an option you could try:

<script type="text/javascript">
  $(function() { $('#<%=testButton.ClientID %>').attr('src', '<%=TestButtonImageUrl %>'); });
</script>
...
<div><input id="testButton" runat="server" type="image" onserverclick="RedirectTest" /></div>

Update: Another option is to create an HttpHandler with processing similar to this.

public void ProcessRequest(HttpContext context)
{
  var testButtonImageUrl = "https://fpdbs.paypal.com/dynamicimageweb?cmd=_dynamic-image";
  context.Response.Redirect(testButtonImageUrl);
}

add web.config to the path to handle image.img or whatever and update aspx

<div><input id="testButton" runat="server" type="image" src="image.img" onserverclick="RedirectTest" /></div>
0
source

All Articles