 |
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 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
|
|
|

October 26th, 2007, 09:28 PM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|

October 28th, 2007, 12:12 PM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|

October 28th, 2007, 05:19 PM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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.
|

October 28th, 2007, 11:22 PM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|

October 29th, 2007, 07:58 PM
|
Friend of Wrox
|
|
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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)
|

March 19th, 2008, 12:45 PM
|
Registered User
|
|
Join Date: Mar 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|

June 18th, 2011, 06:35 AM
|
Registered User
|
|
Join Date: Jun 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|
 |