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

Discussion in 'ASP .Net' started by Flyguy, Mar 24, 2009.

  1. Flyguy

    Flyguy Guest

    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?
     
    Flyguy, Mar 24, 2009
    #1
    1. Advertising

  2. Flyguy wrote:
    > 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
     
    Andrew Morton, Mar 24, 2009
    #2
    1. Advertising

  3. Flyguy

    Flyguy Guest

    I am populating it at the top of my aspx page like so:
    <%
    String imageUrl = “http://www.mysite.comâ€;
    %>


    "Mark Rae [MVP]" wrote:

    > "Flyguy" <> wrote in message
    > news:...
    >
    > > 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?

    >
    > 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, Mar 24, 2009
    #3
  4. 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.
     
    Thomas Sun [MSFT], Mar 25, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sergey Morkovkin
    Replies:
    5
    Views:
    17,471
    gamcfarlane
    Jul 14, 2009
  2. shruds
    Replies:
    1
    Views:
    933
    John C. Bollinger
    Jan 27, 2006
  3. Sara T.
    Replies:
    1
    Views:
    198
    JuanDG
    Oct 25, 2004
  4. Sergey Morkovkin

    Insert SCRIPT or LINK tag into a HEAD section DYNAMICALLY

    Sergey Morkovkin, Sep 11, 2003, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    218
    Slava Tihonyuk
    Sep 15, 2003
  5. Sergio del Amo
    Replies:
    4
    Views:
    266
Loading...

Share This Page