用javascript实现动态增加DataGrid行

用javascript实现动态增加DataGrid行_第1张图片

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="jsAddTableRow.aspx.vb" Inherits="vbProject.WebForm1"%>



WebForm1








Width="200px" Height="16px">用js实现动态增加DataGrid行
Width="0px" TextMode="MultiLine">隐藏的TextBox,用来保存DataGrid中的值
type="button" value="增加一行" οnclick="InsertDataGrid();">
Width="100%" AutoGenerateColumns="False" DataKeyField="EmployeeID">

































Text="保存数据">



Public Class WebForm1
    Inherits System.Web.UI.Page
    Private vbp As New vbProject

#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not IsPostBack Then
            BindData()
            Button1.Attributes.Add("onclick", "getTableText();") '先获取数据
        End If
    End Sub

    '绑定数据
    Private Sub BindData()
        DataGrid1.DataSource = vbp.GetEmployees()
        DataGrid1.DataBind()
    End Sub

    '获取数据并且保存
    Private Sub GetText()
        Dim strTotal As String = TextBox1.Text
        Dim strRow() As String = strTotal.Split("%") '每行的值
        Dim strValue() As String
        Dim i, j As Integer
        For i = 0 To strRow.Length - 2 '后面有一行空数据,也可以在javascript中控制后面的'%'符号
            '这里传入EmployeesAddUp中的参数只有前面4个,所以要转换数组
            strValue = strRow(i).Split(",", 4) '对应数据库中每一列的值
            vbp.employeeUpdate(strValue(0), strValue(1), strValue(2), strValue(3))
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        GetText()
        BindData()
    End Sub

    Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
        If e.Item.ItemType <> ListItemType.Footer And e.Item.ItemType <> ListItemType.Header And e.Item.ItemType <> ListItemType.Pager Then
            Dim lbtn As LinkButton = CType(e.Item.FindControl("LinkButton1"), LinkButton)
            lbtn.Attributes.Add("onclick", "return confirm('你确定要删除吗?')")
        End If
    End Sub

    Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
        If vbp.EmployeeDelete(DataGrid1.DataKeys(e.Item.ItemIndex)) = -1 Then
            Response.Write("")
        End If
        BindData()
    End Sub
End Class

 

 

 

 

你可能感兴趣的:(asp.net(vb.net),datagrid,javascript,textbox,asp,button,server)