How far can I use <%# XPath("") %> kind of stuff?

Last post 07-17-2007 6:14 AM by Strato. 7 replies.

Sort Posts:

  • How far can I use <%# XPath("") %> kind of stuff?

    07-13-2007, 6:42 AM
    • Loading...
    • Strato
    • Joined on 02-22-2007, 10:08 AM
    • Posts 96

    Hello,

    I'm trying to fill a GridView with some dynamic XmlDocument, and for now, it works pretty well.

    Here is what I have :

     

    //This array of XmlDocument is the result of a search on a database with some proprietary classes
    XmlDocument[] TableauNotices = simImport.XMLDOMDocumentArray(IdentifiantsNotices, false, true, 0, "", true, true, true);
    GridViewCible.DataSource = TableauNotices;
    GridViewCible.DataBind();
      
    <asp:TemplateField ItemStyle-CssClass="CelluleResultat">
        <ItemTemplate>
            <%# "Titre : <em><strong>" + XPath("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/TITRE") + "</strong></em><br />"%>
            <%# "Auteur : <em>" + XPath("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/AUTEUR") + "</em><br />"%>
        </ItemTemplate>
    </asp:TemplateField>

     

    Now what I would like to do is check whether a node of my XmlDocument is empty, so it won't show "Titre : " and nothing after it but just jump this line. This code sample doesn't work :

    <%# (XPath("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/TITRE") == "") ? "" : "Titre : <em><strong>" + XPath("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/TITRE") + "</strong></em><br />"; %>
     

    AND, I would like to be able to display as many lines as there are nodes in a node list. Something like :

     

    <%# foreach (XPath("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/DIFFERENT_TITLE") "Different title : <em><strong>" + XPath("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/DIFFERENT_TITLE") + "</strong></em><br />"; %>

     

    How can I do all of this without destroying too much what I already achieved?

    Thank you for your help.

    Filed under: , ,
  • Re: How far can I use <%# XPath("") %> kind of stuff?

    07-13-2007, 7:34 AM
    • Loading...
    • Nullable
    • Joined on 05-02-2006, 8:01 PM
    • Posts 739

    It's funny, we were just talking about this at work... one of the developers was using the <%# %> stuff... and I was trying to explain that he was painting himself into a corner... He got the "value here"... then the "ooh, I need this or this... Ahh Trinary Operator!"... then it hit... he wanted more :)

    My suggestion to him, and you is the same - Take your sever side code server side :)

    -Timothy Khouri
    http://www.SingingEels.com/
    Developer / Architect / Author
  • Re: How far can I use <%# XPath("") %> kind of stuff?

    07-13-2007, 7:43 AM
    • Loading...
    • Strato
    • Joined on 02-22-2007, 10:08 AM
    • Posts 96

    I was excepting such an answer. The problem is : I'm not very at ease with code behind.

    Could you give just a tiny little example? Or a couple of key words to know what to look after?

  • Re: How far can I use <%# XPath("") %> kind of stuff?

    07-17-2007, 3:09 AM

    Hi:

      You can simply compare it with NULL instead of empty string:

    <%#XPath("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/TITRE")==null?"NULL":"NOT NULL"%>
     

      If your problem isn't solved, please inform us.

    Regards

    Sincerely,
    Allen Chen
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: How far can I use <%# XPath("") %> kind of stuff?

    07-17-2007, 3:13 AM
    Answer

    Hi, Strato:

    Here is an example to show how to call the server-side method in aspx file:

    <%# GetTitre("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/TITRE")%>

    Code-Behind:

        protected string GetTitre(string path)
        {
            string value = XPath(path);
            if (value != "")
            {
                "Titre : <em><strong>" + value + "&lt;/strong></em><br />";
            }
            else
            {
                return "";
            }
        }
     Hope it helps.
    Sincerely,
    Techie Zhang
    Microsoft Online Community Support
  • Re: How far can I use <%# XPath("") %> kind of stuff?

    07-17-2007, 4:55 AM
    • Loading...
    • Strato
    • Joined on 02-22-2007, 10:08 AM
    • Posts 96

    Thank you. It helped a lot.Yes

     

    <asp:TemplateField>
      <ItemTemplate>
        <%# FormateInterpretation("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/TITRE") %>
        <%# FormateInterpretation("INTERPRETATION_OEUVRE/DESCRIPTION/OEUVRE_LIBRE/AUTEUR") %>
      </ItemTemplate>
    </asp:TemplateField>

     

        protected string FormateInterpretation(string Path)
        {
            string TexteNoeud = XPath(Path).ToString();
    
            if (TexteNoeud != string.Empty)
            {
                if (Path.EndsWith("TITRE"))
                    return TexteNoeud = "&#9679; Titre : <span style='font-weight: bold; font-style: italic'>" + TexteNoeud + "&lt;/span><br />";
                else if (Path.EndsWith("AUTEUR"))
                    return TexteNoeud = "&#9674; Auteur : <span style='font-style: italic'>" + TexteNoeud + "&lt;/span><br />";
                else
                    return string.Empty;
            }
            else
                if (Path.EndsWith("TITRE"))
                    return TexteNoeud = "&#9679; Titre : <span style='font-weight: bold; font-style: italic; color: #FF0000'>Pas de titre!</span><br />";
                else if (Path.EndsWith("AUTEUR"))
                    return TexteNoeud = "&#9674; Auteur : <span style='font-style: italic; color: #FF0000'>Pas d'auteur!</span><br />";
                else
                    return string.Empty;
        }
     

    But I have one problem left : how to display as many results as there are nodes matching my XPath expression. Something like that :

     

    <%# foreach (XmlNode Test in XPath("OEUVRE/DESCRIPTION/TITRE_FORME_REJETEE/TITRE_OEUVRE")) { "&#187; Autre titre : <em>" + XPath("OEUVRE/DESCRIPTION/TITRE_FORME_REJETEE/TITRE_OEUVRE") + "</em><br />" } %>
     
  • Re: How far can I use <%# XPath("") %> kind of stuff?

    07-17-2007, 5:42 AM
    Answer

    Hi, Strato:

    Here is an example to show how to use Repreater to display multiple nodes in a FormView.

    http://msdn2.microsoft.com/zh-cn/library/73yw7ttd(VS.80).aspx

    If you don't want to add an additional control, you should use XPathSelect method, like:

      protected string getNodes(string path)
        {
            IEnumerator ie = XPathSelect(path).GetEnumerator();
            string nodes = "";
            while (ie.MoveNext())
            {
                System.Xml.XmlElement element = ie.Current as System.Xml.XmlElement;
                nodes += "Titre : <em><strong>" + element.Value + "&lt;/strong></em><br />";
            }
            return nodes;
        }
     Hope it helps.
    Sincerely,
    Techie Zhang
    Microsoft Online Community Support
  • Re: How far can I use <%# XPath("") %> kind of stuff?

    07-17-2007, 6:14 AM
    • Loading...
    • Strato
    • Joined on 02-22-2007, 10:08 AM
    • Posts 96

    It's PERFECT, except it works better with element.InnerText instead of element.Value.

    Thank you very much.

Page 1 of 1 (8 items)
Microsoft Communities
Page view counter