Thread: Bug Base Query
View Single Post
Old December 23rd, 2006, 10:55 AM
Imar's Avatar
Imar Imar is offline
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts

I guess then you're the one that's confused... ;) I think the ideas I presented earlier would work for you....

In the ItemTemplate for the GridView you can set up another GridView (or a DataList or a Repeater or another control). Then when the user clicks a Select button (or another button in the Grid) you handle the SelectedIndexChanged of the GridView. There you can get a reference to the DataKey that is associated with the item that's clicked. Based on this key, you can access the database again, get a list of associated comments, and then bind those to your nested GridView.

Something like this should work:
<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" DataKeyNames="Id" DataSourceID="SqlDataSource1" 
    <asp:TemplateField HeaderText="SomeField" SortExpression="SomeField">
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("SomeField") %>'></asp:Label>
        <br />
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource2">
        <asp:Label runat="server" Text='<%# Eval("Comment") %>'></asp:Label><br />
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
              ConnectionString="<%$ ConnectionStrings:SomeConn%>" 
              SelectCommand="SELECT Comment FROM Comments WHERE ([BugId] = @BugId)">
            <asp:Parameter Name="BugId" Type="Int32" />
As you can see, the template contains a nested SqlDataSource that retrieves the sub items for each main item. The SDS gets a value for the parameter in the code behind.

Code Behind:
  Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, 
          ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    Dim id As Integer = GridView1.DataKeys(GridView1.SelectedIndex).Value
    Dim myRow As GridViewRow = GridView1.Rows(GridView1.SelectedIndex)
    Dim mySource As SqlDataSource = CType(myRow.FindControl("SqlDataSource2"), SqlDataSource)
    mySource.SelectParameters(0).DefaultValue = id
  End Sub
  This is not really tested, so you may need to tweak the code a bit to make it work.

Regarding Ajax: did you take a look at the Microsoft Ajax Framework? It offers a lot of features you can use to avoid postbacks in this scenario. It's a bit too much to get into all the details in this thread, but take a look here:


Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.