p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   Set asp:dropdown initial SelectedValue with XSLT? (http://p2p.wrox.com/showthread.php?t=80682)

larrydb August 26th, 2010 05:00 AM

Set asp:dropdown initial SelectedValue with XSLT?
[Sorry if this isn't the right forum; my question involves both ASP and
XSLT and I wasn't sure where to post.]

I'm trying to initialize the value of a dropdown list in an XSLT
context. Simplified example: A table showing, for each channel, the
current volume and a dropdown for changing the volume. Template:


  <xsl:template name="Volume">
    <xsl:value-of select="@volume"/>
    <asp:DropDownList ... >
        <asp:ListItem Value="-1">choose volume</asp:ListItem>
        <asp:ListItem Value="0">0</asp:ListItem>
        <asp:ListItem Value="1">1</asp:ListItem>
        <asp:ListItem Value="2">2</asp:ListItem>
        . . .
        <asp:ListItem Value="9">9</asp:ListItem>
        <asp:ListItem Value="10">10</asp:ListItem>
        <asp:ListItem Value="11">11</asp:ListItem>

(This template is used via call-template within a <table> that handles
one channel per row; irrelevant details have been suppressed.)

As it stands, the dropdown will initially show "choose volume". How can
it be made to start out showing the current @volume? One might add


<xsl:attribute name="SelectedValue"><xsl:value-of select="@volume"></xsl:attribute>
just before the first ListItem, but attribute SelectedValue "cannot be
set declaratively". Googling gives some workarounds using
SelectedValue='<%# Bind("whatever")%>' or the like but I haven't
stumbled on the right way to apply this idea in XSLT context. What's
the answer?

samjudson August 26th, 2010 05:37 AM

Would the following work:


<xsl:attribute name="SelectedValue">&lt;%# "<xsl:value-of select="@volume">"%&gt;</xsl:attribute>

larrydb August 26th, 2010 06:48 AM

First, your suggestion is broken because the xsl:value-of tag is not closed. You are forgiven since you're just perpetuating the same error from the original post! I'll pretend that ">" is replaced by "/>" in both posts if you'll do the same.

In any case, it doesn't work. The resulting behavior is just as though your line were not present at all. I tried a couple other combinations like this and nothing worked.

Clever idea, though. Anything else I might try? Should I post this query to another forum, e.g. xslt?

samjudson August 26th, 2010 07:00 AM

No, no more ideas. I think what you need to try and do is work out what your final ASP.Net page should look like. i.e. What can you put in the SelectedValue attribute to make it work?

If you can do that, then we can tell you what the XSLT should be to produce the required ASP.Net, but otherwise we are stuck.

larrydb August 26th, 2010 08:27 AM


Originally Posted by samjudson (Post 262444)
No, no more ideas. I think what you need to try and do is work out what your final ASP.Net page should look like. i.e. What can you put in the SelectedValue attribute to make it work?

That question is, of course, exactly what I'm asking. Indeed, that's why I posted in an ASP forum, not an XSLT forum---the question is what the XML should look like, given the mechanisms at hand.

Perhaps you can suggest a different forum or website where I could pose this question.

samjudson August 26th, 2010 08:45 AM

Apologies, you're right - I got confused and though I was the XSLT forum again (which to be fair I mostly am).

I think you need to output XML like this:


    <asp:DropDownList runat="server" ID="DropDownList1">
        <asp:ListItem Value="1">1</asp:ListItem>
        <asp:ListItem Value="2" Selected="True">2</asp:ListItem>
        <asp:ListItem Value="3">3</asp:ListItem>
        <asp:ListItem Value="4">4</asp:ListItem>
        <asp:ListItem Value="5">5</asp:ListItem>
        <asp:ListItem Value="6">6</asp:ListItem>

The question is how best to do this easily and cleanly.

If this was XSLT 2 then you could do a simple loop on the range, but as I suspect you are probably in XSLT 1 then it will probably just have to be a check after each list item:


  <asp:ListItem Value="1"><xsl:if test="@volume=1"><xsl:attribute name="Selected">True</xsl:attribute>1</xsl:if></asp:ListItem>

  <asp:ListItem Value="2"><xsl:if test="@volume=2"><xsl:attribute name="Selected">True</xsl:attribute>2</xsl:if></asp:ListItem>

larrydb August 26th, 2010 10:42 AM

Excellent, thank you very much; this does indeed solve the problem. (Typo in your solution: The actual list item value must go outside, not inside, the <xsl:if>.)

Now for the daily double: What if the items aren't listed explicitly? That is, what if we were starting with something like


  <xsl:template name="Volume">
    <xsl:value-of select="@volume"/>
    <asp:DropDownList DataSourceID="MySource" DataTextField="volume" DataValueField="volume" ... />

Have you anything up your sleeve that handles this case?

All times are GMT -4. The time now is 12:30 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.