如何将Javascript与动态填充的复选框列表集成?
我有一个checkboxList控件,手动填充数据并与JavaScript完全集成:如何将Javascript与动态填充的复选框列表集成?,javascript,asp.net,vb.net,Javascript,Asp.net,Vb.net,我有一个checkboxList控件,手动填充数据并与JavaScript完全集成: <script type="text/javascript"> function ShowHide(chk, txt,txt2) { //Get the Textbox based on selected checkbox ctrltxt = document.getElementById(txt); ctrltxt2= document.ge
<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>
问题:
我如何将上面的JavaScript与这个动态填充版本的同一个checkboxList集成,就像我与手动填充的checkboxList集成一样
这样,每当用户完成表单并选择checkboxlist列表上的任何项目时,所选项目对于特定用户保持选中状态
如果我在另一个页面中查询数据库,但用户希望在一个页面上完成所有操作,则可以执行此操作。您可以使用Attributes集合向ListItem添加客户端属性
下面是一些关于如何进行的示例代码
While sdr.Read()
Dim item As New ListItem()
'-- just a sample... set the appropriate javascript code here
item.Attributes.Add("onclick", "ShowHide(this,'txtTitleChange','classtitles');")
item.Text = sdr("ckRequestReview").ToString()
item.Value = sdr("ID").ToString()
item.Selected = Convert.ToBoolean(sdr("IsSelected"))
ckRequestReview.Items.Add(item)
End While
注:徒手输入的代码仅用于解释概念。可能包含打字错误Hi@PradeepKumar,感谢您的回复。您的示例显示了前两个控件。剩下的四个人怎么了?我尝试将它们添加到列表中,但当我选中一个复选框时,什么也没有发生。我向您展示的只是一个如何将javascript动态添加到列表项的示例。您需要替换txtTitleChange和classtitles,您需要根据数据库中的ckRequestReview值设置它们。您可以使用Select Case语句来实现这一点或任何其他更好的功能。
While sdr.Read()
Dim item As New ListItem()
'-- just a sample... set the appropriate javascript code here
item.Attributes.Add("onclick", "ShowHide(this,'txtTitleChange','classtitles');")
item.Text = sdr("ckRequestReview").ToString()
item.Value = sdr("ID").ToString()
item.Selected = Convert.ToBoolean(sdr("IsSelected"))
ckRequestReview.Items.Add(item)
End While