How can I set MaxLength for TextBoxes generated by a DetailsView, within code behind

In ASP.NET 3.5., after changing the generated DetailsView (or GridView) to templates for particular fields, I can set the MaxLength for textboxes. How can I set the MaxLength in the code behind? Ultimately, I desire to set the MaxLength from the length of the field on the database, perhaps via GetSchemaTable method.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

try this you can set the max length of the control but you get the length of the string and display a alert if it is more the desired limit.

you can access the text of the text box

 Dim admission As DropDownList = CType(Me.DetailsView1.FindControl("ddl_admission"), DropDownList)
this way, change it accordingly.

you can show a client side alert using

  ScriptManager.RegisterStartupScript(Me.UpdatePanel1, GetType(String), "alert", "alert('University Assigned !');", True)

Let me know if you need anything else


It seems that you have provided a means for checking the user's entry length and are displaying an alert when he/she goes over a desired length. Is there a way to set the MaxLength attribute within the Code Behind so that the program does not have to monitor their typing? I'd like to prevent the user from entering more characters than will be allowed in the database table.

you can fix a standard value. But if you want to vary according to database then I think no other go.
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Robb HillSenior .Net Full Stack Developer

Did you ask this question earlier today?  I would swear I read this already...and thought I made a reponse....anyways...

yes you can do this in code behind...

but it looks like that has been answered.  If you do have other questions on this topic please close those out so the experts can get the appropriate credit for tyring to help.

Within SQL Server to get the field lenth you simply could use this select statement..
SELECT DATALENGTH(yourtextfield) AS TEXTFieldSize

As far as how you want to return that value and use it in your code, I think you have options.  You could simply put this select statement on a sqldatasource control and then access the value via a datareader..
To me this would be the easiest approach.

You could dynamically change the select statement of the sqldatasource control at the same time you dynamically were building these textboxes.


This is my only thread concerning the question about setting the MAXLENGTH field dynamically. Your ideas sound good. Could you please provide for me some specific code examples and where to place each? I've included two fields within a DetailsGridView for reference. In this case, I've hard-coded the MAXLENGTH field, but I'd like to change (or add) it dynamically from the Code behind. (VB is preferrable, but any code would help). I am using Templates here because I am performing a RequiredField validation (not shown in code) along with setting the MaxLength. Thanks.
<asp:DetailsView ID="DetailsView1" runat="server" 
            <asp:TemplateField HeaderText="Type" SortExpression="Type">
                    <asp:TextBox ID="txtType" runat="server" 
                        Text='<%# Bind("Type") %>' MaxLength="10"></asp:TextBox>

            <asp:TemplateField HeaderText="CorpInd" SortExpression="CorpInd">
                    <asp:TextBox ID="txtCorpInd" runat="server" 
                        Text='<%# Bind("CorpInd") %>' MaxLength="20"></asp:TextBox>

Open in new window

I finally came up with my own resolution to this issue. I've attached the code that will change the MaxLength for TextBoxes in Insert mode of DetailsView. The trick is to Find the Control and then do a DirectCast on it. Further, this needed to be done within the DataBound event of DetailsView1 (specifically within the area for CurrentMode of Insert). To set the MaxLength in Edit mode, the same code could be used within an If/then statement for Edit mode within the Databound event of DetailsView1.
Protected Sub DetailsView1_DataBound(ByVal sender As Object, _
                                         ByVal e As System.EventArgs) _
                                         Handles DetailsView1.DataBound

        Dim txt As TextBox
        If DetailsView1.CurrentMode = DetailsViewMode.Insert Then
            txt = DirectCast(DetailsView1.FindControl("txtType"), TextBox)
            txt.MaxLength = 8
            txt = DirectCast(DetailsView1.FindControl("txtCorpInd"), TextBox)
            txt.MaxLength = 18
        End If
End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial