Wednesday, December 5, 2012

Preserve TextBox value on PostBack when disabled

In ASP.NET, the TextBox text is not transferred back to the server when the TextBox is disabled.  If you want the TextBox to post back the text, you can add a readonly attribute to the TextBox in the code behind.

protected void Page_Load(object sender, EventArgs e)
{
     TB1.Attributes.Add("readonly", "readonly");
}

You could also add some style to the TextBox so it appears disabled:
.disabled
{
     color:#ACA899;
}   <asp:textbox id="TB1" Runat="server" CssClass="disabled"></asp:textbox>