Note that there are some explanatory texts on larger screens.

plurals
  1. POProgramically adding child controls to a asp.net composite control
    primarykey
    data
    text
    <p>Not found anything that directly answers my problem, so hopefully someone can shed some light on it.</p> <p>I have two Composite Controls, lets call them BudgetTable and BudgetTableItem, where BudgetTable contains a list of BudgetTableItem.</p> <p>So far everything works so long as I add new RowItems in the HTML View - when I add one programmatically it appears, but doesn't survive postback.</p> <p>I can only assume I'm doing something boneheaded with ViewState, and would appreciate any pointers!</p> <p>Thanks in advance.</p> <p>The HTML:</p> <pre><code> &lt;hea:BudgetTable runat="server" ID="btTest" MaximumFundingAvailable="7000" CssClass="bob"&gt; &lt;Items&gt; &lt;hea:BudgetTableItem runat="server" Description="Test1" /&gt; &lt;hea:BudgetTableItem runat="server" Description="Test2" /&gt; &lt;hea:BudgetTableItem runat="server" Description="Test3" /&gt; &lt;/Items&gt; &lt;/hea:BudgetTable&gt; </code></pre> <p>The code behind:</p> <pre><code> [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 &amp;&amp; viewstate is bool) ? (bool)viewstate : false; } set { ViewState["ShowRemoveRow"] = value; } } public bool ShowAddRow { get { var viewstate = ViewState["ShowAddRow"]; return (viewstate != null &amp;&amp; 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 = "&amp;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("&lt;tr&gt;"); } public override void RenderEndTag(HtmlTextWriter writer) { writer.Write("&lt;/tr&gt;"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload