2012-04-14 95 views
0

ASPX页面:通过复选框循环和插入复选框值DB

<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 

我要去哪里错了?我没有得到任何错误。 ..但此代码不适合我。

回答

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 
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代码

+0

列表视图是在面板内部和复选框是列表视图里面......我想点击一个按钮执行代码...我怎么去呢? – user1150440 2012-04-14 05:10:43

+0

,我也在使用母版页。 – user1150440 2012-04-14 05:11:56

+0

我得到'ItemRow'未定义。 – user1150440 2012-04-14 05:26:55