1

If I use the following code without runat="server" the input's src works fine and I see the image coming through.

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

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

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

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

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

PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
  • And no, I do not want to use an ImageButton or any MS web control. I want to figure out how to get this property AND that onserverclick both to work – PositiveGuy Feb 01 '10 at 15:01
  • I'm phasing out the use of ASP.NET web controls in our current ASP.NET based app. We can't move to MVC right now so I'm figuring out how to do this with standard HTML controls so this is a good example of a brick wall I'm hitting while in classic ASP.NET..trying to get the property AND the server-side even handler working. – PositiveGuy Feb 01 '10 at 15:02
  • 1
    I don't really see what you're hoping to achieve here by working against the web forms model in an ASP.NET website? I don't think this will help with understanding ASP.NET MVC. – Andy Rose Feb 01 '10 at 15:55
  • Care to give a reason as to why you want to do this? – Mattias Jakobsson Feb 07 '10 at 19:46

4 Answers4

12

You cannot use the <%= %> syntax with server controls (including standard HTML elements with runat="server"). You have two choices:

  • Access the control in the code behind (as an HtmlInputControl) and assign the src attribute using the Attributes property: imageControl.Attributes["src"] = value;
  • Assign the attribute using the databinding syntax (src="<%# %>") and call imageControl.DataBind() from the code behind
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
2

Maybe I'm missing something. But runat server tag does not support code expression.

m3kh
  • 7,881
  • 2
  • 31
  • 37
  • I need to be able to run that onserverclick method AND have the reference to the property TestButtonImageUrl also work at the same time – PositiveGuy Feb 01 '10 at 15:00
1

When you add runat="server to that html tag, Asp.Net converts it from string to HtmlControl - in this case of type HtmlInputImage. You can see this happen by adding:

<%= testButton.GetType() %>

Then the only thing you need to do is set the Src-property, which, contrary to other comments, you CAN do in inline aspx - no need for a code-behind file:

<%
    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 bit non-intuitive, the reason is that the code is run at render-time, so if the setting of Src-property is after the control, it is too late.

Ope
  • 777
  • 1
  • 7
  • 13
0

if jQuery is an option than you could try this:

<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 a HttpHandler with processing like this

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

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

<div><input id="testButton" runat="server" type="image" src="image.img" onserverclick="RedirectTest" /></div>
dh.
  • 1,501
  • 10
  • 9