Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 2nd, 2009, 08:37 PM
Authorized User
 
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.....
 
Old November 4th, 2009, 03:16 PM
Registered User
 
Join Date: Oct 2009
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
 
Old November 4th, 2009, 03:19 PM
Registered User
 
Join Date: Oct 2009
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
 
Old November 6th, 2009, 03:24 PM
Authorized User
 
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...
 
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)





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





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.