以编程方式将子控件添加到asp.net复合控件

时间:2012-06-27 08:23:08

标签: c# asp.net webforms custom-server-controls

找不到任何可以直接回答我问题的内容,所以希望有人可以对此有所了解。

我有两个复合控件,我们称之为BudgetTable和BudgetTableItem,其中BudgetTable包含一个BudgetTableItem列表。

到目前为止,只要我在HTML视图中添加新的RowItems,一切都有效 - 当我以编程方式添加一个时,它会出现,但不会在回发中存活。

我只能假设我正在使用ViewState进行骨干治疗,并且非常感谢任何指针!

提前致谢。

HTML:

    <hea:BudgetTable runat="server" ID="btTest" MaximumFundingAvailable="7000" CssClass="bob">
            <Items>
                    <hea:BudgetTableItem runat="server" Description="Test1" />
                    <hea:BudgetTableItem runat="server" Description="Test2" />
                    <hea:BudgetTableItem runat="server" Description="Test3" />
            </Items>        
    </hea:BudgetTable>

背后的代码:

    [PersistenceMode(PersistenceMode.InnerProperty)]
[ParseChildren(true)]
public class BudgetTableItem : CompositeControl {

    private TextBox _description = new TextBox();

    private TextBox _cost = new TextBox();

    private CheckBox _heaFunded = new CheckBox();

    /*public delegate void AddRow();
    public delegate void RemoveRow(BudgetTableItem item);

    public event AddRow AddNewRow;
    public event RemoveRow RemoveNewRow;*/

    public string ItemName {
        get {
            var viewstate = ViewState["ItemName"];
            return (viewstate is string) ? (string)viewstate : "default";
        }
        set {
            ViewState["ItemName"] = value;
        }
    }

    public bool ShowRemoveRow {
        get {
            var viewstate = ViewState["ShowRemoveRow"];
            return (viewstate != null && viewstate is bool) ? (bool)viewstate : false;
        }
        set {
            ViewState["ShowRemoveRow"] = value;
        }
    }

    public bool ShowAddRow {
        get {
            var viewstate = ViewState["ShowAddRow"];
            return (viewstate != null && viewstate is bool) ? (bool)viewstate : false;
        }
        set {
            ViewState["ShowAddRow"] = value;
        }
    }

    public string Description {
        get {
            return _description.Text;
        }
        set {
            _description.Text = value;
        }
    }

    public decimal Cost {
        get {
            decimal cost =0;
            decimal.TryParse(_cost.Text, out cost);
            return cost;
        }
        set {
            _cost.Text = value.ToString();
        }
    }

    public bool HeaFunded {
        get {
            return _heaFunded.Checked;
        }
        set {
            _heaFunded.Checked = value;
        }
    }

    protected override void CreateChildControls() {

        Controls.Clear();

        HtmlTableCell tableCell1 = new HtmlTableCell();
        HtmlTableCell tableCell2 = new HtmlTableCell();
        HtmlTableCell tableCell3 = new HtmlTableCell();
        HtmlTableCell tableCell4 = new HtmlTableCell();

        tableCell1.Attributes.Add("class", "col1");
        tableCell2.Attributes.Add("class", "col2");
        tableCell3.Attributes.Add("class", "col3");
        tableCell4.Attributes.Add("class", "col4");

        tableCell1.Controls.Add(_description);
        tableCell2.Controls.Add(_cost);
        tableCell3.Controls.Add(_heaFunded);

        /*if (ShowAddRow || ShowRemoveRow) {

            Button addNewButton = new Button();

            addNewButton.Text = (ShowAddRow) ? "Add Row" : "Remove";

            if (ShowAddRow) {                   
                addNewButton.Click += new EventHandler(addNewButton_Click);
            }

            if (ShowRemoveRow) {
                addNewButton.Click += new EventHandler(removeButton_Click);
            }

            tableCell4.Controls.Add(addNewButton);
        }
        else{*/
            tableCell4.InnerHtml = "&nbsp;";
        //}

        Controls.Add(tableCell1);
        Controls.Add(tableCell2);
        Controls.Add(tableCell3);
        Controls.Add(tableCell4);
    }

    /*void addNewButton_Click(object sender, EventArgs e) {
        if (AddNewRow != null) {
            AddNewRow();
        }
    }*/

    /*void removeButton_Click(object sender, EventArgs e) {
        if (RemoveNewRow != null) {
            RemoveNewRow(this);
        }
    }*/

    protected override void RecreateChildControls() {
        EnsureChildControls();
    }

    public override void RenderBeginTag(HtmlTextWriter writer) {
        writer.Write("<tr>");
    }

    public override void RenderEndTag(HtmlTextWriter writer) {
        writer.Write("</tr>");
    }

}

1 个答案:

答案 0 :(得分:0)

应在ViewState中创建需要Init并希望接收事件的自定义或其他设置。

Http是无国籍的。您的整个页面及其所有控件都会在每次回发时重新创建。您在设计视图中添加的控件将添加到designer.cs文件中,并为您创建。当您自己添加控件时,必须编写代码以在稍后发生的每个PostBack上重新创建控件。

您可以使用会话记住代码添加了哪些控件,并在以后的PostBack上添加它们。