none
Formview物件與checkboxlist搭配應用的問題 RRS feed

  • 問題

  • 請教各位前輩
    FORMVIEW的物件是否可以用迴圈的方式 與CHCEKBOXLIST搭配作資料的呈現控制呢?我使用下列的語法
     Protected Sub FormView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.DataBound
            Me.CheckBoxList1.Items.Clear()
            For Each rows As DataControlField In Me.FormView1.DataItem
                Dim ckb As New ListItem
                ckb.Selected = rows.Visible
                ckb.Text = rows.HeaderText
                Me.CheckBoxList1.Items.Add(ckb)
            Next

        End Sub

    但卻會出現錯誤訊息:無法將型別 'System.Data.DataRowView' 的物件轉換為型別 'System.Collections.IEnumerable'

    而我是參考書上的範例改寫的,但書上範例是以GRIDVIEW為範例,如下所示
    Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
            Me.CheckBoxList1.Items.Clear()
            For Each col As DataControlField In Me.GridView1.Columns
                Dim ckb As New ListItem
                ckb.Selected = col.Visible
                ckb.Text = col.HeaderText
                Me.CheckBoxList1.Items.Add(ckb)

            Next
    End Sub

     

    煩請各位前輩指點
    謝謝

    2008年6月13日 上午 06:06

解答

  • Hi 您可試試這段code

    Code Snippet

    protected void FormView1_DataBinding(object sender, EventArgs e)
            {
                string key = FormView1.DataKey.Value.ToString();
                // get the list items stored in DB for this key;
                // I assume its a dataset
                DataSet ds;
                CheckBoxList chklist = FormView1.FindControl("CheckBoxList1");
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ListItem item = chklist.Items.FindByText(dr["yourvalue"].ToString());
                    if (item != null)
                        item.Selected = true;
                }
            }


    2008年6月13日 上午 06:15