0

I am trying to add a check box, label and DDL to ASP.NET page (aspx) from my back class in C#. I have been using LiteralControl _liText = new LiteralControl(); to attach label so that I can show them using this.Controls.Add(_liText);in CreateChildControls() method.

How do I add DDL and check box to ASp.NET page from C# code so that my label is in the same line with DDL and checkbox?

I have already made DDL using this syntax:

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

Problem is not in this.Controls.Add() which I call from CreateChildControls(). It is OnPreRender() method where I fill ddl and check box. Is LiteralControl class good for this? Here is what I have tried in OnPReRender():

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

                }
            }
4

2 回答 2

2

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

您需要将控件添加到页面

Page.Controls.Add(_ddlCollection);

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

panel1.Controls.Add(_ddlCollection);

您正在添加一个我认为不是您想要的下拉列表。您需要改为添加 ListItems。

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"});

复选框上的文本属性将是复选框的标签,并在您单击文本时勾选/取消勾选复选框。

输出应该是

<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>
于 2013-04-25T08:39:40.953 回答
1

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

<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;
    }
}
于 2013-04-25T08:57:52.337 回答