What I have?
I have a simple web part which has a Table
. The table has two controls, a TextBox
and a Button
. In CreateChildControls()
method, I add the controls to table if !Page.IsPostBack
is true. And, table has view state enabled.
What I want to do?
I want the controls in the table to be present after the post back.
What problem am I facing?
I except the the controls, TextBox
and Button
to be present in the table after the post back. But it is not happening.
I feel building the whole table in every post back is little costly and enabling view state will solve this problem.
Can anyone tell if I am missing something?
Thanks in advance!
Update:
I tried setting EnbleViewState
property of web part. Still the same result.
Code:
public class TreeWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private Table table;
private Button clickMe;
private TextBox content;
protected override void CreateChildControls()
{
base.CreateChildControls();
BuildTable();
}
private void BuildTable()
{
table = new Table();
clickMe = new Button();
content = new TextBox();
table.ID = "myTable";
table.EnableViewState = true;
if (!this.Page.IsPostBack)
{
clickMe.Text = "Click Me!";
clickMe.Click += new EventHandler(clickMe_Click);
content.Text = "Click button to set text";
content.Width = Unit.Pixel(200);
TableCell cell = new TableCell();
cell.Controls.Add(content);
TableRow tr = new TableRow();
tr.Cells.Add(cell);
table.Rows.Add(tr);
cell = new TableCell();
cell.Controls.Add(clickMe);
tr = new TableRow();
tr.Cells.Add(cell);
table.Rows.Add(tr);
}
this.Controls.Add(table);
}
protected void clickMe_Click(object sender, EventArgs e)
{
content.Text = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
}
}