Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 2nd, 2009, 08:37 PM
Authorized User
Points: 100, Level: 1
Points: 100, Level: 1 Points: 100, Level: 1 Points: 100, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
Question disabling a button with textbox

I dont' know how to disable a button when there is no text in a textbox an enable it the moment a character is enterd to textbox!!!!!!!!!

I use textChanged event but it doesn't work correctly.for example when I delete the text(I mean all characters in textbox) the button is enabled but after pressing a BackSpace it becomes disabled.

How can I overcome this problem?Is there any other events to control this?
or any other suggestions?

I need your help.....
Reply With Quote
  #2 (permalink)  
Old November 4th, 2009, 03:16 PM
Registered User
 
Join Date: Oct 2009
Location: NYC
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Smile Try this

Try defining sub for text_changed event for textbox object which reacts to lenght of textbox object. Don't forget to set the AutoPostBack property of the TextBox to "True".

A solution in vb (page8.aspx with page8.aspx.vb as code-behind file) would be:
page8.aspx
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Page8.aspx.vb" Inherits="Page8" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Enabled="False" Text="Button" />
    
    </div>
    </form>
</body>
</html>
page8.aspx.vb
Code:
Partial Class Page8
    Inherits System.Web.UI.Page

    Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Len(CType(sender, TextBox).Text) > 0 Then
            Button1.Enabled = True
        Else
            Button1.Enabled = False
        End If
        Button1.Focus()
    End Sub

End Class
hOPE IT HELPS
Reply With Quote
  #3 (permalink)  
Old November 4th, 2009, 03:19 PM
Registered User
 
Join Date: Oct 2009
Location: NYC
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
Default sorry only after posting reply did i realize you're working on C#

I'm not familiar with exact code in C#, but the principle would be the same:
-create sub (void) for textbox object text-changed event that enables/disables button object
-activate postback for textbox
Reply With Quote
  #4 (permalink)  
Old November 6th, 2009, 03:24 PM
Authorized User
Points: 100, Level: 1
Points: 100, Level: 1 Points: 100, Level: 1 Points: 100, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2009
Posts: 26
Thanks: 1
Thanked 1 Time in 1 Post
Default

Thanks a lot.

It works correctly.I forgot to instantiate a new object of textbox.This way I get lots of extra attributes and methods.

thank you nqsousa...
Reply With Quote
  #5 (permalink)  
Old December 21st, 2009, 01:56 PM
Registered User
 
Join Date: Dec 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

just for newbs connect to textBox event and check
if(TextBox1.Text.Length>0)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
disabling a button with textbox sh_erfan C# 0 November 2nd, 2009 08:35 PM
disabling copy-paste for a textbox control gaikwad.ganesh C# 3 October 21st, 2008 03:41 PM
disabling submit button harpua Javascript 0 July 28th, 2007 11:07 AM
Disabling/enabling the textbox of EditCommandColu ninel ASP.NET 1.0 and 1.1 Professional 1 February 20th, 2006 04:39 PM
Disabling browser's "X" button farhanzia HTML Code Clinic 4 March 23rd, 2005 01:19 PM



All times are GMT -4. The time now is 05:47 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.