0

ASPX 页面:

<asp:ListView ID="lvSubjects" runat="server" >
        <LayoutTemplate>
        <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
        </LayoutTemplate>
        <ItemTemplate>
            <asp:CheckBox ID="cbRegularSubjects" Text=<%# Eval("SubjectName") %> runat="server" />

        </ItemTemplate>


        <AlternatingItemTemplate>
        <asp:CheckBox ID="cbRegularSubjects" Text=<%# Eval("SubjectName") %> runat="server" />
        </AlternatingItemTemplate>
        </asp:ListView>

代码背后:

For Each ctrl As Control In Page.Controls
            If TypeOf ctrl Is CheckBox AndAlso CType(ctrl, CheckBox).Checked Then
                '**Here I want to get the text of the check box and insert into the DB**
            End If


       Next

我哪里出错了??我没有收到任何错误...但是这段代码对我不起作用。

4

2 回答 2

0

您只在 Page.Controls 中搜索,而您的复选框在页面控制层次结构的更深处。

foreach (ListViewItem row in listView.Rows)
{
  if (row.ItemType == ListViewItemType.DataItem)
  {
    CheckBox chk = row.FindControl("Checkboxid");
    if  (chk.Checked)
    {
     //Write code to store this checkbox value to database here
    }
   }
  }

请用正确的控件名称更改VB中的代码

于 2012-04-14T04:48:38.887 回答
0
For i As Integer = 0 To lvSubjects.Items.Count - 1
            Dim coll As ControlCollection = lvSubjects.Items(i).Controls
            For Each c As Control In coll
                If TypeOf c Is CheckBox Then
                    Dim box As CheckBox = CType(c, CheckBox)
                    If box.Checked Then
                        MsgBox(box.Text)
                    End If
                End If
            Next c
        Next i
于 2012-04-14T05:36:51.607 回答