Custom control - why can I not access its value on PostBack

P

Paul

Hi all,

I have a simple custom control with a label and a text box. The text
control's value is persisting when I do a post back, so the view state
is working, but I cannot access the value in the code. Is there
anything different I have to do to expose the value of the text box?

testControl.Text returns the unchanged original value, not the new
value.

Thanks,

Paul

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace Test
{
public class TestControl : Control, INamingContainer
{
protected Label label;
protected TextBox textBox;

public string Title
{
get { return (string)ViewState["Title"]; }
set { ViewState["Title"] = value; }
}
public string Text
{
get { return (string)ViewState["Text"]; }
set { ViewState["Text"] = value; }
}

protected override void CreateChildControls()
{
TableRow row = new TableRow();
Controls.Add(row);

TableCell cell = new TableCell();
row.Controls.Add(cell);

label = new Label();
label.EnableViewState = false;
label.Text = Title;
cell.Controls.Add(label);

cell.Controls.Add(new LiteralControl("  "));

textBox = new TextBox();
textBox.Text = Text;
Page.Response.Write(textBox.Text);
cell.Controls.Add(textBox);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top