[英]How do I integrate Javascript with dynamically populated checkboxList?
I have this checkboxList control that I manually populated with data and fully integrated with JavaScript: 我有一个checkboxList控件,我手动填充了数据并与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>
Above is just relevant code to this question. 以上只是与此问题相关的代码。
When a user selects for instance Job Title from the ckRequestReview checkboxlist control, txtTitleChange
and classtitles
are exposed allowing users to enter data into them. 例如,当用户从ckRequestReview复选框列表控件中选择“职务”时,将显示
txtTitleChange
和类classtitles
,从而允许用户向其中输入数据。
This works a treat. 这可以治疗。
However, due to a change request by users to allow for any selected list item from the checkboxlist to remain selected, I decided to use checkboxlist is dynamically populated from the database. 但是,由于用户的更改请求允许复选框列表中的所有选定列表项保持选中状态,因此我决定使用从数据库动态填充的复选框列表。
Below is the new checkboxList: 以下是新的checkboxList:
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: 题:
How do I integrate JavaScript above with this dynamically populated version of the same checkboxList just as I did with the manually populated checkboxList? 像我手动填充的checkboxList一样,如何将上述JavaScript与同一checkboxList的此动态填充的版本集成在一起?
This way, whenever a user completes the form and selects any of the items on the checkboxlist list, the selected item remains selected for a particular user? 这样,每当用户填写表单并选择复选框列表中的任何项目时,所选项目仍然为特定用户选择?
I could do this if I query the DB in another page but users want everything done on one page. 如果我在另一页中查询数据库,但用户希望在一页上完成所有操作,则可以这样做。
You can use the Attributes
collection to add client-side attributes to a ListItem
. 您可以使用
Attributes
集合将客户端属性添加到ListItem
。
Below is some sample code for how to go about it. 下面是一些示例代码。
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
( NB: freehand typed code meant only for explaining the concept. might contain typos) ( 注意:徒手输入的代码仅用于解释概念。可能包含错别字。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.