通过sql 找到要用的数据,然后做成checkbox可以复选的形式。 
               int i = 0;
                while (dr.Read())
                {
                    //detailPanel.Visible = false;
                    CheckBox cb = new CheckBox();
                    cb.ID = "cb" + i;
                    cb.Text = dr[1].ToString();
                    errorPanel1.Controls.Add(cb);
                    i++;
                }
                Session.Add("errorCount", i);  
所有的checkbox都在errorPanel1这个Panel里面自动生成。
页面生成了下面这样的checkbox
<input id="cb0" type="checkbox" name="cb0" />但是选中提交以后
再用errorPanel1.FindControl("cb" + j.ToString())找不到这些checkbox呢?
我看网上说是要实现 INamingContainer接口,我试着重写了还是不行?
只实现这个接口的类怎么写?
public class ErrorPanel:Panel, INamingContainer
{   public ErrorPanle():base() {}  }
这么写有问题么? 还有可否直接用Panel找到内含的控件个数?我没办法只能在动态加的时候写到session里去。
多谢了。

解决方案 »

  1.   

    不太推荐使用动态生成的服务端控件
    其实也可以把查询的数据绑定到checkboxlist控件,绑定使用datasource数据源,这样取值就方便了。
      

  2.   

    thx 你的建议不错 不过上面这个问题我也想解决一下 以后要在遇到动态添加的也是事啊
      

  3.   

    页面代码:    <form id="form1" runat="server">
        <div>
        <asp:TextBox ID="TextBox1" runat="server" Width="338px"></asp:TextBox>
        <br />
        <asp:Panel ID="Panel1" runat="server"></asp:Panel>
        </div>
        </form>==============================================================================
    C#代码:public partial class Default2 : System.Web.UI.Page
    {
        CheckBox[] cb= new CheckBox[10];    protected void Page_Init(object sender,EventArgs e)
        {
            for (int iLoop = 0; iLoop < 10; iLoop++)
            {
                cb[iLoop] = new CheckBox();
                cb[iLoop].Text = "CB" + iLoop.ToString();
                cb[iLoop].AutoPostBack = true;
                cb[iLoop].CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
                Panel1.Controls.Add(cb[iLoop]);
            }
        }    protected void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            TextBox1.Text += ((CheckBox)sender).Text + ",";
        }
    }效果图:
      

  4.   

    CheckBoxList 如何重写ListItem 我想自定义一个属性 3Q
    我想直接重写ListItem 但好像不行
      

  5.   

    当然能在提交后找到哪个些作了选择啦,看下面的代码:aspx页:    <form id="form1" runat="server">
        <div>
        <asp:TextBox ID="TextBox1" runat="server" Width="338px"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="提交" />
        <br />
        <asp:Panel ID="Panel1" runat="server"></asp:Panel>
        </div>
        </form>
    CS页:
        CheckBox[] cb= new CheckBox[10];    protected void Page_Init(object sender,EventArgs e)
        {
            for (int iLoop = 0; iLoop < 10; iLoop++)
            {
                cb[iLoop] = new CheckBox();
                cb[iLoop].Text = "CB" + iLoop.ToString();
                Panel1.Controls.Add(cb[iLoop]);
            }
        }    protected void Button1_Click(object sender, EventArgs e)
        {
            string CBList = string.Empty;
            for (int iLoop = 0; iLoop < 10; iLoop++)
            {
                if (cb[iLoop].Checked)
                {
                    CBList += (cb[iLoop].Text + ",");
                }
            }
          效果图:图1
    图2