24h購物| | PChome| 登入
2012-12-20 16:12:00| 人氣1,539| 回應0 | 上一篇 | 下一篇

CheckBoxList 使用法

推薦 0 收藏 0 轉貼0 訂閱站台

如何使用CheckBoxList

    '將從DB取出來的CheckBoxList資料顯示於CheckboxList
    Sub Get_CheckboxList()
        sqlcmd.Length = 0
        '從DB撈出資料, 放入DT
        Dim dt As New DataTable
        sqlcmd.Append("Select ID, NAME from AAA")
        cmd.Connection = myconn
        cmd.CommandText = sqlcmd.ToString
        adp = New SqlDataAdapter(sqlcmd.ToString, myconn)
        adp.Fill(dt) '塞入DT

        '顯示於CHECKBOXList
        Me.Ckblist.DataSource = dt
        Me.Ckblist.DataValueField = dt.Columns("ID").ToString
        Me.Ckblist.DataTextField = dt.Columns("NAME").ToString
        Me.Ckblist.DataBind()
    End Sub

'在js中, 取出被勾選的值
        '付款方式 Payment
        str += "var paymentValue = '';"
        str += "var i = 0;"
        str += "var j = 0;"
        str += "var checkboxlist = document.getElementById('" & Me.Ckblist.ClientID & "');"
        str += "for (i=0;i<checkboxlist.rows.length;i++)"
        str += "{"
        str += "for (j=0;j<checkboxlist.rows[i].cells.length;j++)"
        str += "{"
        str += "if (checkboxlist.rows[i].cells[j].childNodes[0].checked==true)"
        str += "{"
        str += "paymentValue = paymentValue + checkboxlist.rows[i].cells[j].childNodes[1].innerText +',';"
        str += "}"
        str += "}"
        str += "}"
        str += "if (paymentValue=='')"
        str += "{"
        str += "alert('請勾選!');"
        str += "checkboxlist.rows[0].cells[0].focus();"
        str += "return false;"
        str += "}"

    'vb後端 取出被勾選的值
    Sub getSelectedcheckboxlist()
        Dim SelectedValue As String = ""
        For Each item As ListItem In Ckblist.Items
            If item.Selected Then
                SelectedValue &= item.Value & "," 'item.Value存值, item.Text存字樣
            End If
        Next
        Me.Hidden_Payment.Value = SelectedValue
    End Sub

        '顯示資料
        '重新再跑一次迴圈, 如果裡面選項的值跟資料庫的值一致 就勾選起來
        '先把資料庫中的值切成array
    '我是把勾選起來的值轉成字串再存入DB的, 如果勾選了A,B,C3項, 會轉成"A,B,C"這樣的字串存入DB
        '所以取出時會先取得字串, 要再切成Ary, 一個個塞回控制項
        Dim ary_payment As Array = ShowData_ary(25).ToString.Split(",") '切成array
        For k = 0 To ary_payment.Length - 1
            For Each item As ListItem In Ckblist.Items
                If item.Value = ary_payment(k) Then
                    item.Selected = True
                End If
            Next
        Next

台長: nut
人氣(1,539) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: 技術 |
此分類下一篇:[好文]電腦中毒怎辦?工作管理員的解毒方法
此分類上一篇:C# vs Crystal Report vs MSSQL 登入問題.

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文