📄 example2.vb
字号:
Option Strict Off
Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HTMLControls
Imports System.Data
Imports System.Data.ADO
Public Class Example : Inherits Page
'dim variables from aspx page
public DataGrid1 as DataGrid
public Label1 as Label
public tbFName as TextBox
public tbLName as TextBox
public tbAddress as TextBox
public tbCity as TextBox
public tbState as TextBox
public tbZip as TextBox
public tbPhone as TextBox
public Submit as button
public Panel1 as Panel
'declare connection
dim Conn as new ADOConnection("DSN=21DaysBanking")
dim ds as new DataSet("MyDataSet")
sub Page_Load(obj as Object, e as EventArgs)
if Not Page.IsPostBack then
FillDataGrid()
end if
end sub
sub Submit_Click(obj as object, e as eventargs)
'insert new data
dim i, j as integer
dim strText as string
dim blnGo as boolean = true
j = 0
GetData()
dim dr as DataRow = ds.Tables("tblUsers").NewRow()
for i = 0 to Panel1.Controls.Count - 1
if Panel1.controls(i).GetType = GetType(TextBox) then
strText = Ctype(Panel1.Controls(i), TextBox).Text
if strText <> "" then
dr(j) = strText
else
blnGo = false
Label1.Text = Label1.Text & "You forgot to enter " & _
"a value for " & Panel1.Controls(i).ID & "<p>"
end if
j = j + 1
end if
next
if not blnGo then
exit sub
end if
ds.Tables("tblUsers").Rows.Add(dr)
ExecuteStatement()
FillDataGrid()
end sub
sub DataGrid1_Edit(obj as object, e as DataGridCommandEventArgs)
FillDataGrid(e.Item.ItemIndex)
end sub
sub DataGrid1_Delete(obj as object, e as DataGridCommandEventArgs)
GetData()
ds.Tables("tblUsers").Rows(e.Item.ItemIndex).Delete
ExecuteStatement()
FillDataGrid()
end sub
sub DataGrid1_Update(obj as object, e as DataGridCommandEventArgs)
if UpdateDataStore(e) then
FillDataGrid(-1)
else
Label1.Text = "Failed"
end if
end sub
sub DataGrid1_Cancel(obj as object, e as DataGridCommandEventArgs)
FillDataGrid(-1)
end sub
sub DataGrid1_PageIndexChanged(obj as Object, e as DataGridPageChangedEventArgs)
DataGrid1.DataBind()
end sub
private function UpdateDataStore(e as DataGridCommandEventArgs) _
as boolean
dim i,j as integer
dim strText as string
dim blnGo as boolean = True
j = 0
GetData()
for i = 1 to e.Item.Cells.Count - 3
strText = Ctype(e.Item.Cells(i).Controls(0), TextBox).Text
if strText <> "" then
ds.Tables("tblUsers").Rows(e.Item.ItemIndex)(j) = strText
j = j + 1
else
blnGo = false
Label1.Text = Label1.Text & "You forgot to enter " & _
"a value<p>"
end if
next
if not blnGo then
return false
exit function
end if
ExecuteStatement()
return blnGo
end function
private sub GetData()
'open connection
dim objCmd as ADODataSetCommand = new ADODataSetCommand _
("select * from tblUsers", Conn)
'fill dataset
objCmd.FillDataSet(ds, "tblUsers")
'define primary key
dim keys() as DataColumn = {ds.Tables("tblUsers"). _
Columns("UserID")}
ds.Tables("tblUsers").PrimaryKey = keys
end sub
private sub FillDataGrid(Optional EditIndex as integer=-1)
GetData()
if not EditIndex.Equals(Nothing) then
DataGrid1.EditItemIndex = EditIndex
end if
'select data view and bind to server control
DataGrid1.DataSource = ds.Tables("tblUsers"). _
DefaultView
DataGrid1.DataBind()
end sub
private function ExecuteStatement()
'open connection
dim objCmd as new ADODataSetCommand _
("select * from tblUsers", Conn)
try
'ds.AcceptChanges()
objCmd.Update(ds, "tblUsers")
catch ex as ADOException
Label1.Text = "Error updating the database. Please" & _
" make sure all values are correctly input"
end try
ds.Clear
end function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -