1

How can I get something like this to work in asp.net

<asp:Panel Visible="<%(SType==switch_type.Trunk).ToString()%>" runat="server">Tickle</asp:Panel>

Where switch_type is an enum of values and SType is a get/set in the codebehind.

I have this working, but I just feel it is ugly

<% if (SType == switch_type.Trunk)
    { %>
        ...

I know I can set the panel as visible/invisible in the codebehind, but there are going to be a lot of panels and it just seems easier to set the visibility in the aspx file.

4 Answers 4

2

Behold, the power of Events!

ASPX side:

<asp:panel runat="server" id="myPnlName OnLoad="panelLoadEvent" Tooltip='<% Response.Write(switch_type.Trunk) %>'>
    Stuff
</asp:panel>

Code Side:

protected sub panelLoadEvent(sender as object, e as EventArgs)
  dim pnl as Panel = sender 
  dim oItem as switch_type = ctype(pnl.ToolTip, switch_type) 
  pnl.visibile = iif(stype=oItem,true,false)
End sub 

The point is you put the VALUE you want to check into the tooltip of the panel, and every panel gets processed by the same LoadEvent handler as defined in the OnLoad attribute of the Panel aspx declaration.
At that point you check to see if the given value matches your variable, and set the visibility appropriately.

EDIT If you want to store a string representation in the tooltip as opposed to the underlying int of the enum, you can parse it back to the enum using something like:

[Enum].Parse(System.Type, Value)
Sign up to request clarification or add additional context in comments.

Comments

0

How about this

<asp:Panel runat="server" Visible="<%= SType == switch_type.Trunk %>">
    Stuff
</asp:Panel>

2 Comments

Aye, that's what I was thinking. Without the shorthand = operator in front the chances are it will do the evaluation but not actually output it.
I tried this, but I get this: ... Parser Error Message: Cannot create an object of type 'System.Boolean' from its string representation '<%= SType == switch_type.Five_E %>' for the 'Visible' property.
0

As there's a lot of panels is it worth creating a dataset that you can bind to from a Repeater and you will then be able to use the <%# %> syntax to perform your visible invisible logic?

1 Comment

that won't work, because the panels have different sets of data in them.
-1

You could use a MultiView control, and put each panel in one of the views.

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.