Brian Kraemer
Brian Kraemer

Reputation: 453

ASP.NET is not calling C# code

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;
}

Upvotes: 0

Views: 104

Answers (3)

Thomas Taylor
Thomas Taylor

Reputation: 555

<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)

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460108

<%# 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");

Upvotes: 0

Vignesh Kumar A
Vignesh Kumar A

Reputation: 28403

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") %>' />

Upvotes: 0

Related Questions