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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top