Saturday, March 24, 2012

Please help Friends how to control maxlength textbox in datagrid

Is there a way to set the maxlength property for a text box in the datagrid.

My database table field size is only 10 chars for first name.

How can i make the user not to enter more tha 10 chars on the front end.

Is there any Max length property i can assign for the text boxes.

I have 4 text boxes in datagrid.

Thank you very much.Hi,

if it is single-line TextBox, you can use its MaxLength property like you normally can.

<asp:TextBox ID="xxx" MaxLength="4"... /
This works even if TextBox is in a DataGrid.
Thanks Joteke, I have the following code triggered when i click edit in the datagrid, then it will show all text boxes etc. where can i place the maxlength="4" property in the following code.

Thank you very much.

< Code >
Sub MyDataGrid_UpdateCommand(s As Object, e As DataGridCommandEventArgs )
Dim conn As SqlConnection
Dim MyCommand As SqlCommand
Dim strConn as string = "server=Rajender;uid=sa;pwd=sa;database=NORTHWIND"
Dim txtFirstName As textbox = E.Item.cells(2).Controls(0)
Dim txtLastName As textbox = E.Item.cells(3).Controls(0)
Dim txtTitle As textbox = E.Item.cells(4).Controls(0)
Dim strUpdateStmt As String
strUpdateStmt =" UPDATE Employees SET" & _
" FirstName =@.Fname, LastName =@.Lname, Title = @.Title " & _
" WHERE EmployeeID = @.EmpID"
conn = New SqlConnection(strConn)
MyCommand = New SqlCommand(strUpdateStmt, conn)
MyCommand.Parameters.Add(New SQLParameter("@.Fname", txtFirstName.text))
MyCommand.Parameters.Add(New SQLParameter("@.Lname", txtLastName.text))
MyCommand.Parameters.Add(New SQLParameter("@.Title", txtTitle.text))
MyCommand.Parameters.Add(New SQLParameter("@.EmpID", e.Item.Cells(1).Text ))
conn.Open()
MyCommand.ExecuteNonQuery()
MyDataGrid.EditItemIndex = -1
conn.close
BindData
End Sub

</ code >
In that case, you could declare EditItemTemplate for the DataGrid (declaratively in asox) in which you set the TextBox's MaxLength.

If you want to do it programmatically, then you'd do it in ItemDataBound method (when ItemType is EditItem)

0 comments:

Post a Comment