ASP.NET使用foreach遍历控件

前端代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>





    
    


    



后台代码


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        foreach (Control ctl in control.Controls)
        {
            CheckBox chbox;
            if (ctl is CheckBox)
            {
                chbox = (CheckBox)ctl;
                if (chbox.Checked)
                {
                    TextBox1.Text += chbox.Text + ",";
                }
            }
        } 
        
            
        
    }
}

效果如下图

ASP.NET使用foreach遍历控件_第1张图片

你可能感兴趣的:(ASP.Net)