Concatenate DropDownList display data (XML Data Source)

Last post 09-27-2007 11:58 AM by Superfly. 7 replies.

Sort Posts:

  • Concatenate DropDownList display data (XML Data Source)

    09-26-2007, 12:08 PM
    • Loading...
    • Superfly
    • Joined on 01-31-2006, 6:42 AM
    • Posts 38

    Hi there,

    I have an asp:DropDownList control, which is bound to a XML Data Source ("XmlDataMobile").


    The XML Document ("Mobile.xml"):

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <mob_numbers>
     <mob_number user="John Smith" project="ICT" number="07000000001"/>
     <mob_number user="Anne Jones" project="ICT" number="07000000002"/>
     <mob_number user="Jack Scott" project="ICT" number="07000000003"/>
     <mob_number user="Jane Lewis" project="ICT" number="07000000004"/>
    </mob_numbers>


    "User" is the field being displayed in the DropDownList (DataTextField), and the "number" is the value (DataValueField).  So the DropDownList currently looks like this:

    John Smith
    Anne Jones
    Jack Scott
    Jane Lewis


    I would like the DropDownList, which is called "ddlRecipient", to look like this:

    John Smith, ICT, 07000000001
    Anne Jones, ICT, 07000000002
    Jack Scott, ICT, 07000000003
    Jane Lewis, ICT, 07000000004


    What would be the best way to achieve this concatenation?


    I am "dragging and dropping" using Visual Web Developer Express Edition 2005 (I've a basic knowledge of C#!).  Any help would be greatly appreciated!

    Cheers,
    Stewart

  • Re: Concatenate DropDownList display data (XML Data Source)

    09-26-2007, 3:29 PM
    Answer
    • Loading...
    • k2schreck
    • Joined on 03-03-2006, 1:19 PM
    • NJ
    • Posts 43

     Hi Stewart,

    This site has some good examples for these kind of things found in the QuickStart section.

    To answer your question you can use an xslt file to transform your data and it works conveniently with the xml data souce.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <users>
    <xsl:for-each select="mob_numbers/mob_number">
    <item>
    <xsl:attribute name="text">
    <xsl:value-of select ="@user"/>, <xsl:value-of select ="@project"/>, <xsl:value-of select ="@number"/>
    </xsl:attribute>
    <xsl:attribute name="value">
    <xsl:value-of select ="@number"/>
    </xsl:attribute>
    </item>
    </xsl:for-each>
    </users>
    </xsl:template>
    </xsl:stylesheet>

    This transform you xml file into:

    <users>
    <item text="John Smith, ICT, 07000000001" value="07000000001"></item>
    <item text="Anne Jones, ICT, 07000000002" value="07000000002"></item>
    <item text="Jack Scott, ICT, 07000000003" value="07000000003"></item>
    <item text="Jane Lewis, ICT, 07000000004" value="07000000004"></item>
    </users>

    I saved this xsl file as DropDownConverter.xsl. Then in your aspx web page configure the XML DataSource to reference this file in the "Transform" section and adjust your dropdownlist is look for "text" and "value".

    <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="XmlDataSource1" 
        DataTextField="text" DataValueField="value"></asp:DropDownList>
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Users.xml" 
        TransformFile="~/DropDownConverter.xsl"></asp:XmlDataSource>
    

    This should get you going. Let us know how you make out.

    Best regards,
    Kurt
  • Re: Concatenate DropDownList display data (XML Data Source)

    09-27-2007, 6:16 AM
    • Loading...
    • Superfly
    • Joined on 01-31-2006, 6:42 AM
    • Posts 38

    Hi Kurt,

    That works perfectly - thanks for your help! Big Smile

    All the best,
    Stewart

  • Re: Concatenate DropDownList display data (XML Data Source)

    09-27-2007, 7:57 AM
    • Loading...
    • Superfly
    • Joined on 01-31-2006, 6:42 AM
    • Posts 38

    Just one more thing - hope you don't mind!  If I wanted to add an extra value at the top of the list, i.e. "Select one", what is the most effective way of doing this?

    Best Regards,
    Stewart

  • Re: Concatenate DropDownList display data (XML Data Source)

    09-27-2007, 9:38 AM
    • Loading...
    • k2schreck
    • Joined on 03-03-2006, 1:19 PM
    • NJ
    • Posts 43

    I'm glad this is working for you. As for the "Select One", you can either add an addition user to your datasource or you can revise the xslt to include that option. The previous would create a name of "Select One,," in the dropdown assuming you leave the project and number attributes empty. That looks ugly so I'd suggest revising the xslt, however you'll need to work a little for this one Stick out tongue. You have all the info you need to edit the xslt file.

    Your goal is xml that looks like: 

    <users>
        <item text="Select One" value="0"></item>
        <item text="John Smith, ICT, 07000000001" value="07000000001"></item>
        <item text="Anne Jones, ICT, 07000000002" value="07000000002"></item>
        <item text="Jack Scott, ICT, 07000000003" value="07000000003"></item>
        <item text="Jane Lewis, ICT, 07000000004" value="07000000004"></item>
    </users>

    The first item is always the default item in an html <select>. I chose a value of 0 but you can use what ever you like. I have found that an empty value (value="") can be inconsistent when performing validation on a dropdownlists.

    Ok now in the xslt file .... 

    1    <users>
    2        <xsl:for-each select="mob_numbers/mob_number">
    3          <item>
    4            <xsl:attribute name="text">
    5              <xsl:value-of select ="@user"/>, <xsl:value-of select ="@project"/>, <xsl:value-of select ="@number"/>
    6            </xsl:attribute>
    7            <xsl:attribute name="value">
    8              <xsl:value-of select ="@number"/>
    9            </xsl:attribute>
    10         </item>
    11     </xsl:for-each>
    12   </users>
    

    This is the bulk of where things are happening. You want the "Select One" to appear as the first item. You can see the root of the final xml data in line 1 <users> and the repeat group starting on line 2 <xsl:for-each>. The "Select One" item will be entered between these two lines.

    Hint: Since you are not transforming any data from your original xml source to create the "Select One" item option, you just hard code it.

    Can't do all the work for you Wink. If you have any problems or I did not explain things well don't hesitate to let me know.

     

    Best regards,
    Kurt
  • Re: Concatenate DropDownList display data (XML Data Source)

    09-27-2007, 11:27 AM
    • Loading...
    • Superfly
    • Joined on 01-31-2006, 6:42 AM
    • Posts 38

    Hi Kurt, thanks for the advice.  This done the job: 


    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="/">
        <users>
          <item text="-- Select One --" value="0"></item>
          <xsl:for-each select="mob_numbers/mob_number">
            <item>
              <xsl:attribute name="text">
                <xsl:value-of select ="@user"/>, <xsl:value-of select ="@project"/> (<xsl:value-of select ="@number"/>)
              </xsl:attribute>
              <xsl:attribute name="value">
                <xsl:value-of select ="@number"/>
              </xsl:attribute>
            </item>
          </xsl:for-each>
        </users>
      </xsl:template>
    </xsl:stylesheet>


    Best Regards,
    Stewart

  • Re: Concatenate DropDownList display data (XML Data Source)

    09-27-2007, 11:37 AM
    • Loading...
    • k2schreck
    • Joined on 03-03-2006, 1:19 PM
    • NJ
    • Posts 43

     Excellent, glad I could help. Smile Take care!

    Best regards,
    Kurt
  • Re: Concatenate DropDownList display data (XML Data Source)

    09-27-2007, 11:58 AM
    • Loading...
    • Superfly
    • Joined on 01-31-2006, 6:42 AM
    • Posts 38

    Thanks Kurt, you too.

    Cheers,
    Stewart

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