Dim dt As DataTable=sql.ExecuteDataset(SqlHelper.ConnectionString,CommandType.Text,”select AccountName from T_Account where AccountName="”&ClientName&”"”).Tables(0)If dt Is Nothing Then
Return False
End If
If dt.Rows.Count>=1 Then
Return True
Else
Return False
End If
End Function
’调用此函数,插入注册信息
Public Function SaveData() As Boolean
’判断是否已经存在登录名
If NameExist(EmailTextBox.Text) Then
EmailUniqueCustomValidator.IsValid=False
End If
If Not Page.IsValid Then
Exit Function
End If
Dim ne As New Nearfar
Try
’调用一个存储过程,插入到三个表中
Dim mAcc As New T_AccountRow
With mAcc
.AccountName=EmailTextBox.Text
.Password=PasswordTextBox.Text
.PwdQuestion=DropDownList1.SelectedItem.Text
.PwdAnswer=AddressTextBox.Text
.AccountTime=Now.Date
End With
ne.T_AccountCollection.Insert(mAcc)
Return True
Catch ex As Exception
Return False
End Try
End Function
6.6.3如何实现后台数据维护
后台数据维护一般主要完成基础数据的查询、删除、更新、插入操作,这种类型的操作模式有一定的通用性。因而非常有必要加以总结和推广。
在ASP.NET中一般可以用DataGrid控件来展示数据,用文本框来录入数据。所以总体思路是,使用两个panel,把整个界面按功能分成两个区域。一个是完成查询和删除功能的区域,简称为查询区域,该区域也是默认显示的区域,同时在该区域还有更新和添加的链接。我们用另外一个panel作为更新和录入区,简称为录入区。
在显示区,用DataGrid展示查询的所有数据。使用DataGrid可以方便地实现分页显示数据,按照某一标题排序等功能。每一行数据,都有相应的删除按钮,单击“删除”可直接删除该行数据,单击更新或添加按钮,则网页显示录入区,隐藏查询区。
网页刚启动时,也就是在网页的Pageload事件中,显示查询区,隐藏录入区,并读取数据呈现在DataGrid中。当单击“添加”时,隐藏查询区,显示录入区,让用户录入一条新的数据。当单击“更新”时,流程与单击“添加”一样,不同之处是,单击“更新”多做了一步操作,它将该条记录的数据自动赋给录入区域中相应的文本框。关于网页的具体实现参见以下代码中的提示。
Private Sub Page_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
’在此处放置初始化页的用户代码
ShowQuery()
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
’将数据赋给datagrid
Private Sub BindGrid()
Dim dv As DataView
dv=MyBase.MyNearFar.T_HelpCollection.GetAllAsDataTable.DefaultView
Me.dgHelp.DataSource=dv
Me.dgHelp.DataBind()
Cache(”ds”)=Me.dgHelp.DataSource
End Sub
’将数据赋给datagrid,并按照传入的列排序
Private Sub BindGrid(ByVal SortExpression As String)
Dim dv As DataView
dv=MyBase.MyNearFar.T_HelpCollection.GetAllAsDataTable.DefaultView
dv.Sort=SortExpression
Me.dgHelp.DataSource=dv
Me.dgHelp.DataBind()
Cache(”ds”)=Me.dgHelp.DataSource
End Sub
’删除一行数据
Private Sub dgHelp_DeleteCommand(ByVal source As Object,ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)Handles dgHelp.DeleteCommand MyBase.MyNearFar.T_HelpCollection.DeleteByPrimaryKey(Me.dgHelp.DataKeys(CInt(e.Item.ItemIndex)))
BindGrid()
End Sub
’响应用户单击更新的事件
Private Sub dgHelp_ItemCommand(ByVal source As Object,ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgHelp.ItemCommand
If LCase(e.CommandName).Equals(”select”) Then
’将选中的行的数据付给录入区
Dim rv As T_HelpRow
rv=MyBase.MyNearFar.T_HelpCollection.GetByPrimaryKey
(Me.dgHelp.DataKeys(CInt(e.Item.ItemIndex)))
With rv
Me.txthelpcontent.Text=.HelpContent
Me.txthelptitle.Text=.HelpTitle
Me.dropdwnmodule.SelectedValue=.ModuleName
Me.dropdownpriority.SelectedValue=.Priority.ToString
Cache(”helpid”)=.HelpID
End With
ShowAdd()
Cache(”state”)=”update”
End If
End Sub
’验证录入的值是否合法
Private Function validatePage()As Boolean
If Me.txthelptitle.Text=””Then
showerror(”标题不能为空!”)
Return False
End If
If Me.txthelpcontent.Text=””Then
showerror(”标题不能为空!”)
Return False
End If
Return True
End Function
’清除录入区的相应控件的值
Private Sub ClearControl()
Me.txthelpcontent.Text=””
Me.txthelptitle.Text=””
Me.dropdownpriority.SelectedIndex=0
Me.dropdwnmodule.SelectedIndex=0
End Sub
’响应用户单击确定事件,如果当前状态是插入,则执行插入操作
’如果当前状态是更新,则执行更新操作
Private Sub btnOk_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnOk.Click
Dim nerow As New T_HelpRow
If validatePage()Then
nerow.HelpTitle=Me.txthelptitle.Text
nerow.HelpContent=Me.txthelpcontent.Text
nerow.ModuleName=Me.dropdwnmodule.SelectedItem.ToString
nerow.Priority=CInt(Me.dropdownpriority.SelectedItem.Text)
nerow.PublishTime=Now.ToUniversalTime
If Cache(”state”)=”update”Then
nerow.HelpID=Cache(”helpid”)
MyBase.MyNearFar.T_HelpCollection.Update(nerow)
Else
MyBase.MyNearFar.T_HelpCollection.Insert(nerow)
ClearControl()
End If
Else
ShowAdd()
Exit Sub
End If
BindGrid()
End Sub
Public Function myGet(ByVal str As String,ByVal mylen As Int16) As String
If Len(str)> mylen Then
Return(Left(str,mylen)+”....”)
Else
Return str
End If
End Function
’对用户单击”添加”事件的响应
Private Sub btnAddLink_Click (ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnAddLink.Click
ShowAdd()
Cache(”state”)=”add”
End Sub
’实现datagrid的分页显示
Private Sub dgHelp_PageIndexChanged (ByVal source As Object,ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles dgHelp.PageIndexChanged
Me.dgHelp.CurrentPageIndex=e.NewPageIndex
BindGrid(Cache(”CurrentSortExpression”))
End Sub
’显示录入区域,隐藏查询区域
Private Sub ShowAdd()
Me.panQuery.Visible=False
Me.panAdd.Visible=True
"Me.panAdd.Style.Item(”Top”)=0
End Sub
’显示查询区域,隐藏录入区域
Private Sub ShowQuery()
Me.panQuery.Visible=True
Me.panAdd.Visible=False"Me.panQuery.Style.Item(”top”)=0
End Sub
’实现排序功能
Private Sub dgHelp_SortCommand(ByVal source As Object,ByVal e As System.Web.UI.Web—Controls.DataGridSortCommandEventArgs) Handles dgHelp.SortCommand
If Cache(”CurrentSortExpression”)=e.SortExpression Then
Cache(”CurrentSortExpression”)=Reverse(Cache(”CurrentSortExpression”))
Else
Cache(”CurrentSortExpression”)=e.SortExpression
End If
BindGrid(Cache(”CurrentSortExpression”))
End Sub
’实现倒排序
Private Function Reverse(ByVal str As String) As String
Return str+”desc”
End Function
’对录入区域返回按钮的响应
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)