Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 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 February 19th, 2007, 10:36 PM
Registered User
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dropdownlists in DetailsView

I am creating a site based on TheBeerHouse concepts and am developing a page similar to Admin/AddEditArticle.aspx. I want to include an 'Albums' dropdownlist and a 'Photos' dropdownlist on the page. The selectedvalue from Albums is used as a key to populate the following 'Photos' dropdownlist.


In my BLL.Articles.Article class, I add four properties similar to the CategoryID and CategoryTitle fields: AlbumID, AlbumCaption, PhotoID and PhotoCaption. AlbumID is a foreign key in Articles to an Albums class; PhotoID is a foreign key in Articles to a Photos class. The Photos class contains a foreign key AlbumID to the Albums class.

My Article class contains:

ID
CategoryID
CategoryTitle
AlbumID
AlbumTitle
PhotoID
PhotoTitle
...

My Album class contains:

AlbumID
AlbumCaption

My Photo class contains:

PhotoID
AlbumID
PhotoCaption
PhotoUrl

In the database, the Articles table contains

ArticleID
CategoryID
AlbumID
PhotoID
...

The Albums table contains

AlbumID
AlbumCaption

The Photos table contains

PhotoID
AlbumID
PhotoCaption
PhotoUrl

When editing an Article in Admin/AddEditArticle, I have successfully added an ItemTemplate with a dropdownlist that allows the user to choose an Album from the list of Albums by displaying a list of AlbumCaptions that are bound to AlbumIDs.

I then want to have a dropdown list of PhotoCaptions for Photos that is filtered based on the AlbumID selected above. (Actually, will be populated using a query of photos WHERE AlbumID=x.)

I am using ObjectDataSources throughout my app and have created all of the necessary BLL and DAL code. I am relatively new to .NET, however, and am struggling with the User Interface code.

The AddEditArticle detailview includes the following Album template:

  <asp:TemplateField HeaderText="Album"
    InsertVisible="False" SortExpression="AlbumID">
    <ItemTemplate>
      <asp:Label ID="lblAlbum" runat="server"
        Text='<%# Eval("Caption") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
      <asp:DropDownList ID="ddlAlbums" runat="server"
        DataSourceID="objAllAlbums" DataTextField="Caption"
        DataValueField="AlbumID"
        SelectedValue='<%# Bind("AlbumID") %>' Width="100%">
      </asp:DropDownList>
    </EditItemTemplate>
  </asp:TemplateField>

Once the user selects, the appropriate AlbumID, I then want to populate a dropdown list of the PhotoCaptions of the Photos associated with the selected Album.

I assume I need to handle a ddlAlbums_SelectedIndexChanged event, set a value in the detailsview, and then call databind.

I then need to create a DataSource that allows me to call my the GetPhotos method of my Photo BLL class, supplying the selected AlbumID as a parameter.

I've tried a number of combinations, but none of them have worked. Suggestions?

I assume I need something similar to this:

<asp:ObjectDataSource ID="objPhotosByAlbum" runat="server"
  SelectMethod="GetPhotos" TypeName="PhotoManager">
  <SelectParameters>
    <asp:ControlParameter ControlID="ddlAlbums"
      Name="albumID" PropertyName="SelectedValue" Type="Int32" />
  </SelectParameters>
</asp:ObjectDataSource>

Is ControlParameter the correct control? Or simply a Parameter control? How do I map the selectedvalue from the ddlAlbums control to act as input to the ddlPhotos control?

Thanks for any assistance!

Kevin
 
Old February 20th, 2007, 06:18 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

>Is ControlParameter the correct control? Or simply a Parameter control?

Sounds like a job for google.

>How do I map the selectedvalue from the ddlAlbums control to act as input to the ddlPhotos control?

Simple data binding. You might want to enlist google's services again. This is not easy to explain in a message.

Of course, the MS AJAX UpdatePanel would be a sweet solution here. It would prevent an ugly postback.

Eric

 
Old February 20th, 2007, 10:13 AM
Registered User
 
Join Date: Oct 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default


OK, it looks like I have this working. Yes, I agree an Ajax solution would be better, but one step at a time... For now, I'll live with the admittedly ugly postback. If anyone cares to post an Ajax version, I'd love to see it.

The problem turned out to be placement of the ObjectDataSources (ODS) on the page. I followed the model Marco used on Admin/AddEditArticle.aspx and grouped my ODS's at the bottom of the page. Apparently, the controlParameter in the SelectParameters of the ODS could not find the control referenced. It probably looks at all of the controls on the page at the same hierarchy level without parsing through the DetailsView child controls. By moving the ODS inside the DetailsView, everything mapped up.

Although the example I gave in my question was adding images to an Article class, the real-world class is managing Events (as in Theatre, Movies, etc. in Atlanta.) Every event has two photos associated with it: a photo about the event and an image of the title of the event (using the specific event typeface, color, etc.) Both photos for a particular event are always stored in the same Album.

So, here's the markup (postback and all). I did not have to add anything to the code file.


        <asp:TemplateField HeaderText="Album"
          InsertVisible="False"
          SortExpression="AlbumID">
          <ItemTemplate>
            <asp:Label ID="lblAlbum" runat="server"
              Text='<%# Eval("Caption") %>' />
          </ItemTemplate>
          <EditItemTemplate>
            <asp:DropDownList ID="ddlAllAlbums2" runat="server"
              DataSourceID="objAllAlbums2"
              DataTextField="Caption"
              DataValueField="AlbumID"
              SelectedValue='<%# Bind("AlbumID") %>'
              Width="100%"
              AutoPostBack="true">
            </asp:DropDownList>
            <asp:ObjectDataSource ID="objAllAlbums2" runat="server"
              SelectMethod="GetAlbums"
              TypeName="PhotoManager" />
          </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Photo"
          InsertVisible="False"
          SortExpression="PhotoID">
          <ItemTemplate>
            <asp:Label ID="lblPhoto" runat="server"
              Text='<%# Bind("PhotoID") %>' />
          </ItemTemplate>
          <EditItemTemplate>
            <asp:DropDownList ID="ddlAllPhotosByAlbum2"
              runat="server"
              DataSourceID="objAllPhotosByAlbum2"
              DataTextField="Caption"
              DataValueField="PhotoID"
              SelectedValue='<%# Bind("PhotoID") %>'
              Width="100%">
            </asp:DropDownList>
            <asp:ObjectDataSource ID="objAllPhotosByAlbum2"
              runat="server"
              SelectMethod="GetPhotos"
              TypeName="PhotoManager">
              <SelectParameters>
                <asp:ControlParameter
                  ControlID="ddlAllAlbums2"
                  DefaultValue="1"
                  Name="AlbumID"
                  PropertyName="SelectedValue"
                  Type="Int32" />
              </SelectParameters>
            </asp:ObjectDataSource>
          </EditItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="TitlePhoto"
          InsertVisible="False"
          SortExpression="TitlePhotoID">
          <ItemTemplate>
            <asp:Label ID="lblTitlePhoto" runat="server"
              Text='<%# Bind("TitlePhotoID") %>' />
          </ItemTemplate>
          <EditItemTemplate>
            <asp:DropDownList ID="ddlAllTitlePhotosByAlbum"
              runat="server"
              DataSourceID="objAllPhotosByAlbum2"
              DataTextField="Caption"
              DataValueField="PhotoID"
              SelectedValue='<%# Bind("TitlePhotoID") %>'
              Width="100%">
            </asp:DropDownList>
          </EditItemTemplate>
        </asp:TemplateField>



Kevin





Similar Threads
Thread Thread Starter Forum Replies Last Post
Callback in 3 DropDownLists Paula222 ASP.NET 2.0 Professional 3 December 13th, 2006 08:53 AM
Databinding 3 Dropdownlists rit01 ASP.NET 2.0 Basics 1 May 30th, 2006 10:47 AM
dislaying dropdownlists dynamically ashokparchuri General .NET 1 March 1st, 2005 08:04 AM
DropDownLists venterjo General .NET 1 January 8th, 2005 09:10 AM





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