2011-01-07 2 views
2

이것은 매우 기본적인 질문입니다.하지만 VB에서이 작업을 수행하는 방법을 찾지 못했습니다 ... CheckBoxList가 있습니다. 자신의 가치를 채울 수있는 텍스트 상자가 있습니다. 그래서 그 체크 박스 (CheckBoxList의 ListItem)가 체크되었을 때 그 텍스트 박스가 활성화되도록해야합니다. 이것은 코드 뒤에, 나는 특정 ListItem가 검사되는지 테스트하기 위해 If 문에 무엇을 넣을 지 잘 모르겠다.ASP.NET, VB : CheckBoxList의 어떤 항목이 선택되어 있는지 확인

Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged 
    If ___ Then 
     txtelect.Enabled = True 
    Else 
     txtelect.Enabled = False 
    End If 
End Sub 
+0

CheckBoxList와 Textbox의 aspx 마크 업을 보여줄 수 있습니까? –

답변

8

당신은 루프 수 CheckBoxList의 체크 상자를 통해 각 항목이 선택되어 있는지 확인합니다. 위의 코드는 CheckBoxList의 SelectedIndexChanged 이벤트 처리기에 배치 될

For Each li As ListItem In CheckBoxList1.Items 
    If li.Value = "ValueOfInterest" Then 
     'Ok, this is the CheckBox we care about to determine if the TextBox should be enabled... is the CheckBox checked? 
     If li.Selected Then 
      'Yes, it is! Enable TextBox 
      MyTextBox.Enabled = True 
     Else 
      'It is not checked, disable TextBox 
      MyTextBox.Enabled = False 
     End If 
    End If 
Next 

: 이런 식으로 뭔가를 시도하십시오.

0

당신의 영문이 유사 가정하면 :

<asp:TextBox ID="txtelect" runat="server"></asp:TextBox> 
    <asp:CheckBoxList id="CheckBoxList1" runat="server" autopostback="true" > 
     <asp:ListItem Text="enable TextBox" Value="0" Selected="True"></asp:ListItem> 
     <asp:ListItem Text="1" Value="1" ></asp:ListItem> 
     <asp:ListItem Text="2" Value="2" ></asp:ListItem> 
     <asp:ListItem Text="3" Value="3" ></asp:ListItem> 
    </asp:CheckBoxList> 
이 당신의 텍스트 상자가 활성화 할 필요가있는 경우

당신은 확인하기 위해 ListItem's- Selected 속성을 사용할 수 있습니다 :

Private Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged 
     'use the index of the ListItem where the user can enable the TextBox(starts with 0)' 
     txtelect.Enabled = CheckBoxList1.Items(0).Selected 
    End Sub 
0

나는 이렇게하지 않을 것이고, 매우 비효율적이다. 텍스트 상자를 활성화 또는 비활성화하기 위해 서버를 누르기 만하면 javascript를 사용해야합니다. 아래 코드가 더 좋을 것입니다.

<asp:DataList ID="mylist" runat="server"> 
     <ItemTemplate> 
      <input type="checkbox" id="chk<%#Container.ItemIndex %>" onclick="document.getElementById('txt<%#Container.ItemIndex %>').disabled=(!this.checked);" /> 
      <input type="text" id="txt<%#Container.ItemIndex %>" disabled="disabled" /> 
     </ItemTemplate> 
    </asp:DataList> 
관련 문제