I am creating a search module. The basic module has a text box (where the user can type in the search criteria) and combos after which results are displayed in a gridview.
The gridview is set up to include a child gridview in a template field. The child gridview should display data only when selecting the row on the parent grid. I have code in the rowcommand event handler for the parent grid to bind the child gridview's datasource to the selected row's datakeyname.
I am quite new to ASP.Net so maybe there are obvious issues with my code (please bare with me) Here are the issues I am battling with:
1) The rowcommand event handler only fires (predictably) when I'm logged in as Host. As this is a public search grid, I want any user (logged in or not) to be able to search the grid.
2) When I do log in as Host, the parent data loads without problem. When I click on any of the parent rows for the FIRST time, every child grid for every parent row becomes visible, except for the one that I clicked on. I have a suspision that the events are firing twice on the first click (because I have code that says "childgrd.visible = not childgrd.visible" to toggle the visibility based on the user's click)
3) (Last issue) Every time I click a parent item, each child grid (that is displayed at that point) shows the child data for the last selected parent row. I.e. if I have a parent grid that lists store names and a child grid that shows address data and I click on store 1, then the child grid becomes visible showing store 1's data. Then I click on store 2. The child grid below store 2 is displayed showing store 2 address data, however, the child grid under store 1 now also shows address data for store 2
I have been looking through the code for hours and I'm either misunderstanding the entire setup or I'm looking over my own stupid mistake. If anyone can assist, I would appreciate it.
Below my code:
<tr>
<td>
<asp:GridView ID="GridViewMainListing" runat="server" Width="100%"
AutoGenerateColumns="False" DataKeyNames="ResultsListingItemId"
DataSourceID="ObjectDataSource_MainListing"
CssClass="NestedGridView1Style"
OnRowCommand="GridViewMainListing_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ToolTip="Click here to see the hours" Text="Select" runat="server" ID="btnSelect" CommandName="Select"></asp:Button>
<!--<asp:CommandField ShowSelectButton="True" ButtonType="Button"><ItemStyle Width="10%" /></asp:CommandField>-->
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<table cellpadding="2" cellspacing="0" border="0" width="100%" align="left">
<!-- this row has the Header-->
<tr>
<td width="25%">
Name
</td>
<td width="25%">
Centre / Street
</td>
<td width="20%">
Contact No
</td>
<td width="20%">
Suburb
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table border="0" cellpadding="2" cellspacing="0" width="100%" align="left">
<!-- this row has the parent grid -->
<tr>
<td width="25%">
<asp:Label ID="lblCompanyName" runat="server" Text='<%# Eval("ResultsListingName") %>'></asp:Label>
</td>
<td width="25%">
<asp:Label ID="lblCity" runat="server" Text='<%# Eval("ResultsListingCentreOrStreet") %>'></asp:Label>
</td>
<td width="20%">
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("ResultsListingContactNo") %>'></asp:Label>
</td>
<td width="20%">
<asp:Label ID="lblSuburb" runat="server" Text='<%# Eval("ResultsListingSuburb") %>'></asp:Label>
</td>
</tr>
<!-- this row has the child grid-->
<tr>
<td colspan="4">
<table border="0" cellpadding="3" cellspacing="0" width="100%">
<tr>
<!-- this cell has the list of hours for the selected listing -->
<td>
<asp:GridView ID="GridviewDetail" runat="server" DataKeyNames="ResultsDetailItemId" Visible="True"
DataSourceID="ObjectDataSource_DetailListing" AutoGenerateColumns="False"
CssClass="GridView1ChildStyle"
Width="90%"
HorizontalAlign="Right">
<HeaderStyle CssClass="Header" />
<Columns>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:Label ID="lblResultsDescription" Width="50%" runat="server" Text='<%# Eval("ResultsDescription") %>'></asp:Label>
<!--<asp:Label ID="Label3" Width="50%" runat="server" Text='<%#Bind("ResultsDescription") %>'></asp:Label>-->
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Hours">
<ItemTemplate>
<asp:Label ID="lblResultsHours" Width="50%" runat="server" Text='<%# Eval("ResultsHours") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
<asp:ObjectDataSource ID="ObjectDataSource_DetailListing" runat="server"
OldValuesParameterformatString="original_{0}"
SelectMethod="GetLSTResultsDetail" TypeName="PracticalComforts.Modules.LSTResults.LSTResultsController" >
<SelectParameters>
<asp:ControlParameter ControlID="GridViewMainListing" Name="ResultsListingItemId" PropertyName="SelectedDataKey.Values[0]" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
<td>
</tr>
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Herewith the code behind:
Protected Sub GridViewMainListing_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridViewMainListing.RowCommand
'variables
Dim gv As GridView = DirectCast(sender, GridView)
Dim rowIndex As Integer = e.CommandArgument
Select Case e.CommandName
Case "Select"
Dim childgv As GridView = DirectCast(gv.Rows(rowIndex).FindControl("GridviewDetail"), GridView)
If Not childgv Is Nothing Then
'visibility of child grid
childgv.Visible = Not childgv.Visible
'refresh the data source
Dim odsHours As ObjectDataSource = DirectCast(gv.Rows(rowIndex).FindControl("ObjectDataSource_DetailListing"), ObjectDataSource)
odsHours.SelectParameters("ResultsListingItemId").DefaultValue = gv.DataKeys(rowIndex)(0).ToString
End If
End Select
End Sub
Protected Sub GridViewMainListing_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewMainListing.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
'set the command argument
Dim btnSelect As Button = e.Row.Cells(0).FindControl("btnSelect")
btnSelect.CommandArgument = e.Row.RowIndex.ToString
End If
End Sub