开发者

input losing src

开发者 https://www.devze.com 2022-12-18 12:42 出处:网络
If I use the following code without runat=\"server\" the input\'s src works fine and I see the image coming through.

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 开发者_Go百科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


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


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


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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消