把原来aspx.vb 中的内容写入类项目中 可页面上的控件事件如何处理呢
我在页面中放了个datalist控件 添加 删除 和修改 记录的按钮全放在datalist头模板和脚模板中当我把aspx.vb文件移至类项目中后
发现 datalist出现未定义
于是我在类中重新声明了datalist
public datalst as new datslist
之后在运行中发现 页面载入事件是正常 datalist里面也能载入数据
但其他关于datalist的事件代码都不能执行
类 代码如下
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports shop.dbase
Imports shop.mudle
Partial Public Class admin_spclslist
Inherits Page
Public datalst As New DataList
Public Event Itemcmd As DataListCommandEventHandler
Public ItemCommand As DataListCommandEventHandler
Private Sub databinder(ByVal classpath As String)
Dim sql As String = "select * from bl_class where classpath='" & classpath & "' order by rsdate desc"
Dim sqlhlp As New SqlHelper
Dim ds As DataSet
ds = sqlhlp.DataSetContainer(sql, "bl_class")
datalst.DataSource = ds.Tables(0).DefaultView
datalst.DataBind()
Dim lbrpath As New Label
lbrpath = datalst.Controls(0).FindControl("lbpath")
lbrpath.Text = classpath
End Sub
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
If Not IsPostBack Then
databinder("/")
End If
End Sub
Protected Sub Page_InitComplete(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.InitComplete
AddHandler datalst.ItemCommand, ItemCommand
End Sub
Protected Sub DataLst_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles MyClass.Itemcmd
If e.CommandSource.GetType.Name = "Button" Then
If e.CommandName = "addclass" Then
Dim txtbox As TextBox = datalst.Controls(0).FindControl("classname")
Dim lbpathstr As Label = datalst.Controls(0).FindControl("lbpath")
Dim ckstr As New checkStr
If ckstr.CheckHackStr(txtbox.Text.Trim, 2, 50) Then
Dim sqladd As String = "insert bl_class (classname,classpath) values('" & txtbox.Text.Trim & "','" & lbpathstr.Text.Trim & "')"
Dim sqlhp As New SqlHelper
sqlhp.DbaseDelOrUpdateOrInsert(sqladd)
databinder(lbpathstr.Text.Trim)
Else
Response.Write("<script language='javascript' >alert('输入的分类名称包含限制字符!') </script>")
End If
End If
If e.CommandName = "modclass" Then
Dim txtbox As TextBox = datalst.Controls(0).FindControl("classname")
Dim lbpathstr As Label = datalst.Controls(0).FindControl("lbpath")
Dim lbmodclassid As Label = datalst.Controls(0).FindControl("modclassid")
Dim ckstr As New checkStr
If ckstr.CheckHackStr(txtbox.Text.Trim, 2, 50) Then
Dim sqlmod As String = "update bl_class set classname='" & txtbox.Text.Trim & "' where classid='" & lbmodclassid.Text.Trim & "'"
Dim sqlhp As New SqlHelper
sqlhp.DbaseDelOrUpdateOrInsert(sqlmod)
databinder(lbpathstr.Text.Trim)
Else
Response.Write("<script language='javascript' >alert('输入的分类名称包含限制字符!') </script>")
End If
End If
If e.CommandName = "delclass" Then
Dim lbpathstr As Label = datalst.Controls(0).FindControl("lbpath")
For i As Integer = 0 To datalst.Items.Count - 1
Dim cb As CheckBox = datalst.Items(i).FindControl("CheckBox1")
If cb.Checked = True Then
Dim lbclassidstr As Label = datalst.Items(i).FindControl("lbclassid")
Dim lbclassnamestr As Label = datalst.Items(i).FindControl("lbclassname")
Dim delpath As String = lbpathstr.Text.Trim & lbclassnamestr.Text.Trim & "/"
Dim sqldel As String = "delete from bl_class where classpath like '" & delpath & "%' or classid='" & lbclassidstr.Text.Trim & "' "
Dim sqlhp As New SqlHelper
sqlhp.DbaseDelOrUpdateOrInsert(sqldel)
End If
Next
Dim classpath As String = lbpathstr.Text.Trim
databinder(classpath)
End If
End If
If e.CommandSource.GetType.Name = "LinkButton" Then
If e.CommandName = "look" Then
Dim lbclsname As Label = e.Item.FindControl("lbclassname")
Dim lbrpath As Label = datalst.Controls(0).FindControl("lbpath")
Dim classpath As String = lbrpath.Text.Trim & lbclsname.Text.Trim & "/"
lbrpath.Text = classpath
databinder(classpath)
End If
If e.CommandName = "modcls" Then
Dim tbclsname As TextBox = datalst.Controls(0).FindControl("classname")
Dim lbclsname As Label = e.Item.FindControl("lbclassname")
Dim lbcid As Label = e.Item.FindControl("lbclassid")
Dim lbclsid As Label = datalst.Controls(0).FindControl("modclassid")
lbclsid.Text = lbcid.Text
tbclsname.Text = lbclsname.Text
Dim modbt As Button = datalst.Controls(0).FindControl("clsbt")
modbt.CommandName = "modclass"
modbt.Text = "修改"
End If
End If
End Sub
End Class
--------------------编程问答-------------------- Protected Sub DataLst_ItemCommand(ByVal source As Object, ByVal e As DataListCommandEventArgs) Handles
我知道问题是出在这里 如何让这个事件是datalstde 事件呢 --------------------编程问答-------------------- 自己解决了
Public WithEvents datalst As New DataList
补充:.NET技术 , ASP.NET