Option Strict Off
Imports System
Imports System.Collections
Imports System.Text
Imports System.Data
Imports System.Data.OleDb
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Class BaseClass

Inherits System.Web.UI.Page
protected Grid1 as DataGrid
Protected Message as label
Protected acode_category as dropdownlist
Protected AddPanel as Panel
Dim myConnection As OleDbConnection
Dim myCommand    As OleDbDataAdapter
Dim ds           As New DataSet
Dim ConnStr      As String
Dim SQL          As String
Sub Page_Load(Source As Object, E As EventArgs) 
	ConnStr = "Provider=SQLOLEDB; Data Source=(local); Initial Catalog=ASPNET;User ID=sa;"
	myConnection = New OleDbConnection(ConnStr)      
	if NOT (isPostBack)
	rebind
	end if
End Sub
Sub ReBind()
	
	SQL = "select  m.*, g.code_display as category "
	SQL = SQL + "from masters m,  groups g "
	SQL = SQL + " where m.code_category = g.code_value"
	myCommand = New OleDbDataAdapter(SQL, myConnection)
	
	myCommand.Fill(ds, "masters")
	'绑定DataGrid	
	Grid1.DataSource=ds.Tables("masters").DefaultView
	Grid1.DataBind()
	SQL = "Select * from groups order by code_display"
	myCommand = New OleDbDataAdapter(SQL, myConnection)
	myCommand.Fill(ds, "groups")
	'填充dropdownlist
	acode_category.DataSource=ds.Tables("groups").DefaultView
	acode_category.DataBind()
	hidePanel()
End Sub
Sub Grid1_Edit(Sender As Object, E As DataGridCommandEventArgs)
	Grid1.EditItemIndex = E.Item.ItemIndex
	ReBind()
End Sub
Sub Grid1_Cancel(Sender As Object, E As DataGridCommandEventArgs)
	Grid1.EditItemIndex = -1
	ReBind()
End Sub
sub hidePanel()
	if AddPanel.visible = true then
	   AddPanel.visible = false
	end if
end sub
Sub RunSql(sql as string)
	
	try
		Dim mycommand2 As New OleDbCommand(sql,myConnection)
		myConnection.Open()
		myCommand2.ExecuteNonQuery()
		myConnection.Close()
		'退出编辑模式
		Grid1.EditItemIndex = -1
	 Catch ex As OleDbException
		' SQL 错误
		Dim errItem   As OleDbError
		Dim errString As String
		For Each errItem In ex.Errors
		 errString += ex.Message + "<br/>"
		Next
		Message.Text = "SQL 错误。细节如下:<br/><br/>" & errString
		Message.Style("color") = "red"
	 catch myException as Exception
		Response.Write("出现异常: " + myException.ToString()) 
		Message.Style("color") = "red"
	end try
	rebind
	response.write(sql) 
End sub
End Class
