banupriya
banupriya

Reputation: 1249

Max length for bound field in gridview in ASP.Net

I have to set maximum length for bound field in an editable gridview. For this i have used data format string property and also given ApplyFormatInEditMode="true" still it accepts invalid input. The gridview does not have template field, it contains bound fields only. I have written OnRowEditing and RowUpdating events. The dataformat string is DataFormatString="{0:N0}" but it accepts '2352345234523454352345' input also and displays server error while updating in database. I want to spcify maximum length for the textboxes generated dynamically when Edit button is clicked.

Upvotes: 3

Views: 18912

Answers (4)

Eric Barr
Eric Barr

Reputation: 4155

As you mentioned, this is for an editable gridview, so what you really want to do is set the MaxLength property of the TextBox when the gridview is in Edit mode. For that, you will want to replace your BoundField with a TemplateField, then use the EditItemTemplate to create your TextBox with the MaxLength:

<asp:TemplateField HeaderText="My Field Name">
    <ItemTemplate><%# Eval("sMyFieldName")%></ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox runat="server" ID="sMyFieldName" Text='<%# Bind("sMyFieldName") %>' Width="250" MaxLength="10" ></asp:TextBox>
    </EditItemTemplate>
</asp:TemplateField>

Upvotes: 1

banupriya
banupriya

Reputation: 1249

I have done this by writing the following code in Rowdatabound event in gridview. In this event, I have implicitly converted gridview cells to dynamically generated textboxes and set width and Max length for them. In key press event, i have blocked alphabets and other special characters from being entered in the textbox. Now it works fine as expected!. The code is as follows:

if (e.Row.RowType == DataControlRowType.DataRow) 
    {
        for (int i = 0; i < dgv.Columns.Count - 1; i++)
        {
            if ((e.Row.Cells[i].Controls.Count > 0) && (e.Row.Cells[i].Controls[0] is TextBox))
            {
                ((TextBox)e.Row.Cells[i].Controls[0]).Width = 40;
                ((TextBox)e.Row.Cells[i].Controls[0]).MaxLength = 5;
            }
        }
    }            
    //To make the text box accept numbers, delete, backspace, numlock,dot only
    e.Row.Attributes.Add("onkeypress", "javascript: var Key = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;  return ((Key >= 48 && Key <= 57) || (Key == 110) || (Key == 190) || (Key == 8) || (Key == 46) || (Key == 144))");

Upvotes: 4

David Griebel
David Griebel

Reputation: 21

You can use Jquery against the textboxes that have already been rendered, provided you assign a CSS class to them.

$(".numericInputTextBox").each(function() {
     $(this).attr("MaxLength", "9");
 )};

Upvotes: 2

Kamran Khan
Kamran Khan

Reputation: 9986

DataFormatString does not handle the MaxLength. Try using DataBinder.Eval() inside textbox, and set the MaxLength there. Like following:

<asp:TemplateField HeaderText="My Text">
<ItemTemplate>
<asp:TextBox ID="txtID"  MaxLength="10" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "YOUR_BOUND_ITEM_NAME") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>

--EDIT--

asp:BoundField by its nature display the records. So there is no way you can set the MaxLength of it.

I have to set maximum length for bound field in an editable gridview.

You can grab the edit_button_click(or which ever event) event that shows the text box in the grid. There you can set the MaxLength of text box.

Alternatively, This example might help.

Upvotes: 4

Related Questions