Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a text box and label sitting next to each others. Label's text value comes from a drop down list. I have to turn the focus of text box to itself if, label's value is smaller than text box value and it goes on until, right value is selected.
Can You help me?

What I have tried:

This is my code:-
.aspx file:-
ASP.NET
<div class="row mb10">
<asp:Label runat="server" ID="lbl_Adults" CssClass="col-lg-2 control-label" Text="Adults : " AssociatedControlID="txt_Adults"></asp:Label>
<div class="col-lg-6">
    <div class="col-lg-4" style="margin-left:-15px;">
        <asp:TextBox runat="server" ID="txt_Adults" CssClass="form-control" OnTextChanged="txt_Adults_TextChanged" AutoPostBack="true"></asp:TextBox>
        <asp:RequiredFieldValidator Font-Bold="true" ForeColor="Red" runat="server" ID="req_Adults" ControlToValidate="txt_Adults" ErrorMessage="Enter No. of Adults"></asp:RequiredFieldValidator>
    </div>
    <div class="col-lg-8" style="font-family:'Copperplate Gothic'">
    Only <asp:Label runat="server" ID="lbl_No_Of_Adults"></asp:Label> Adult(s) allowed.
    </div>
 </div>
</div>


Here's .cs file code:-
C#
protected void txt_Adults_TextChanged(object sender, EventArgs e)
         {
  
                 if (txt_Adults==null)
                 {
                     txt_Adults.Text = "";
                     txt_Adults.Focus();
                 }
                 else if (Convert.ToInt32(txt_Adults.Text) > Max_NoAdults)
                 {
                     txt_Adults.Text = "";
                     txt_Adults.Focus();
                 }
                 else if(Convert.ToInt32(txt_Adults.Text)<=Max_NoAdults)
                 {
                     NoAdults = Convert.ToInt32(txt_Adults.Text);
                 }
             
         }
Posted
Updated 7-Aug-17 9:58am
v3
Comments
Michael_Davies 29-Jul-17 4:32am    
Lots of issues in your code, take a look at these;

You are using a While on an event handler with potentially never ending loop.

You test if the control txt_Adults is Null or not, as it is a handler for the txt_Adults control you can safely assume txt_adults exists.

You Convert.ToString a literal that is a string "0".

You appear not to verify that txt_Adults contains only numeric and simply Convert it, if the user types anything other than numbers you will get an exception.
sam_matte 29-Jul-17 6:48am    
I've removed the while loop and added a condition at else to check if
txt_Adults
carries value less than or equal to Max_NoAdults. The literal "0" got converted fine.  My code worked smoothly. I may have to apply regular expression to check whether entered string is number or not. Can you help me with it?
Michael_Davies 29-Jul-17 7:07am    
Use the Improve question and show your code as it is now.
sam_matte 29-Jul-17 7:28am    
I've updated my code. Please take a look.
Michael_Davies 29-Jul-17 8:13am    
txt_Adults is the textbox control, hopefully it is not Null as the event handler was fired by it, you probably mean to check that txt_Adults has text, use either (txt_Adults.Text != "") or (txt_Adults.Text.Length != 0).

if (txt_Adults.Text.Length != 0)
{
int32 tempValue;

	if (int32.TryParse(txt_Adults.Text, tempValue) == true)
	{
//
// tempValue now contains the integer value of the text
//
		if (tempValue > Max_NoAdults)
		{
			txt_Adults.Text = "";
			txt_Adults.Focus();
		}
		else if(tempValue <= Max_NoAdults)
		{
			NoAdults = tempValue;
		}
	}
	else
	{
		txt_Adults.Focus();
	}
}

1 solution

The textbox will not null if using text_changed event.
int32? tempValue; //nullable int variable
 
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