How can I insert a variable into an ASP.NET tag?

F

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 " />

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?
 
A

Andrew Morton

Flyguy said:
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?

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
 
T

Thomas Sun [MSFT]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top