Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 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 June 19th, 2005, 07:27 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Datagrid nested in Datalist (passing parameters)

Hi

I have a datalist which shows a list of hotel rooms available in a hotel. There is a "Click for Prices" link button, which when clicked, displays the <SelectedItemTemplate> of the datalist. Inside this, is a datagrid, where I want to display all the prices for the hotel room selected. The problem I'm having is that I'm unsure how to pass through the selected "roomID" to the function which populates the prices datagrid.

From some of the reading I have done, I found that I needed to use as OnItemDataBound in my datalist to then be able to reference the dataGrid (because it only appears when the SelectedItemTemplate displays). I have used OnItemDataBound="BindGrid" to reference the DataGrid, and then want to try and pass a CommandArgument through as a parameter to my getprices function.

Here is all the relevant code (which might help explain what i'm going on about):

*************************************************
Sub Page_Load(Sender As Object, E As EventArgs)
  If Not IsPostBack Then
    dlistRooms.DataSource = getRooms()
    dlistRooms.DataBind()
End Sub

Sub DataList_ItemCommand(Sender As Object, E As DatalistCommandEventArgs)
  Dim cmd As String = E.CommandSource.CommandName
  If cmd = "select" Then
    dlistRooms.SelectedIndex = 0
  End If
  dlistRooms.DataSource = getRooms(E.CommandSource.CommandArgument)
  dlistRooms.DataBind()
End Sub

Sub BindGrid(Sender As Object, E As DataListItemEventArgs)
  Dim listType As ListItemType = CType(E.Item.ItemType ListItemType)
  If listType = ListItemType.SelectedItem Then
    Dim dGrid As DataGrid = CType(E.Item.FindControl("dgridPrices"), DataGrid)
    dGrid.DataSource = getPrices("SHE01")
    dgGrid.DataBind()
  End If
End Sub

Function getRooms() As DataSet
  ... code that gets the hotel room types from the database ...
End Function

Function getRooms(ByVal room_ID As String) As DataSet
  ... overloaded function for getting a SELECTED room type...
End Function

Function getPrices(ByVal room_ID As String) As DataSet
  ... code in here which gets the prices for a SELECTED room type...
End Function

And then my datalist/datagrid...

<asp:DataList id="dlistRooms" runat="server" onItemCommand="DataList_ItemCommand" onItemDataBound="BindGrid">
  <ItemTemplate>
    <h4><%# DataBinder.Eval(Container.DataItem, "room_Name") %></h4>
    <h4>No. Bedrooms: <%# DataBinder.Eval(Container.DataItem, "room_Bedrooms") %></h4>
    <h4>No. Sharing: <%# DataBinder.Eval(Container.DataItem, "room_Sharing") %></h4>
    <h4><asp:LinkButton id="btnPrice" runat="server" CommandName="select" CommandArgument='<%# Container.DataItem("room_ID") %>' Text="Click for prices" /></h4>
  </ItemTemplate>
  <SelectedItemTemplate>
    <h4><%# DataBinder.Eval(Container.DataItem, "room_Name") %></h4>
    <h4>No. Bedrooms: <%# DataBinder.Eval(Container.DataItem, "room_Bedrooms") %></h4>
    <h4>No. Sharing: <%# DataBinder.Eval(Container.DataItem, "room_Sharing") %></h4>
    <asp:DataGrid id="dgridPrices" runat="server" />
  </SelectedItemTemplate>
</asp:DataList>
  </ItemTemplate>


 
Old June 19th, 2005, 08:44 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

First, you need to tell your grid which column holds the unique record ID for the table, in your case it's "room_ID". Add the following attribute to your grid control:

   DataKeyField="room_ID"

Then when your ItemDataBound method runs and calls the BindGrid method for the item that is selected, we can grab the data key value from the DataKey collection for the main grid item (e.Item). In your BindGrid method:

    dGrid.DataSource = getPrices(dlistRooms.DataKeys(e.Item.ItemIndex))


-Peter
 
Old June 20th, 2005, 01:54 AM
Authorized User
 
Join Date: May 2005
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Peter

Thank you so much for your help again - it now works like a charm. It was plain sailing for a while, then I got stuck on this, so it's great to be able to move on (and face the next challenge). :)
I'm definitely going to have to read up on this DataKeysField... and dream of the day when this is like a second language to me too! (In the mean time I'll battle on with my Professional ASP.NET book. Eek.)






Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested datalist arijoy ASP.NET 1.0 and 1.1 Professional 0 December 6th, 2006 01:23 PM
Passing more than one parameters from datagrid Lyn ASP.NET 1.0 and 1.1 Professional 6 August 29th, 2006 02:30 AM
hyperlink in nested datalist debjanib ASP.NET 1.0 and 1.1 Professional 3 June 5th, 2006 02:17 PM
Using Javascript in Nested Datalist savan_thakkar ASP.NET 1.0 and 1.1 Professional 0 March 21st, 2006 05:54 PM
Nested DataList Problem orcities C# 0 July 12th, 2004 01:40 PM





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