0

For some reason this following ASP code is not calling the c# method "checkLastNames" that is suppose to be evaluated in the 'Visible=' field below.

<asp:Button ID="btnZ" 
            runat="server" 
            Text="Z" 
            Height="20px" 
            Width="25px" 
            Font-Size="12px" 
            CommandArgument="Z" 
            OnClick="btnA_Click" 
            Visible='<%# checkLastNames("Z") %>' />

When I enter debug mode the method isn't even being called. Visible just defaults to true. I've tried changing the method to return only false just to see if it would work but "Visible" is still defaulting to true.

protected bool checkLastNames(string s){
    return false;
}
5
  • 4
    You're using the <%# format.. it should be <%= blah %>. # is for databinding. Commented Feb 10, 2014 at 16:43
  • That's the databinding syntax...are you calling DataBind() in the codebehind? Commented Feb 10, 2014 at 16:43
  • No, it's not databound, so that part of the problem. When I try with <%= %> I get an error : Cannot create an object of type 'System.Boolean' from its string representation '<%= checkLastNames("Z") %>' for the 'Visible' property. Commented Feb 10, 2014 at 16:56
  • Why not do this in the codebehind? It's much cleaner: btnZ.Visible = checkLastNames("Z"); Commented Feb 10, 2014 at 16:58
  • Yes, you're right. It is cleaner. I'm doing that now. Thank you! Commented Feb 10, 2014 at 17:05

3 Answers 3

1
<asp:Button ID="btnZ" 
        runat="server" 
        Text="Z" 
        Height="20px" 
        Width="25px" 
        Font-Size="12px" 
        CommandArgument="Z" 
        OnClick="btnA_Click" 
        Visible='<%# checkLastNames("Z") %>' />

That # means it's only evaulated during a databind operation. So if you are not databinding the page explicitly (through calling DataBind()) then this won't show.

        Visible='<%= checkLastNames("Z") %>' />

You might want to try the code above. Also I would probably put this in a static function (assuming it's functionality is encapsulated in there)

Sign up to request clarification or add additional context in comments.

Comments

0

<%# is for databinding expressions, so this works only if the namingcontainer control of this Button is databound.

For example in Page_Load:

this.DataBind();

But why not using codebehind in the first place?

btnZ.Visible = checkLastNames("Z");

1 Comment

Thanks. I'm getting an error when I change the code (Cannot create an object of type 'System.Boolean' from its string representation '<%= checkLastNames("Z") %>' for the 'Visible' property.) In any case, I'll just do it in code behind. Thank you.
0

Try like this

<asp:Button ID="btnZ" 
            runat="server" 
            Text="Z" 
            Height="20px" 
            Width="25px" 
            Font-Size="12px" 
            CommandArgument="Z" 
            OnClick="btnA_Click" 
            Visible='<%= checkLastNames("Z") %>' />

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.