0

I'm trying to pass a variable into my "asp:Button CommandArgument" - I know that I've got to jump through some hoops as runat=server is set, but I have tried everything I have found so far, but to no avail

I know that my code behind works nicely as, if I explicitly define the variables in my "asp:Button CommandArgument" everything works - but as soon as I try to insert a variable via <% %> syntax? The variable is NOT replaced by the string it represents

I'm using vStudio 2019 community preview (and 2017) as my IDE, targeting .net framework 4.7.2 - everything builds cleanly, and I know that the value for the variable is being set - it's jut not making it into my "asp:Button CommandArgument" and I need it to

I've tried everything I have found - all permutations of the <% $> syntax I can find, tried using CodeExpressionBuilder

The pertinent part of my problematic code is

<% if (variable is "value") { %>

<asp:Button ID = "button1" 
    runat = "server" 
    Text = "Processing '<%: variable %>' Records" 
    CommandArgument = '<%: variable %>' 
    OnCommand = "button1_Click" 
/>

<% } %>

I expect the variable I am inserting into the "asp:Button CommandArgument" to be replaced by it's current value

What I get? Various error messages and the literal translation of my ' or " wrapped <%: <%# <%= variable %> which doesn't help

I know I can hard-code some of what I want to accomplish - but that's not optimal, and doesn't help me when I want to pass variable dates to my procedures via "asp:Button CommandArgument"

I know I must be doing something wrong - but cannot, for the life of me, figure out what

Any suggestions?

2
  • I noticed that the CommandArgument part is using single quotes around what would be the value. Should those be double quotes like the other attributes? Does the Text attribute get the correct value? And where/how is "variable" defined? Perhaps it's a scope issue? Commented Mar 29, 2019 at 12:29
  • Use a Repeater Control to add controls and use DataBinding Expressions to dynamically add the text to the conrol. Commented Mar 29, 2019 at 13:30

1 Answer 1

2

ASP.NET will not allow you to declare controls like this:

<asp:Button ID = "button1" 
    runat = "server" 
    Text = "Processing '<%: variable %>' Records" 
    CommandArgument = '<%: variable %>' 
    OnCommand = "button1_Click" 
/>

The reason is that <asp:Button … /> is pre-processed by the interpreter before any inline statements (eg. <%: variable %>).

To set/modify properties of an ASP.NET form control, you have to change them in code-behind. The most popular place would be in the Page_Load() event. Like this:

void Page_Load(object sender, EventArgs e)
{
   if (!Page.IsPostBack)
   {
       button1.Text = "Processing " + variable + " Records"; 
       button1.CommandArgument = variable;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.