views:

97

answers:

2

I need to change the readonly property of a asptextbox from the code behind based on certain conditions.

I know how to run a function when the page loads, but I don't know how to reference the form fields to change the readonly property.

Can anyone point me in the right direction?

Thanks in advance,

Anthony

A: 

The ASP Textbox also has its own ReadOnly property, but none of the other ASP input form elements have it.

You can access the similar disabled html attribute through the Enabled property.

MyTextbox.Enabled = False 'makes control readonly

More info here

dtryan
This is my text box.<asp:TextBox ID="Req_DateTextBox" runat="server" Text='<%# Bind("Req_Date", "{0:d}") %>'If I use "Req_DateTextBox.Attributes("readonly") = True" I get an error saying "Req_DateTextBox is not declared"
Anthony
@Dtryan enabled property in asp.net gets rendered as a "disabled" html attribute. The definition of it is: "A disabled input element is unusable and un-clickable." Read.Only gets rendered as well read only and its definition is: "A read-only field cannot be modified. However, a user can tab to it, highlight it, and copy the text from it."
chobo2
@Anthony, if you can't refer to your textbox from the code behind, its declaration is probably not in the designer file. If you don't have a designer file, you will need to declare it yourself, e.g. public withevents Req_DateTextBox as new Textbox
dtryan
@chobo2 thanks for that clarification
dtryan
A: 

I think dtryan is wrong. If you use Enabled then you cannot type anything at all int he box. If you want to use read only then use the read only property.

Like this

 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

protected void Page_Load(object sender, EventArgs e)
{
    // some text that is put into the textbox.
    TextBox1.Text = "Some Text that you can't edit but can copy out of.";
    // if read only is set to true then you can't change the text but copy it out.
    TextBox1.ReadOnly = true;

    // if enabled is set to false then you can't change the text or copy the text out.
   //TextBox1.Enabled = false;
}

if this is your code

<asp:TextBox ID="Req_DateTextBox" runat="server" Text='<%# Bind("Req_Date", "{0:d}") %>

Then it would be Req_DateTextBox.ReadOnly = true;

chobo2
protected void Page_Load(object sender, EventArgs e){ Req_DateTextBox.ReadOnly = true;}Get's me two errors. End of statements expected and declaration expected.
Anthony
You have to show all your code for that page and the code behind. When you start to type Req_ does intellisense popup wanting to fill in the rest name? If not then something is wrong.
chobo2
Also looking at what you pasted for you html control do you actaully close the asp textbox like this </asp:TextBox> since that could be a reason why it's not working.
chobo2