Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9007179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:37:07+00:00 2026-06-16T01:37:07+00:00

Alright, I have an embarrassingly simple question that hopefully has an embarrassingly simple answer.

  • 0

Alright, I have an embarrassingly simple question that hopefully has an embarrassingly simple answer. I am trying to get an asp:ImageButton to display an alert message that contains variables from my codebehind. Here’s the overly simplified version of the code:

public string AlertMe
{
    get
    {
        return "alert('hi');";
    }
}

Now when I try and access it this way:

<asp:ImageButton ID="btn" runat="server" ImageUrl="/Images/img.ico" OnClientClick='<%=AlertMe%>'/>

I see a postback, but no alert message, as if it can’t access my property at all. Meanwhile, these 2 lines:

<%=AlertMe%><br />
<a onclick="<%=AlertMe%>">click this</a>

both work fine and dandy. (The first displays the code for the javascript alert, and the second fires the alert with no issues.)

So my big question is: why does the OnClientClick event for an asp control fail to register the alert? Is there an obvious flaw I’m missing, or do I really need to register the entire event from code-behind?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:37:08+00:00Added an answer on June 16, 2026 at 1:37 am

    You can’t use the <%= ... %> syntax to set attributes of a Server Control. To set dynamic attributes on Server Controls, you have to set it in code.

    this.btn.OnClientClick = this.AlertMe;
    

    This is because <%= ... %> is a shortcut for Response.Write(). So, it isn’t called until after the aspx has been parsed, evaluated, and rendering has begun. By then it’s too late.

    As an alternative to setting the value in the code behind, you could use this quasi-declarative technique:

    <asp:ImageButton ID="btn" runat="server" ImageUrl="/Images/img.ico" />
    <%
    btn.OnClientClick = <%=AlertMe%>;
    %>
    

    There are other ways to get around it. This answer to a similar question discusses several approaches you can take. I can’t find any “official” reference (eg, msdn) that mentions this specifically, but I didn’t look that hard. Here are a couple of blog posts that discuss the issue as well:

    • Blog post by Jim Black: Server tags cannot contain <% … %> constructs
    • Blog post at weblogs.asp.net: Use ExpressionBuilder To Avoid “Server tags cannot contain <% … %> constructs”

    Or google it with bing for more fun reading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright I have a question that seems simple but I haven't been able to
Alright so I have been trying to get all of my separate classes (At
Alright simple question I have integriters like 5 188 4634 And they all need
Alright so I have a task, that I have to let the client try
Alright, so I have this collision detection code, and I'm trying to push the
Alright I have two problems. I'm using AVAudioPlayer to play a simple audio file
Hey, alright so I have a .plist that looks like; <plist version=1.0> <dict> <key>Item
Alright lets say I have a cPlayer class that inherits from cOrganism , which
Alright, I have a fairly simple design. class Update(models.Model): pub_date = models.DateField() title =
Alright, Question #1: well i have a richtextbox, and id like to add emotions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.