I have successfully implemented a checkboxList control that I manually populated with data and integrated with JavaScript:
<script type="text/javascript">
function ShowHide(chk, txt,txt2) {
//Get the Textbox based on selected checkbox
ctrltxt = document.getElementById(txt);
ctrltxt2= document.getElementById(txt2);
//Check if checkbox is checked or not
if (chk.checked) {
//Show the Textbox
ctrltxt.style.display = 'block';
ctrltxt2.style.display = 'block';
} else {
//Hide the textbox
ctrltxt.style.display = 'none';
ctrltxt2.style.display = 'none';
}
}
</script>
<table style="border: 0; border-style: solid">
<tr valign="top" style="background-color: #f5f7f7; font-size: large; white-space: nowrap;">
<td style="font-size: large; font-weight: bold;">Request a Review of:</td>
<td>
<asp:CheckBoxList ID="ckRequestReview" runat="server" CssClass="cb" Style="border-width: 0;" RepeatDirection="horizontal" RepeatColumns="4" RepeatLayout="Table">
<asp:ListItem onclick="ShowHide(this,'txtTitleChange','classtitles');">Job Title</asp:ListItem>
<asp:ListItem onclick="ShowHide(this,'txtPayGradeChange','gradeclass');">Pay Grade</asp:ListItem>
<asp:ListItem onclick="ShowHide(this,'txtClassSpecChange','clssSpec');">Specs</asp:ListItem>
</asp:CheckBoxList>
</td>
</tr>
</table>
The code snippet above pertains to this specific question.
Initially, when a user selects an item like Job Title from the ckRequestReview checkboxlist control, the textboxes txtTitleChange
and classtitles
become visible for entering data.
This implementation has been working well so far.
However, based on user feedback requesting to retain selected items in the checkbox list, I decided to dynamically populate the checkboxList from a database. Here's how it's done now:
Private Sub PopulateChoices()
Using conn As New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("CNchoices").ConnectionString()
Using cmd As New SqlCommand()
cmd.CommandText = "select * from muytable"
cmd.Connection = conn
conn.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
Dim item As New ListItem()
item.Text = sdr("ckRequestReview").ToString()
item.Value = sdr("ID").ToString()
item.Selected = Convert.ToBoolean(sdr("IsSelected"))
ckRequestReview.Items.Add(item)
End While
End Using
conn.Close()
End Using
End Using
End Sub
Question:
Now, I need to know how to integrate the previously mentioned JavaScript functionality with this dynamically populated version of the checkboxList. The goal is for any selected item to remain selected even after form submission.
Is there a way to achieve this without querying the database on another page, as users prefer all actions to be completed on a single page?