?
Solved

maxlength for textbox C# asp.net not working

Posted on 2005-05-12
13
Medium Priority
?
2,211 Views
Last Modified: 2012-05-05
I have a web forms control that is a textbox.  In the attributes, I've set the maxlength to 30.  It shows up in the html code as being set to 30, but when I run the page, it doesn't limit the characters to 30.  I can just keep on typing.  Is there something else I can do to limit the actual keystrokes allowed in the text box?  The database field will only take 30 and will cut off any characters after that.
0
Comment
Question by:Pemberley1
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 2

Expert Comment

by:SThorogood
ID: 13987151
Are you using a SingleLine TextBox - i.e. an input rather than a textarea element. From the MSDN library:

Remarks
Use the MaxLength property to limit the number of characters that can be entered into the TextBox control.

Note   This property is only applicable when the TextMode property is set to TextBoxMode.SingleLine or TextBoxMode.Password.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 13987239
hi

  the maxlength is property of input control of html

try to create html file with this code only

<html>
<input type="text" maxlength="10">
</html>

and look if you have the same problem or not, if you still have it, it may be a browser issures not c#
0
 
LVL 2

Expert Comment

by:SThorogood
ID: 13987370
What browser are you using? The HTML sample works fine in IE6
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 13987549
the sample working also in IE5, it's plain html code
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 13987814
well regardless of if you set maxlength on the textbox you should also in your code behind processing insure that the field is < 30 characters.

The reason for this is that not every browser plays nicely and people can also simply do a post to your page with bad data (takes all of about 5 minutes with the webclient class) and this could cause problems with your app.

Data validation on the client side in a web app is a nicety/convenience; you have to do your validation on the server side as well.

Aside from this; I bet the first respondent is correct and you have a multi-line textbox.

Greg
0
 

Author Comment

by:Pemberley1
ID: 13988436
I have Internet Explorer 6.
The sample works for me.  I have it set to single line and I added it to my code - but it still isn't working.
0
 

Author Comment

by:Pemberley1
ID: 13988446
The text box does go from not visible to visible at a certain point in the code - would that make a difference?
0
 
LVL 2

Expert Comment

by:SThorogood
ID: 13988484
can you post some code?
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 2000 total points
ID: 13988491
I dont think, but it may you have code changing the MaxLength
0
 

Author Comment

by:Pemberley1
ID: 13989524
This textbox starts out as not visable - just as a check, I changed it to visible.  The MaxLength worked - until it hit this code
tbPhysicianName.Visible = true;
tbPhysicianName.MaxLength = 30;

It seems to be setting the values back to defaults as soon as it is told to make the text box visible.  It seems to be ignoring the second line of code.
0
 

Author Comment

by:Pemberley1
ID: 13989981
I went through it step by step - it keeps the value for max length throughout the visable = true.  It loses the value when you click on a datagrid selection at the very first line, it is already set to 0:

private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  pnlgchilderr.Visible = false;
                  lblDuplicateerror.Visible = false;
                  lblOptDuplicateerror.Visible = false;
                  tbLastname.Text = DataGrid1.SelectedItem.Cells[0].Text;
                  tbFirstname.Text = DataGrid1.SelectedItem.Cells[1].Text;
                  tbMI.Text = DataGrid1.SelectedItem.Cells[2].Text;
                  tbRelationship.Text = DataGrid1.SelectedItem.Cells[5].Text.ToUpper();
                  tbDOB.Text = DataGrid1.SelectedItem.Cells[6].Text;
                  tbSSN.Text = DataGrid1.SelectedItem.Cells[7].Text;
0
 

Author Comment

by:Pemberley1
ID: 13990170
I've tried adding
tbPhysicianName.MaxLength = 30;
to that code and it still doesn't change it - am I using the correct syntax?
0
 

Author Comment

by:Pemberley1
ID: 14028013
Thanks for your hellp - I gave you the points because you were the first and last to try to help.  I think the form must just be corrupted.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Integration Management Part 2
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question