如何在复选框列表中获取当前选中的复选框索引 [英] how to get currect selected checkbox index in checkboxlist
问题描述
在我的复选框列表中有一些类,如1-b.tech(cse)2-b.tech(it)3-b.tech(ece)4-b.tech(me)当我选择b.tech( ece)它给出b.tech(ece)的正确classid然后我选择b.tech(me)它再次给b.tech(我)的classid如果我选择b.tech(it)或b.tech( cse)然后它给出正确的classid。基本上它选择了cleckbox列表中第一个选定的classid。如何在每个选择中选择当前选定的classid?
1-b.tech(cse)2-b.tech(它)3-b.tech(ece)4-b.tech(我)
i我正在使用此代码 -
asp.net代码 -
< td > < b > 类:< / b > < / td >
< td > < asp:CheckBoxList ID = CheckBoxList1 runat = server AutoPostBack = true
RepeatDirection = < span class =code-keyword>水平 DataSourceID = SqlDataSource1
DataTextField = 类 DataValueField = classesid >
< / asp:CheckBoxList >
< / td >
vb.net代码 -
受保护的 Sub CheckBoxList1_Sel ectedIndexChanged( ByVal sender 作为 对象,< span class =code-keyword> ByVal e As System.EventArgs)句柄 CheckBoxList1 .SelectedIndexChanged
Dim c As 字符串
Dim d 作为 字符串
Dim f 作为 字符串
对于 i As < span class =code-keyword>整数 = 0 CheckBoxList1.Items.Count - 1
如果 CheckBoxList1.Items(i).Selected 那么
cmd.CommandText = 从classesid =& CheckBoxList1.SelectedValue&
c = cmd.ExecuteScalar
Label1.Text = c
cmd.CommandText = 从stu选择计数(studentid) classesid =& CheckBoxList1.SelectedValue&
d = cmd.ExecuteScalar
Label2.Text = d
cmd.CommandText = 从矩阵中选择count(*),其中classesid =& ; CheckBoxList1.SelectedValue&
f = cmd.ExecuteScalar
Label3.Text = f
Label4.Text = CInt (Label2.Text) - CInt (Label3.Text)
否则
结束 如果
下一步
结束 Sub
结束 类
string result = Request.Form [ __ EVENTTARGET];
string [] checkedBox = result.Split('
跨度>); ;
int index = int .Parse(checkedBox [checkedBox.Length - 1 跨度>]);
if (cbYears.Items [index] .Selected)
{
// 您的逻辑
}
其他
{
// 您的逻辑
}
protected void CheckBoxList1_SelectedIndexChanged( object sender,EventArgs e)
{
string value = string .Empty;
string result = Request.Form [ __ EVENTTARGET跨度>];
string [] checkedBox = result.Split('
in my checkboxlist there are some classes like 1-b.tech(cse) 2-b.tech(it) 3-b.tech(ece) 4-b.tech(me) when i select b.tech(ece) it give the correct classid of b.tech(ece) and then i select b.tech(me) it give the classid of b.tech(me) again if i select b.tech(it) or b.tech(cse) then it give right classid. basically it pick the first selected classid in cleckbox list. how can i pick the current selected classid on each selection?
1-b.tech(cse) 2-b.tech(it) 3-b.tech(ece) 4-b.tech(me)
i am using this code--
asp.net code-
<td><b>Class:</b></td>
<td><asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true"
RepeatDirection="Horizontal" DataSourceID="SqlDataSource1"
DataTextField="classes" DataValueField="classesid">
</asp:CheckBoxList>
</td>
vb.net code--
Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
Dim c As String
Dim d As String
Dim f As String
For i As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
cmd.CommandText = "select classes from classes where classesid=" & CheckBoxList1.SelectedValue & ""
c = cmd.ExecuteScalar
Label1.Text = c
cmd.CommandText = "select count(studentid) from stu where classesid=" & CheckBoxList1.SelectedValue & ""
d = cmd.ExecuteScalar
Label2.Text = d
cmd.CommandText = "select count(*) from matrix where classesid=" & CheckBoxList1.SelectedValue & ""
f = cmd.ExecuteScalar
Label3.Text = f
Label4.Text = CInt(Label2.Text) - CInt(Label3.Text)
Else
End If
Next
End Sub
End Class
string result = Request.Form["__EVENTTARGET"]; string [] checkedBox = result.Split('
'); ; int index = int.Parse(checkedBox[checkedBox.Length - 1]); if (cbYears.Items[index].Selected) { //your logic } else { //your logic }
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) { string value = string.Empty; string result = Request.Form["__EVENTTARGET"]; string[] checkedBox = result.Split('
这篇关于如何在复选框列表中获取当前选中的复选框索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!