Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 26th, 2007, 10:28 PM
Friend of Wrox
 
Join Date: Jul 2006
Location: olathe, ks, USA.
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default How to tie to nested GridView inside DataList

I have a DataList that has a GridView inside it.

I am trying to tie to the rowCreating event of my nested GridView. Basically, I want to customize each row to either show data or to have a command so..basically, my DataList would be simply an Event Title while the GridView shows people signed up for the Event. If there are openings for the event, I want to put a button in the row to allow them to enroll so...

EVENT TITLE

1) John Doe
2) Jane Doe
3) ENROLL
4) ENROLL

..where 1) - 4) are rows in the Gridview. So..

a) How Can I tie to the nested Gridview
b) What Event in the GridView do I need to tie to to customize the rows?

Thanks,
Rob

Reply With Quote
  #2 (permalink)  
Old October 28th, 2007, 01:12 PM
Friend of Wrox
 
Join Date: Jul 2006
Location: olathe, ks, USA.
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

I think I might be on the right track, but I'm not sure why, when I debug and put a breakpoint in the gridview handler, I never get the debug to activate--the breakpoint isn't triggered:

Protected Sub DataList3_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList3.ItemDataBound
        Dim myGridView As GridView
        myGridView = CType(e.Item.FindControl("GridView2"), GridView)
        AddHandler myGridView.DataBound, AddressOf Me.myGridView_DataBound
        If myGridView.DataKeys.Count > 0 Then
            Dim shiID As Integer = myGridView.DataKeys(0).Value
        End If

        Dim tester As Integer = 0
    End Sub

    Protected Sub myGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim mGridView As GridView
        mGridView = CType(DataList3.FindControl("GridView2"), GridView)

*Breakpoint inserted here* Dim tester As Integer = 0
    End Sub

There's some extraneous code that I don't expect you to understand. Basically, I am trying to capture the shiftID so that I can pass it to a function to find the members that have enrolled in the shift. Once I have that count, then I want to do something in the Databound eventhandler to add rows to the Gridview if the amount of rows is less than a value i set as numPeople. In other words, if the maximum amount of people I want to enroll in the shift is 4 and there are only two people enrolled (two people would be returned to the Gridview) then I want to add two rows with a linkbutton that is called "ENROLL" that will have it's own event handler set up to enroll the person that is logged in the site to the event.

My first obstacle is that the dynamically set event handler I created is not triggering.

Kind Regards,
Rob

Reply With Quote
  #3 (permalink)  
Old October 28th, 2007, 06:19 PM
Friend of Wrox
 
Join Date: Jul 2006
Location: olathe, ks, USA.
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Ok, I have made a little more headway in that my nested Gridview had set it's DataSource property to point to a custome method I created to return a DataTable and didn't have it's DataSourceID method pointing to a DataSource. Once I replaced the DataSource method with an actual DataSourceID of a newly created Object DataSource, it triggered the dynamically added Handler--now I need to mess with how to add the linkbuttons to the GridView----more to come.

Reply With Quote
  #4 (permalink)  
Old October 29th, 2007, 12:22 AM
Friend of Wrox
 
Join Date: Jul 2006
Location: olathe, ks, USA.
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

I think I can do this if I can figure out a way to get the DataKey of the DataList Item for which the GridView is being created. I don't know how to reference it. So, in other words, I have a DataList. For each shift in the Datalist, a Gridview lists the members. So I want the DataKey (which is the shiftID) specific to that shift.

I have the following:

The problem is the second line of myGridView_RowDataBound I don't know how to change Dim shift As Integer = DataList3.DataKeys.Item(0) to be the specific DataKey of the DataList.


******

 Protected Sub DataList3_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList3.ItemDataBound
        Dim myGridView As GridView
        rowCount = 0
        myGridView = CType(e.Item.FindControl("GridView2"), GridView)
        AddHandler myGridView.RowDataBound, AddressOf myGridView_RowDataBound
        Dim tester As Integer = DataList3.DataKeys.Item(shCount)
        ObjectDataSource1.SelectParameters.Clear()
        ObjectDataSource1.SelectParameters.Add("shiftID", tester)
    End Sub


    Protected Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        Dim shift As Integer = DataList3.DataKeys.Item(0)
        Dim numPeople As Integer = Member.GetNumPeoplePerShift(shift)

        Dim myGridView As GridView
        myGridView = CType(DataList3.FindControl("GridView2"), GridView)
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            rowCount += 1
        ElseIf (e.Row.RowType = DataControlRowType.Footer) Then
            Dim tester = rowCount

            If rowCount < numPeople Then
                Dim tbl As New Table
                tbl = e.Row.Parent
                For i As Integer = 1 To (numPeople - rowCount)
                    Dim myRow As New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
                    Dim myCell As New TableCell
                    Dim myLabel As New Label
                    myLabel.Text = "test"
                    myCell.Controls.Add(myLabel)
                    myRow.Cells.Add(myCell)
                    tbl.Rows.Add(myRow)
                Next

            End If
            rowCount = 0

        End If
        Dim hold As Integer = 1
    End Sub

Reply With Quote
  #5 (permalink)  
Old October 29th, 2007, 08:58 PM
Friend of Wrox
 
Join Date: Jul 2006
Location: olathe, ks, USA.
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

Well--I'm not enitrely sure how I finally got this to work--I'm sure there is a much easier way to do it--but I created some global page variables to track the index ... so here's my final methods:



Imports System.IO
Imports System.Data
Imports System.Data.SqlClient

Partial Class Member_checkenrollment

Inherits System.Web.UI.Page

Private rowCount As Integer = 0
Private shCount As Integer = 0

Protected Sub DataList3_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataList3.ItemDataBound

Dim myGridView As GridView
rowCount = 0
myGridView = CType(e.Item.FindControl("GridView2"), GridView)
AddHandler myGridView.RowDataBound, AddressOf myGridView_RowDataBound
Dim shiftID As Integer = DataList3.DataKeys.Item(0)

ObjectDataSource1.SelectParameters.Clear()
ObjectDataSource1.SelectParameters.Add("shiftID", shiftID)

End Sub



Protected Sub myGridView_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

Dim shift As String = DataList3.DataKeys.Item(shCount)
Dim numPeople As Integer = Member.GetNumPeoplePerShift(shift)

If (e.Row.RowType = DataControlRowType.DataRow) Then

   rowCount += 1

ElseIf (e.Row.RowType = DataControlRowType.Footer) Then

   If rowCount < numPeople Then
      Dim tbl As New Table
      tbl = e.Row.Parent
      For i As Integer = 1 To (numPeople - rowCount)
         Dim myRow As New GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Normal)
         Dim myCell As New TableCell
         Dim myLabel As New Label
         myLabel.Text = "test"
         myCell.Controls.Add(myLabel)
         myRow.Cells.Add(myCell)
         tbl.Rows.Add(myRow)
      Next
   End If

rowCount = 0
shCount += 1

End If

End Sub



GetNumPeoplePerShift is a simple function I put to just return an integer from the shift Table that has the max # of people that can enroll in a shift.


TOO much customization with counters--there has to be a way inside the RowDataBound function to get the right DataList3.DataKeys(SOMETHING) where SOMETHING is the current Datalist Item being created. (ie the first item, second, third..etc)


Reply With Quote
  #6 (permalink)  
Old March 19th, 2008, 01:45 PM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I really appreciate the work you did here. I was looking to do something very similar and your solution helped me figure out what I needed to do. I did come up with a simpler solution. Since this is an older post you may have already figured it out yourself but here is what I did:

Protected Sub dlvClasses_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlvClasses.ItemDataBound

            Dim classId As String = Me.dlvClasses.DataKeys.Item(e.Item.ItemIndex)
            Dim ods As ObjectDataSource = CType(e.Item.FindControl("dsClauses"), ObjectDataSource)

            ods.SelectParameters("insuranceAuthorityClassCode" ).DefaultValue = classId
            ods.Select()

End Sub


The ObjectDataSource is the one I defined for my gridview. Basically I developed my gridview and set the properties outside of the datalistview. When I was done I cut and pasted it into the itemtemplate of the datalistview. I then wrote the code above to populate the gridview and datalistview itemdatabound event.

Hope this helps someone out there! Here is the HTML for my user control in which I have this set up:

<asp:DataList ID="dlvClasses" runat="server" DataSourceID="dsInsuranceEntityAuthorityClasses" DataKeyField="InsuranceAuthorityClassCode">
    <ItemTemplate>
        <table>
            <tr>
                <td class="typetitle">Authority Class <%# Eval("InsuranceAuthorityClassCode") %>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:GridView ID="gvClauses" SkinID="ReadOnlyTabListing" runat="server" AutoGenerateColumns="False" DataSourceID="dsClauses">
                        <Columns>
                            <asp:BoundField DataField="InsuranceAuthorityClauseCode" HeaderText="Clause"
                                SortExpression="InsuranceAuthorityClauseCode" />
                            <asp:BoundField DataField="ClauseDescription" HeaderText="Description" ReadOnly="True"
                                SortExpression="ClauseDescription" />
                            <asp:BoundField DataField="EffectiveDateDisplay" HeaderText="Effective Date"
                                ReadOnly="True" SortExpression="EffectiveDateDisplay" />
                            <asp:BoundField DataField="CancelDateDisplay" HeaderText="Cancel Date" ReadOnly="True"
                                SortExpression="CancelDateDisplay" />
                        </Columns>
                    </asp:GridView>
                    <asp:ObjectDataSource ID="dsClauses" runat="server"
                        SelectMethod="GetInsuranceAuthoritysByEntityNumber Class" TypeName="IDFPR.RegulatedEntities.BLL.InsuranceAut hority">
                        <SelectParameters>
                            <asp:SessionParameter Name="entityNumber" SessionField="EntityNumber" Type="Int32" />
                            <asp:Parameter Name="insuranceAuthorityClassCode" Type="String" />
                        </SelectParameters>
                    </asp:ObjectDataSource>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList><asp:ObjectDataSource ID="dsInsuranceEntityAuthorityClasses" runat="server"
    SelectMethod="GetInsuranceEntityAuthorityClasses"
    TypeName="IDFPR.RegulatedEntities.BLL.InsuranceEnt ityAuthorityClass">
    <SelectParameters>
        <asp:SessionParameter Name="entityNumber" SessionField="EntityNumber" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

Reply With Quote
  #7 (permalink)  
Old June 18th, 2011, 07:35 AM
Registered User
 
Join Date: Jun 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Many thanks

The post by etasigma is great and has saved me ages as it is a precise fit for what I want to do.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Datalist inside datalist amit.jagtap ASP.NET 2.0 Professional 1 September 4th, 2007 06:03 AM
Nested datalist arijoy ASP.NET 1.0 and 1.1 Professional 0 December 6th, 2006 01:23 PM
hyperlink in nested datalist debjanib ASP.NET 1.0 and 1.1 Professional 3 June 5th, 2006 03: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 02:40 PM



All times are GMT -4. The time now is 07:07 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.