Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I enable a text box on clicking a button?
firstly I insert the property readonly="true" to the textbox.
what code should i use to set the property readonly=false for the textbox when i click the button

OP's additional information moved from non-solution 3 below
i write this one in the click handler

txtID.Attributes("readonly", "false")

but no success :/
Posted
Updated 13-Nov-12 13:10pm
v2

If your code is posting back then in the C# you can set ReadOnly = false. If you want to do it on client side I would recommend using jQuery and then you can do

JavaScript
$("#idOfTextBox").attr("disabled",false);
 
Share this answer
 
Comments
fjdiewornncalwe 13-Nov-12 10:23am    
+5. I Wasn't thinking of web for some reason.
ZurdoDev 13-Nov-12 10:29am    
Ha. Good point. I went back and looked and it was not tagged ASP.Net. It is so trivial in WinForms I assumed it must be the web.
1) Add a click handler to your code behind and tie it to your Button control.
2) In the click handler, write something like YourTextBox.readonly = false
3) That's it.

Cheers.

Edit: After seeing your answer that this is a web app, See ryan's answer as the way to go.
 
Share this answer
 
v2
'//TextBox1 is disabled when form load
VB
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       TextBox1.Enabled = False
   End Sub

'//TextBox1 is enabled when you click on Button1
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox1.Enabled = True
End Sub
 
Share this answer
 
This works great for me now
Thnx
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       txt1.Disabled = True


   End Sub

   Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

       txt1.Disabled = False

 End Sub
 
Share this answer
 
VB
'//Or if you wanna switch between enable/disable,you can use CheckBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    TextBox1.Enabled = False
    TextBox1.BackColor = Color.Red
End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked = True Then
        TextBox1.Enabled = True
        TextBox1.BackColor = Color.Green
    Else
        TextBox1.Enabled = False
        TextBox1.BackColor = Color.Red
    End If

End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900