我正在尝试动态创建一个数据表,该数据表将允许我在执行过程时多次填充单个记录。我知道如何使用我需要的列创建数据表,但我的问题是每次执行子例程以添加记录时,它都会重新初始化数据表并清除先前填充的记录。如何创建数据表以使其仅初始化一次?我尝试在 Page_Load 事件期间使用“Not IsPostback”条件进行初始化,但这不起作用。还有其他想法吗?
Partial Public Class ContactLookup
Inherits System.Web.UI.Page
Dim dtContacts As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
dtContacts = New DataTable
dtContacts.Columns.Add("Email", GetType(String))
dtContacts.Columns.Add("AccountNum", GetType(String))
End If
End Sub
Protected Sub btnAddContact_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAddContact.Click
Dim AcctNum As String = txtLocAcct.Text
Dim Email As String = txtEmail.Text
Dim drContacts As DataRow = dtContacts.NewRow
drContacts("Email") = Email
drContacts("AccountNum") = AcctNum
dtContacts.Rows.Add(drContacts)
End Sub
End Class