ASP.NET: casting XPath for bool - possible?

Hello!

I have an XML value that I would like to use as a boolean 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 tried to set the Show value in my XML to "true", "True", "0" but to no avail. Is it possible? My XPath definitely works because I tried to move the <% # (bool) XPath ("Menu / Show")%> outside so that I can see its value, which is correct. I've tried this:

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

      

But this also throws an exception.

Thank.

0


source to share


5 answers


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



+1


source


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



+1


source


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 the most obscure way to convert to boolean?

+1


source


Try the following:

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

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

0


source


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

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

0


source







All Articles