views:

364

answers:

5

Greetings!

I have an XML value that I'd like to use as a boolean value to toggle the visibility of a Panel. I have something like this:

<asp:FormView id="MyFormView" runat="server" DataSourceID="MyXmlDataSource">
    <ItemTemplate>
        <!-- some stuff -->
        <asp:Panel id="MyPanel" runat="server" Visible='<%# (bool)XPath("Menu/Show") %>'>
        </asp:Panel>
        <!-- some more stuff -->
    </ItemTemplate>
</asp:FormView>
<asp:XmlDataSource id="MyXmlDataSource" runat="sever" DataFile="MyFile.xml" />

However, this throws an exception. I've tried setting the value of Show in my XML to "true", "True", "0", but to no avail. Is this even possible? My XPath definitely works because I've tried moving <%# (bool)XPath("Menu/Show") %> outside so that I can see its value and it is correct. I have tried this:

<%#((bool)XPath("Menu/Show")).ToString() %>

But this also throws an exception.

Thanks.

+2  A: 

Try <%#(Convert.ToBoolean(XPath("Menu/Show"))) %>

Andrew Rollings
A: 

Try this:

Not sure of the syntax of string equality in classic ASP, but you need to do a string compare, then return true or false.

...Visible='<%#XPath("Menu/Show")).ToString().equals("0") ? return true : false%>'...

Byron Whitlock
+1  A: 

if the xpath returns a string, don't you want to use Boolean.Parse(XPath("Menu/Show"))

Andrew Cox
Can Boolean.TryParse() be used?
Bullines
Yes, that should work also.
Andrew Rollings
TryParse would return true if it could parse it - not if the value it parsed was true.
Mark Brackett
How could TryParse be used from within <%# %> ?
Bullines
+1  A: 

Yet another:

System.Xml.XmlConvert.ToBoolean()

Valid strings are "1" or "true" for true and "0" or "false" for false.

Do I get bonus points for having the most obscure way of converting to boolean?

Dan Esparza
A: 

Visible='<%# (XPath("Menu/Show")) as string == "1" ? true : false %>'

beware of null exception. Use "as string" instead of .ToString()