how to apply require field validation on datagrid

  • 12 years ago

     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> 

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” - Bill Gates