2010-11-19 163 views
1

我想插入複選框文本,只有當他們被選中。 ..如何做到這一點..如何只添加選中的複選框添加到數組列表中?

保護小組的Page_Load(BYVAL發件人爲對象,BYVALË作爲System.EventArgs)把手Me.Load TextBox1.Text =的Request.QueryString(名爲 「txt」) 昏暗分裂作爲字符串()= TextBox1.Text.Split( 「 」) 每個ID作爲字符串在分裂 昏暗CTRL作爲對照= Page.FindControl(「 複選框」 & ID)

 If Not ctrl Is Nothing Then 
      Dim chkbox As CheckBox = DirectCast(ctrl, CheckBox) 
      chkbox.Enabled = False 
      Dim arrList As New ArrayList() 
      'populate the list with some temp values 
      arrList.Add(CheckBox1.Text) 
      arrList.Add(CheckBox2.Text) 

      'databind the list to our repeater 
      Repeater1.DataSource = arrList 
      Repeater1.DataBind() 
     End If 
    Next 
End Sub 

此代碼將添加所有複選框不管它是否被檢查!

任何機構可以做到這一點......因此只有選中的複選框會在數組列表中添加

回答

3

是,這是你期待什麼呢?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 

TextBox1.Text = Request.QueryString("txt") 
Dim splitted As String() = TextBox1.Text.Split(",") 

For Each id As String In splitted 
    Dim ctrl As Control = Page.FindControl("checkbox" & id) 

     If Not ctrl Is Nothing Then 
      Dim chkbox As CheckBox = DirectCast(ctrl, CheckBox) 
      chkbox.Enabled = False 
      Dim arrList As New ArrayList() 
      'populate the list with some temp values 
      if chkbox.Checked then 
        arrList.Add(chkbox.Text) 
      end if 

      'databind the list to our repeater 
      Repeater1.DataSource = arrList 
      Repeater1.DataBind() 
     End If 
    Next 
End Sub