handling an event for a control embedded in a datalist

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
I have a datagrid inside a datalist. The datalist shows parent info and the datagrid shows the child info for that parent. There is a checkbox on each row of the child datagrid. Also inside the datalist is a radiobuttonlist. When this radiobuttonlist is checked, I would like it to iterate thru each row on the child grid and either select or deselct the checkboxes (which are on each row of the child datagrid). In addition, the user can also check each of these checkboxes individually as well. btw, I made the columns that I need to access inside the datagrid template columns. For the rows that are checked, I want to save them to a dataset.

I created code below to do the above task:


Private Sub DataList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataList1.SelectedIndexChanged



'Capture the postback event from the radiobuttonlist

Dim rblConditions As RadioButtonList = DirectCast(FindControl("RadioButtonList1"), RadioButtonList)

Dim dg2 As DataGrid = DirectCast(FindControl("DataGrid2"), DataGrid)

Dim chkCondition As CheckBox = DirectCast(FindControl("Condition"), CheckBox)

Dim lblLockNumber As Label = DirectCast(FindControl("LockNumber"), Label)

Dim lblConditionDetailID As Label = DirectCast(FindControl("ConditionDetailID"), Label)

Dim lblLoanID As Label = DirectCast(FindControl("LoanID"), Label)

Dim i As Int16



If rblConditions.SelectedIndex = YesNoIndex.YES Then

'check all the boxes in the child datagrid

For i = 0 To dg2.Items.Count - 1

chkCondition.Checked = True

Next

Else

'uncheck all the boxes in the child datagrid

For i = 0 To dg2.Items.Count - 1

chkCondition.Checked = False

Next

End If



For i = 0 To dg2.Items.Count - 1

If chkCondition.Checked = True Then

Dim drSave As dsSaveCondition.dsSaveConditionRow = DsSaveCondition1.dsSaveCondition.NewdsSaveConditionRow

drSave.Lock_Number = lblLockNumber.Text

drSave.ConditionDetailID = lblConditionDetailID.Text

drSave.LoanID = lblLoanID.Text

DsSaveCondition1.dsSaveCondition.Rows.Add(drSave)

End If

Next



If ViewState.Item("DsSaveCondition1") Is Nothing Then

DsSaveCondition1.AcceptChanges()

Else

DsSaveCondition1 = DirectCast(ViewState.Item("DsSaveCondition1"), DataSet)

End If



ViewState.Item("DsSaveCondition1") = DsSaveCondition1



End Sub




Since I won't be able to test this out until Monday, I'm wondering if the above code is "on target". I'm unsure of how to handle the postback event from the radiobuttonlist (autopostback is set to TRUE) which is inside the datalist as well as cycling thru the datagrid rows to check or uncheck the boxes.

Can someone please tell me if I need to do anything else?

Your help is greatly appreciated...
 
I know the datalist can respond to events from child button controls via the ITEMCommand event, etc of the Datalists ItemCommand event. But, can the datalist handle events such as the one I have outlined in the previous message, particularly selecting a child radiobutton inside the datalist, posting the page back and responding to which radiobutton was checked (inside the datalist) and then programmatically selecting check boxes on a child datagrid within the datalist.

I presently have the code to do this inside the "DataList1_SelectedIndexChanged" event (as described in my previous msg). If this is not the case, I would then try to place the code inside an event that I would set up in the HTML as follows for the radiobuttonlist:
OnSelectedIndexChanged="rblRadioButtonList1_SelectedIndexChanged"

I then would have an event in code-behind which will handle this:
Protected WithEvents RadioButtonList1 As RadioButtonList
.
.
.
Private Sub rblRadioButtonList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedIndexChanged

Can someone please tell me if I'm target with this?
 
can't find child controls

I'm having a problem trying to access column controls inside a child datagrid which is inside a datalist.

1) I placed the following code in the button event just to see if I could cycle thru the datagrid control collection:
<code>
Dim strhello As String
Dim myDataListItem As DataListItem
Dim myDatagridItem As DataGridItem
Dim myCheckBox As CheckBox
Dim myDataGrid As DataGrid

For Each myDataListItem In DataList2.Items
myDataGrid = myDataListItem.FindControl("DataGrid3")
If Not IsNothing(myDataGrid) Then
For Each myDatagridItem In myDataGrid.Items
myCheckBox = myDataGrid.FindControl("Condition3")
If myCheckBox.Checked Then
strhello = "found!"
'do any processing that is needed
End If
Next
End If
Next
</code>

2) The line "For Each myDatagridItem In myDataGrid.Items" produces the following:
- myDataGrid.Items.Count yields 4 (which is correct since there are 4 rows in this child grid)
- myDatagridItem.Controls.Count yields 8 (which is correct because there are 8 column controls). According to this statement, I should be able to access the columns inside this child grid.
- However, the statement "myCheckBox = myDataGrid.FindControl("Condition3")" yields NOTHING... I can't access any of the column controls inside this child datagrid!

3) I'm able to get a listing on each of the items inside this datagrid from item 0 to 7 as follows:
myDatagridItem.Controls.Item(7)
{System.Web.UI.WebControls.TableCell}
[System.Web.UI.WebControls.TableCell]: {System.Web.UI.WebControls.TableCell}
BindingContainer: {System.Web.UI.WebControls.DataGridItem}
ClientID: "DataList2__ctl1_Datagrid3__ctl2__ctl1"
Controls: {System.Web.UI.ControlCollection}
EnableViewState: True
ID: Nothing
NamingContainer: {System.Web.UI.WebControls.DataGridItem}
Page: {ASP.BulkConditions_aspx}
Parent: {System.Web.UI.WebControls.DataGridItem}
Site: Nothing
TemplateSourceDirectory: "/LydianMDS"
UniqueID: "DataList2:_ctl1:Datagrid3:_ctl2:_ctl1"
Visible: True

Notice the ID above. EACH OF THESE ITEMS IS SET TO NOTHING! That is the reason why I can't access the controls in code..........

OK now... Since I have debugged this issue, I'm hoping someone will be able to tell me WHY THOSE ITEMS HAVE AN ID OF NOTHING... I'm expecting to find these items programatcially, but can't. In my HTML below, you can plainly see that the template columns have an ID associated with them.

The following is the HTML of my entire datalist (parent) and datagrid (child) controls.
<code>
<asp:DataList id=DataList2 style="Z-INDEX: 105; LEFT: 8px; POSITION: absolute; TOP: 192px" runat="server" DataMember="spMSSASPGetDistinctOutstandingConditionsInInvestorReviewStep" DataKeyField="Lock_Number" DataSource="<%# DsOutstandingCondsInInvestorRvw1 %>" Width="936px" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Height="544px">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
<HeaderTemplate>
<P>Outstanding BULK Conditions</P>
</HeaderTemplate>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<FooterTemplate>
NO MORE OUTSTANDING CONDITIONS
</FooterTemplate>
<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
<ItemTemplate>
<asp:label id=Name2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Name")%>'>
</asp:label>
<asp:label id=LockNumber2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Lock_Number")%>'>
</asp:label>
<asp:label id=LoanID2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.LoanID")%>' Visible="False">
</asp:label>
<P>
<asp:Button id="btnWaive" runat="server" Text="Waive All Conditions" CommandName="Waive"></asp:Button>    
<asp:Button id="btnNoWaive" runat="server" Text="Don't Waive Any Condtions" CommandName="NoWaive"></asp:Button></P>
<asp:DataGrid id=Datagrid3 runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" datasource='<%# Container.DataItem.CreateChildView("spMSSASPGetDistinctOutstandingConditionsInInvestorReviewStepspMSSASPGetOutstandingConditionsInInvestorReviewStep")%>'>
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#008A8C"></SelectedItemStyle>
<AlternatingItemStyle BackColor="Gainsboro"></AlternatingItemStyle>
<ItemStyle ForeColor="Black" BackColor="#EEEEEE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:checkbox runat="server" id="Condition3"></asp:checkbox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Name" ReadOnly="True" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="LoanID">
<ItemTemplate>
<asp:label runat="server" id="LoanID3" Text='<%# DataBinder.Eval(Container, "DataItem.LoanID") %>'>
</asp:label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Lock Number">
<ItemTemplate>
<asp:label runat="server" id="LockNumber3" Text='<%# DataBinder.Eval(Container, "DataItem.Lock_Number") %>'>
</asp:label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Conditional Dtl ID">
<ItemTemplate>
<asp:label runat="server" id="ConditionDetailID3" Text='<%# DataBinder.Eval(Container, "DataItem.ConditionDetailID") %>'>
</asp:label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="ConditionID" ReadOnly="True" HeaderText="ConditionID"></asp:BoundColumn>
<asp:BoundColumn DataField="Description" ReadOnly="True" HeaderText="Description"></asp:BoundColumn>
<asp:BoundColumn DataField="Comment" ReadOnly="True" HeaderText="Comment"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="Black" BackColor="#999999" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
</ItemTemplate>
<FooterStyle ForeColor="Black" BackColor="#CCCCCC"></FooterStyle>
<SeparatorTemplate>
----------------------------------------------------------------------------------------------------------------------------------------------------------
</SeparatorTemplate>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#000084"></HeaderStyle>
</asp:DataList>
</code>

Can someone PLEASE HELP ME WITH THIS. I have gone as FAR AS I can go with it. I'm totally stuck at this point...

Why do those controls in the datagrid have an ID of NOTHING (thereby not allowing me to access those controls)...
 
I've never done exactly what you are trying to do. Normally when I loose my ID's in a datagrid on a postback is if that ID is generated at runtime. Even though you have it in the design, I'm wondering if becuae it is a template, it still gets created at runtime, in which case you'd have to rebind the whole datalist. Not sure about this. Have u tried accessing it by the controls index position instead of by name?
 
The problem was that I wasn't doing the findcontrol method down on the proper child control level!!! By looping thru the following code, and playing around, I was able to gain access to the child controls inside the datagrid....

For all to see and share:
<code>
Dim myDataListItem As DataListItem
Dim myDatagridItem As DataGridItem
Dim myDataGrid As DataGrid
Dim myCheckBox As CheckBox
Dim lblLockNumber As Label
Dim lblConditionDetailID As Label
Dim lblLoanID As Label
Dim i As Int16

Try
For Each myDataListItem In DataList2.Items
myDataGrid = myDataListItem.FindControl("DataGrid3")
If Not IsNothing(myDataGrid) Then
i = 0
For Each myDatagridItem In myDataGrid.Items
myCheckBox = myDatagridItem.Controls.Item(i).FindControl("Condition3")
If myCheckBox.Checked Then
lblLockNumber = myDatagridItem.Controls.Item(i).FindControl("LockNumber3")
lblConditionDetailID = myDatagridItem.Controls.Item(i).FindControl("ConditionDetailID3")
lblLoanID = myDatagridItem.Controls.Item(i).FindControl("LoanID3")
Dim drSave As dsSaveCondition.dsSaveConditionRow = DsSaveCondition1.dsSaveCondition.NewdsSaveConditionRow
drSave.Lock_Number = lblLockNumber.Text
drSave.ConditionDetailID = lblConditionDetailID.Text
drSave.LoanID = lblLoanID.Text
DsSaveCondition1.dsSaveCondition.Rows.Add(drSave)
End If
i += 1
Next
End If
Next
Catch ex As Exception
m_blnError = True
Response.Write("<script>alert('" & ex.Message & "');</script>")
End Try
</code>
 
Back
Top