2

没有找到任何直接回答我的问题的东西,所以希望有人能对此有所了解。

我有两个复合控件,我们称它们为 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>");
    }

}
4

1 回答 1

0

需要ViewState并希望接收事件的控件、自定义控件或其他控件应在Init.

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

您可以使用会话来记住哪些控件是由代码添加的,并在以后的 PostBacks 中添加它们。

于 2012-08-17T21:32:26.287 回答