I have Nested Gridviews with Radiobuttonlists. I need to save child gridviews radiobuttonlist to sql database but not parent's.
The below code only let's me save the parents radiobuttonlist to sql. It would be great if someone could help me fix it.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=QuestList]').click(function () {
var text = $(this).val();
if (text == "No") {
$(this).closest('table').parent().find('div').show();
var radio = $(this).closest('table').parent().find("TR").find("INPUT");
for (var i = 0; i < radio.length; i++) {
if ($(this).closest('table').parent().find("TR").find("INPUT")[i].value == "No") {
$(this).closest('table').parent().find("TR").find("INPUT")[i].checked = true;
}
}
}
else if (text == "Yes") {
$(this).closest('table').parent().find('div').hide();
var radio = $(this).closest('table').parent().find("TR").find("INPUT");
for (var i = 0; i < radio.length; i++) {
if ($(this).closest('table').parent().find("TR").find("INPUT")[i].value == "Yes") {
$(this).closest('table').parent().find("TR").find("INPUT")[i].checked = true;
}
}
}
else if (text == "N/A") {
$(this).closest('table').parent().find('div').show();
var radio = $(this).closest('table').parent().find("TR").find("INPUT");
for (var i = 0; i < radio.length; i++) {
if ($(this).closest('table').parent().find("TR").find("INPUT")[i].value == "N/A") {
$(this).closest('table').parent().find("TR").find("INPUT")[i].checked = true;
}
}
}
});
});
</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" GridLines="None"
CssClass="Grid" DataKeyNames="CustomerId" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButtonList ID="QuestList" runat="server" DataValueField="Question" RepeatDirection="Horizontal">
<asp:ListItem Enabled="False" Style="display: none" Text="" Value="0"></asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
<asp:ListItem Value="N/A">N/A</asp:ListItem>
</asp:RadioButtonList>
<br></br>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" CssClass="ChildGrid"
GridLines="None">
<Columns>
<asp:BoundField DataField="OrderId" HeaderText="OrderId" />
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButtonList ID="StandList" runat="server" DataValueField="Description" RepeatDirection="Horizontal">
<asp:ListItem Enabled="False" Style="display: none" Text="" Value="0"></asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No" Selected="False">No</asp:ListItem>
<asp:ListItem Value="N/A">N/A</asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br></br>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Insert" runat="server" OnClick="Insert"/>
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strResponse As String = String.Empty
For Each row As GridViewRow In GridView1.Rows
If row.RowType = DataControlRowType.DataRow Then
Dim GridView2 As GridView = TryCast(row.FindControl("GridView2"), GridView)
For Each row1 As GridViewRow In GridView2.Rows
If row1.RowType = DataControlRowType.DataRow Then
Dim rbl As RadioButtonList = TryCast(row1.FindControl("StandList"), RadioButtonList)
If rbl.SelectedIndex <> -1 Then
strResponse = rbl.SelectedValue
Dim strConnString As String = ConfigurationManager.ConnectionStrings("zDEVConnectionString2").ConnectionString
Dim strSQL As String = String.Empty
Dim cn As SqlConnection = New SqlConnection(strConnString)
cn.Open()
strSQL = "Update qm2AuditResults set Response =@Response Where AdtRsltsID=@ID"
Dim cmd As SqlCommand = New SqlCommand(strSQL, cn)
cmd.Parameters.Add("@Response", SqlDbType.VarChar)
cmd.Parameters("@Response").Value = strResponse
cmd.Parameters.AddWithValue("@ID", GridView1.DataKeys(row1.RowIndex).Value)
cmd.ExecuteNonQuery()
cn.Close()
End If
End If
Next
End If
Next
End Sub