I have a checkbox list nested inside a gridview.
The gridview pulls its data from an objectdatasourc e and lists countries. The nested checkbox list is databound to another object datasource and lists the regions for each country. The checkboxlist has autopostback set to true and I want to perform an action based on whether or not each checkbox is selected. I am having trouble retrieving the nested checkboxlist to access the selected values.
[HTML] <asp:SqlDataSou rce ID="CountriesDa taSource" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:JustCivilsCo nnection %>"
SelectCommand=" SELECT [countryID], [countryName] FROM [tblCountries] ORDER BY [countryName]">
</asp:SqlDataSour ce>
<div>
<asp:GridView ID="gvCountries " runat="server" AutoGenerateCol umns="False" DataKeyNames="c ountryID"
DataSourceID="C ountriesDataSou rce" CellPadding="2" GridLines="None ">
<Columns>
<asp:BoundFie ld DataField="coun tryName" HeaderText="Cou ntry" SortExpression= "countryNam e" >
<ItemStyle Font-Bold="True" Font-Size="Medium" />
</asp:BoundField>
<asp:TemplateFi eld HeaderText="Reg ions">
<ItemTemplate >
<asp:SqlDataSou rce ID="RegionsData Source" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:JustCivilsCo nnection %>"
SelectCommand=" SELECT [regionID], [regionName] FROM [tblRegion] WHERE ([regionCountry] = @regionCountry) ORDER BY [regionName]">
<SelectParamete rs>
<asp:Paramete r Name="regionCou ntry" Type="Int32" />
</SelectParameter s>
</asp:SqlDataSour ce>
<asp:CheckBoxLi st ID="cblRegions " runat="server" AutoPostBack="T rue" DataSourceID="R egionsDataSourc e"
DataTextField=" regionName" DataValueField= "regionID" OnSelectedIndex Changed="cblReg ions_SelectedIn dexChanged">
</asp:CheckBoxLis t>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
<RowStyle HorizontalAlign ="Left" VerticalAlign=" Top" />
<HeaderStyle HorizontalAlign ="Left" />
</asp:GridView>
</div>[/HTML]
The gridview pulls its data from an objectdatasourc e and lists countries. The nested checkbox list is databound to another object datasource and lists the regions for each country. The checkboxlist has autopostback set to true and I want to perform an action based on whether or not each checkbox is selected. I am having trouble retrieving the nested checkboxlist to access the selected values.
[HTML] <asp:SqlDataSou rce ID="CountriesDa taSource" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:JustCivilsCo nnection %>"
SelectCommand=" SELECT [countryID], [countryName] FROM [tblCountries] ORDER BY [countryName]">
</asp:SqlDataSour ce>
<div>
<asp:GridView ID="gvCountries " runat="server" AutoGenerateCol umns="False" DataKeyNames="c ountryID"
DataSourceID="C ountriesDataSou rce" CellPadding="2" GridLines="None ">
<Columns>
<asp:BoundFie ld DataField="coun tryName" HeaderText="Cou ntry" SortExpression= "countryNam e" >
<ItemStyle Font-Bold="True" Font-Size="Medium" />
</asp:BoundField>
<asp:TemplateFi eld HeaderText="Reg ions">
<ItemTemplate >
<asp:SqlDataSou rce ID="RegionsData Source" runat="server" ConnectionStrin g="<%$ ConnectionStrin gs:JustCivilsCo nnection %>"
SelectCommand=" SELECT [regionID], [regionName] FROM [tblRegion] WHERE ([regionCountry] = @regionCountry) ORDER BY [regionName]">
<SelectParamete rs>
<asp:Paramete r Name="regionCou ntry" Type="Int32" />
</SelectParameter s>
</asp:SqlDataSour ce>
<asp:CheckBoxLi st ID="cblRegions " runat="server" AutoPostBack="T rue" DataSourceID="R egionsDataSourc e"
DataTextField=" regionName" DataValueField= "regionID" OnSelectedIndex Changed="cblReg ions_SelectedIn dexChanged">
</asp:CheckBoxLis t>
</ItemTemplate>
</asp:TemplateFie ld>
</Columns>
<RowStyle HorizontalAlign ="Left" VerticalAlign=" Top" />
<HeaderStyle HorizontalAlign ="Left" />
</asp:GridView>
</div>[/HTML]
Code:
Protected Sub gvCountries_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvCountries.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim ds As SqlDataSource = CType(e.Row.FindControl("RegionsDataSource"), SqlDataSource)
ds.SelectParameters("regionCountry").DefaultValue = gvCountries.DataKeys(e.Row.RowIndex).Value
End If
End Sub