Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 26th, 2010, 05:00 AM
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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:

Code:
  <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>
     </asp:DropDownList>
  </xsl:template>
(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

Code:
<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?
 
Old August 26th, 2010, 05:37 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Would the following work:

Code:
<xsl:attribute name="SelectedValue">&lt;%# "<xsl:value-of select="@volume">"%&gt;</xsl:attribute>
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 26th, 2010, 06:48 AM
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default Alas

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?
 
Old August 26th, 2010, 07:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old August 26th, 2010, 08:27 AM
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by samjudson View Post
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.
 
Old August 26th, 2010, 08:45 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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:

Code:
    <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>
    </asp:DropDownList>
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:

Code:
  ...
  <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>
  ...
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
The Following User Says Thank You to samjudson For This Useful Post:
larrydb (August 26th, 2010)
 
Old August 26th, 2010, 10:42 AM
Registered User
 
Join Date: Aug 2010
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Default

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

Code:
  <xsl:template name="Volume">
    <xsl:value-of select="@volume"/>
    <asp:DropDownList DataSourceID="MySource" DataTextField="volume" DataValueField="volume" ... />
  </xsl:template>
Have you anything up your sleeve that handles this case?





Similar Threads
Thread Thread Starter Forum Replies Last Post
using Dropdown in XSLT amitranjan286 XSLT 11 August 20th, 2009 06:32 AM
Trying to pass SelectedValue of dropdown in querystring. jazzcatone ASP.NET 2.0 Professional 3 May 5th, 2009 07:05 PM
Initial Comments - XSLT 2.0 3rd Ed. baryza Wrox Book Feedback 1 July 9th, 2007 07:18 PM
Unable to set SelectedValue on DropDownList jlrolin ASP.NET 2.0 Professional 0 April 13th, 2007 09:30 AM
Set cbo.selectedvalue using Value problem persists Ron Howerton Visual Basic 2005 Basics 0 July 12th, 2006 11:52 AM





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