Discussion:
How can I insert a variable into an ASP.NET tag?
(too old to reply)
Flyguy
16 years ago
Permalink
I am trying to insert a variable into an ImageButton’s ImageUrl value.

<asp:ImageButton ID="MyTestButton" runat="server" onclick=" MyTestButton
_Click" ImageUrl="<%=imageUrl%> myimage.gif " />

When I do this I will get this in my html:
src="<%=imageUrl%20%> myimage.gif "

How can I insert a variable into an ASP.NET tag?
Andrew Morton
16 years ago
Permalink
I am trying to insert a variable into an ImageButton's ImageUrl value.
<asp:ImageButton ID="MyTestButton" runat="server" onclick="
MyTestButton _Click" ImageUrl="<%=imageUrl%> myimage.gif " />
src="<%=imageUrl%20%> myimage.gif "
How can I insert a variable into an ASP.NET tag?
Do it in code:
MyTestButton.ImageUrl="whatever"

Using imageUrl for the name of the variable may not be the best idea in the
world when the property you're setting is called ImageUrl.

Andrew
Mark Rae [MVP]
16 years ago
Permalink
Post by Flyguy
I am trying to insert a variable into an ImageButton’s ImageUrl value.
<asp:ImageButton ID="MyTestButton" runat="server" onclick=" MyTestButton
_Click" ImageUrl="<%=imageUrl%> myimage.gif " />
src="<%=imageUrl%20%> myimage.gif "
How can I insert a variable into an ASP.NET tag?
How are you declaring / populating the imageUrl variable server-side?

Also, as Andrew correctly pointed out, imageUrl is a poor choice of variable
name in this case...
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Flyguy
16 years ago
Permalink
I am populating it at the top of my aspx page like so:
<%
String imageUrl = “http://www.mysite.com”;
%>
...
Mark Rae [MVP]
16 years ago
Permalink
Post by Flyguy
Post by Mark Rae [MVP]
Post by Flyguy
How can I insert a variable into an ASP.NET tag?
How are you declaring / populating the imageUrl variable server-side?
<%
String imageUrl = “http://www.mysite.com”;
%>
I tried that, and I get the same behaviour as you.

However, I can confirm Andrew's advice to do this in code, e.g.

<%
String MyImageUrl = "http://www.mysite.com";
MyTestButton.ImageUrl = MyImageUrl + "/myimage.gif";
%>
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Thomas Sun [MSFT]
16 years ago
Permalink
Hi Flyguy,

I agree with Andrew and Mark. It would be better we set ImageButton's
ImageUrl in code.

When we use <%= %>, the expression will be executed and displayed when it
appears in the page. In this case, ImageButton is an ASP.NET Control and
ASP.NET will translate this server control to HTML control firstly. During
this point, the <%= %> will be encoded to "&lt;%=imageUrl%>", so the
property is not displayed.

If we want to use <%= %>, we can use HTML control. For example:
===========================
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

public string imageUrl = "http://www.mysite.com";

protected void Page_Load(object sender, EventArgs e)
{

}

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<img src='<%= imageUrl +"/myimage.gif" %>' />
</div>
</form>
</body>
</html>
=========================
Additionally, we can create a custom CodeExpressionBuilder to use raw code
to assign value to control property if your ASP.NET application is version
2.0. To do so, we need to use the CodeDom's CodeSnippetExpression to
convert the given string into a CodeExpression and then register it in the
web.config expressionBuilders section.

For example:

1. The first step is to create class which inherits ExpressionBuilder
class, and override GetCodeExpression method:
=========================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Compilation;
using System.CodeDom;

/// <summary>
/// Summary description for CustomExpressionBuilder
/// </summary>
[ExpressionPrefix("ExpPre")]
public class CustomExpressionBuilder: ExpressionBuilder
{
public override CodeExpression GetCodeExpression(BoundPropertyEntry
entry, object parsedData, ExpressionBuilderContext context)
{
return new CodeSnippetExpression(entry.Expression);
}
}
========================

2. The second step is registering this custom CodeExpressionBuilder in
web.config:
=======================
<compilation debug="true">
<expressionBuilders>
<add expressionPrefix="ExpPre"
type="CustomExpressionBuilder"/>
</expressionBuilders>
</compilation>
======================

3. The last step is to use this custom CodeExpressionBuilder:
======================
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

public string imageUrl = "http://www.mysite.com";

protected void Page_Load(object sender, EventArgs e)
{

}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Use Custom CodeExpressionBuilder <asp:textbox ID="TextBox1"
runat="server" Text='<% $ ExpPre: DateTime.Now %>'></asp:textbox><br />
Use Custom CodeExpressionBuilder <asp:ImageButton
ID="MyTestButton" runat="server" ImageUrl='<% $ ExpPre: imageUrl+
"/myimage.gif" %>' /><br />
</div>
</form>
</body>
</html>
=======================

For more information about Custom CodeExpressionBuilder example, please
refer to
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionB
uilder.aspx.


I look forward to receiving your test results.


Best Regards,
Thomas Sun

Microsoft Online Partner Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Loading...