2011-01-07 16 views
2

これは非常に基本的な質問ですが、VBでこれを行う方法を見つけることができませんでした... CheckBoxListあなた自身の価値を埋めるためのテキストボックスが含まれています。だから、チェックボックス(CheckBoxListのListItem)がチェックされているときに、そのテキストボックスを有効にする必要があります。これはコードの背後にある、私は、特定のListItemがチェックされているかどうかをテストするために、If文に何を入れるべきかわからない。ASP.NET、VB:CheckBoxListのどの項目が選択されているかを確認する

あなたのASPXが次のようになりと仮定すると
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> 
関連する問題