如何添加复选框,标签和DDL到ASP.. NET页面编程

本文关键字:ASP DDL NET 编程 标签 何添加 添加 复选框 | 更新日期: 2023-09-27 18:11:01

我试图添加一个复选框,标签和DDL到ASP。. NET页面(aspx)从我的后台类在c#。我一直在使用LiteralControl _liText = new LiteralControl();附加标签,以便我可以在CreateChildControls()方法中使用this.Controls.Add(_liText);显示它们。

我如何添加DDL和复选框的ASp。. NET页面从c#代码,使我的标签是在同一行DDL和复选框?

我已经使用这种语法创建了DDL:

List<DropDownList> _ddlCollection=new List<DropDownList>();
for (int i = 0; i < 5; i++)
            {
                _ddlCollection.Add(new DropDownList());
            }

问题不在this.Controls.Add()中,我从CreateChildControls()中调用。它是OnPreRender()方法,我填充ddl和复选框。LiteralControl类适合这个吗?以下是我在OnPReRender()中尝试过的:

 foreach (SPList list in web.Lists)
            {
                if (!list.Hidden)
                {
                    _liText.Text += @<input type="checkbox">;
                    _liText.Text += list.Title + "<br />";
                }
            }

如何添加复选框,标签和DDL到ASP.. NET页面编程

您的控件存在,但页面或用户控件不知道它们。

您需要将控件添加到页面

Page.Controls.Add(_ddlCollection);

您还可以将您的控件添加到页面上的其他控件,例如面板。

panel1.Controls.Add(_ddlCollection);

你正在添加一个下拉列表,我认为这不是你想要的。您需要添加listtitems。

var dropDown = new DropDownList {Id = "dropDown1"};
dropDown.Items.Add(new ListItem("text", "value");
Page.Controls.Add(dropDown);

为下拉菜单添加一个标签。

Page.Controls.Add(new Label {AssociatedControlId = dropDown.Id, Text = "Drop me down"});

对于您的其他控件,请遵循相同的过程:

添加复选框不要在文本中添加html控件,除非你确实需要这样做。如果您希望能够访问后面的代码中的复选框,那么您需要将其添加为asp.net控件。使用占位符

placeHolder1.Controls.Add(new CheckBox {Id = "chkBox", Text="Tick me"});

复选框上的text属性将成为该复选框的标签,并在单击文本时勾选/取消勾选该复选框。

输出应为

<label for="dropDown1" >Drop me down</label>
<select id="dropDown1" >
    <option value="value" >text</option>
</select>    
<input type="checkbox" id="chkbox" />
<label for="chkbox" >Tick me</label>

首先,您应该添加占位符。

<form id="form1" runat="server">
    <asp:PlaceHolder runat="server" ID="phMain"></asp:PlaceHolder>
</form>

接下来,如果你添加了一行,你使用表格(这是一个简单但不推荐的方法,下一步将所有添加到页面并设置页面的css样式)。

protected override void CreateChildControls()
{
    base.CreateChildControls();
    Table table = new Table();
    for (int i = 0; i < 3; i++)
    {
        TableRow tr = new TableRow();
        TableCell tc1 = new TableCell();
        tc1.Controls.Add(new LiteralControl(String.Format("Line {0}",i)));
        tr.Cells.Add(tc1);
        TableCell tc2 = new TableCell();
        CheckBox chb = new CheckBox();
        chb.ID = String.Format("CheckBox_{0}", i);
        chb.Text = String.Format("CheckBox {0}", i);
        chb.CheckedChanged += chb_CheckedChanged;
        chb.AutoPostBack = true;
        tc2.Controls.Add(chb);
        tr.Cells.Add(tc2);
        TableCell tc3 = new TableCell();
        DropDownList ddl = new DropDownList();
        ddl.ID = String.Format("DropDownList_{0}", i);
        ddl.Items.Add("1111");
        ddl.Items.Add("2222");
        ddl.Items.Add("3333");
        ddl.SelectedIndex = i;
        ddl.Enabled = false;
        tc3.Controls.Add(ddl);
        tr.Cells.Add(tc3);

        table.Rows.Add(tr);
    }
    phMain.Controls.Add(table);
}
void chb_CheckedChanged(object sender, EventArgs e)
{
    CheckBox chb = sender as CheckBox;
    string ddlid = chb.ID.Replace("CheckBox", "DropDownList");
    DropDownList ddl = this.Page.FindControl(ddlid) as DropDownList;
    if (ddl != null)
    {
        ddl.Enabled = chb.Checked;
    }
}