i want to apply validation in the text fields of datagrid. but i
have no idea. kindly tell me where i put validation code in my datagrid.
<%@ Import Namespace="System.Data.OleDb" %>
<Script Runat="Server">
Dim conNorthwind As OleDbConnection
Dim cmdSql As OleDbCommand
Dim strSql As String
Sub Page_Load
conNorthwind = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; " _
& "DATA SOURCE=" _
& Server.MapPath("nwind.mdb;"))
If Not IsPostBack Then
BindDataGrid
End If
End Sub
Sub BindDataGrid
cmdSql = New OleDbCommand("Select * From login", conNorthwind)
conNorthwind.Open()
dgrdlogin.DataSource = cmdSql.ExecuteReader()
dgrdlogin.DataBind()
conNorthwind.Close()
End Sub
Sub dgrdlogin_EditCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
dgrdlogin.EditItemIndex = e.Item.ItemIndex
BindDataGrid()
End Sub
Sub dgrdlogin_UpdateCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
Dim intmyID As Integer
Dim txtusername As TextBox
Dim strusername As String
Dim txtpw As TextBox
Dim strpw As String
Dim txtfirstname As TextBox
Dim strfirstname As String
Dim txtlastname As TextBox
Dim strlastname As String
intmyID = dgrdlogin.DataKeys(e.Item.ItemIndex)
txtfirstname = e.Item.Cells(1).Controls(0)
strfirstname = txtfirstname.Text
txtpw = e.Item.Cells(4).Controls(0)
strpw = txtpw.Text
txtusername = e.Item.Cells(3).Controls(0)
strusername = txtusername.Text
txtlastname = e.Item.Cells(2).Controls(0)
strlastname = txtlastname.Text
strSql = "Update login Set firstname=@firstname, lastname=@lastname, username=@username, pw=@pw " _
& " Where ID=@myID"
cmdSql = New OleDbCommand(strSql, conNorthwind)
cmdSql.Parameters.Add("@firstname", strfirstname)
cmdSql.Parameters.Add("@lastname", strlastname)
cmdSql.Parameters.Add("@username", strusername)
cmdSql.Parameters.Add("@pw", strpw)
cmdSql.Parameters.Add("@myID", intmyID)
conNorthwind.Open()
cmdSql.ExecuteNonQuery()
conNorthwind.Close()
dgrdlogin.EditItemIndex = -1
BindDataGrid()
Response.Write("update successfully")
End Sub
Sub dgrdlogin_CancelCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
dgrdlogin.EditItemIndex = -1
BindDataGrid()
End Sub
</Script>
<html>
<head><title>ExpertDataGridEditlogin.aspx</title></head>
<body>
<form id="Form1" Runat="Server">
<asp:DataGrid
ID="dgrdlogin"
OnEditCommand="dgrdlogin_EditCommand"
OnUpdateCommand="dgrdlogin_UpdateCommand"
OnCancelCommand="dgrdlogin_CancelCommand"
DataKeyField="ID"
AutoGenerateColumns="False"
CellPadding="10"
HeaderStyle-BackColor="LightGrey"
Runat="Server" BackColor="White">
<Columns>
<asp:BoundColumn
HeaderText="Employee ID"
DataField="ID"
ReadOnly="True" />
<asp:BoundColumn
HeaderText="First Name" DataField="firstname" />
<asp:BoundColumn
HeaderText="Last Name" DataField="lastname" />
<asp:BoundColumn
HeaderText="User Name" DataField="username" />
<asp:BoundColumn
HeaderText="Password" DataField="pw" />
<asp:EditCommandColumn
EditText="Edit!"
UpdateText="Update!"
CancelText="Cancel!" />
</Columns>
<HeaderStyle BackColor="LightGray" />
</asp:DataGrid>
</form>
</body>
</html>
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).