Link to home
Start Free TrialLog in
Avatar of speedy1fas
speedy1fasFlag for United States of America

asked on

Datalist inside another datalist - how do I grab an ID from the outer datalist to use as a select parameter to populate the inner datalist?

First of all, I want to apologize for being an idiot. I am very new to this and barely consider myself a beginner.

I am programming with ASP.net using VB. I am also using SQL Server 2005.

I have a datalist that is populated by a sqldatasource using an external parameter that returns a category name and a categoryid. For example, it will return the following records:
CategoryName: "Labor" CategoryID: "1"
CategoryName: "Parts" CategoryID: "2"
CategoryName: "Service Specials" CategoryID: "5"
It will return a set of records from my database via a stored procedure. Within the itemtemplate of my "outer" datalist, I'd like to use another datalist that is populated by another stored procedure that calls upon the CategoryID for each returned result. For example, if the return for the first record has categoryid of 1, then the "inner" stored procedure will pass the categoryid as a parameter with value 1 to return the following records:
ServiceName: "Technician" ServicePrice: "10"
ServiceName: "Electrician" ServicePrice: "5"
By using the code below, the result will look like:

Labor
Technician
Electrician

Parts
<<Part Name 1>>
<<Part Name 2>>

Service Specials
<<Specials Name 1>>
<<Specials Name 2>>

Basically, I need help in how to pass the categoryid as a parameter to my "inner" datasource's stored procedure. Again, I apologize for being an idiot, but I need help. Thanks!
<asp:Datalist ID="dlJobDetails" runat="server" DataSourceID="dsCategories">
   <ItemTemplate>
      <asp:Label ID="lblCategory" runat="server" Text='<%# Eval("CategoryName") %>' />
      <asp:DataList ID="dlServices" runat="server" DataSourceID="dsServices">
         <ItemTemplate>
            <asp:Label ID="lblServiceName" runat="server" Text='<%# Eval("ServiceName") %>' />
         </ItemTemplate><br /><br />
      </asp:DataList>
   </ItemTemplate>
</asp:Datalist>
<%-- Skipped a bunch of other stuff--%>
<asp:SqlDataSource ID="dsServices" runat="server" ConnectionString="<%$ ConnectionStrings:DevelopmentConnection %>" SelectCommand="usp_M_ServiceNameGet" SelectCommandType="StoredProcedure">
   <SelectParameters>
      <%-- Here is where I need to send the parameter of "CategoryID" to my stored procedure. --%>
   </SelectParameters>
</asp:SqlDataSource>

Open in new window

Avatar of David Robitaille
David Robitaille
Flag of Canada image

a smiple way of doing this is to moce the SqlDataSource "dsServices" into the Datalist "dlJobDetails". you could then use a hiddenfield to link it with a controlparameter.
Another solution is to "bind" the inner datalist in the ItemDataBound event of the outer datalist.
Avatar of speedy1fas

ASKER

Would the datalist "dlJobDetails" have two datasources then? Is that possible and how would I code that? Would I still have the "inner" datalist then?

How would I go about "binding" the inner datalist?

Do you have any code examples of either solution?

Thanks so much for your help!
FYI, it<s ususly called "Nested datalist" you could get exemple by searching that or "nested gridView" (the principle is the same.)here some :
http://msdn.microsoft.com/en-us/library/aa992038(VS.80).aspx
http://www.velocityreviews.com/forums/t300351-quotcodelessquot-nested-gridviewsthere-has-to-be-a-better-way.html
 The idea is to have a datasource for the "outer" datalist.
Each ITEM of the DataList will have a other DataSource Inside. The select parameter of the inside datasource is linked to a field in the item ("beside him"). then you could set the datasource of the inner datalist to this item.
Again, thanks so much for the help, I tried the "codeless" mention from your velocityreviews link. I placed an invisible label inside the itemtemplate of the "outer" gridview and in the datasource dsServices I added a selectparameter for that control. Here is the label I used:

<asp:Label ID="lblCategoryID" runat="server" visible="False" Text='<%# Eval("CategoryID") %>' />

in my datasource under the select parameters ("dsServices"), I added the following:

<asp:ControlParameter ControlID="lblCategoryID" Name="CategoryID" Type="Int16" />

I am now getting the error:

Could not find control 'lblCategoryID' in ControlParameter 'CategoryID'.

Any thoughts? Again, Thanks!
ASKER CERTIFIED SOLUTION
Avatar of David Robitaille
David Robitaille
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are amazing. Thank you so much. Worked perfectly!
Perfect, THANKS!!!!
Glad i could help!