Click here to Skip to main content
11,412,834 members (73,627 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 13-Nov-12 4:50am
Edited 13-Nov-12 14:10pm
Nelek70.7K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

This works great for me now
Thnx
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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

'//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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

'//TextBox1 is disabled when form load
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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox1.Enabled = True
End Sub
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

$("#idOfTextBox").attr("disabled",false);
  Permalink  
Comments
Marcus Kramer at 13-Nov-12 10:23am
   
+5. I Wasn't thinking of web for some reason.
ryanb31 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 414
1 Sascha Lefévre 200
2 Maciej Los 155
3 ProgramFOX 130
4 Sergey Alexandrovich Kryukov 110
0 Sergey Alexandrovich Kryukov 9,025
1 OriginalGriff 7,247
2 Maciej Los 3,525
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web02 | 2.8.150427.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100