书城计算机网络综合应用软件设计
8724600000030

第30章 软件构造(9)

Dim help As SqlHelper

块内加以分组。这样后面用到的H1中的内容都会自动应用中的属性值。H1{COLOR:red;TEXT—ALIGN:center }

Dim dt As DataTable=help.ExecuteDataset(SqlHelper.ConnectionString, Command—Type.Text,”select OperatorAccount from T_Operator where OperatorAccount="”&;account&;”"”) .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

’添加的确定

Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs ) Handles Button1.Click

If Me.InsertAccountExist(Me.txtInsertAcc.Text.Trim)Then

Me.Insertvalidator.IsValid=False

End If

If Not Page.IsValid Then

Exit Sub

End If

’插入

Dim row As New T_OperatorRow

row.OperatorAccount=Me.txtInsertAcc.Text.Trim

row.Password=Me.txtInsertPass.Text.Trim

row.RealName=Me.txtInsertName.Text.Trim

row.OperatorType=Me.DropDownList2.SelectedItem.Text

Try

Me.near.T_OperatorCollection.Insert(row)

Me.binddata()

Me.Panel1.Visible=True

Me.PanelInsert.Visible=False

Me.PanelUpdate.Visible=False

Catch ex As Exception

End Try

End Sub

当然了,对于更新操作,也可以在DataGrid中的模板列中加入其他控件来实现。

下面再给大家具体介绍,利用模板实现“更新”功能。

①添加列:添加四个模板列,一个按钮列。

在三个模板列中的ItemTemplate部分加入Label控件,分别把Label控件的text属性绑定到数据库中的列Stor_id.、stor_name、City。然后在EditItemTemplate和FooterTemplate部分加入Textbox控件,分别用于编辑和增加数据。按钮列中有三种类型的按钮,选择“编辑、更新、取消”按钮。在另一个模板的Item—Template和FooterTemplate加入button控件,用于删除和添加。

②为“编辑”按钮增加事件。

Private Sub doEdit(ByVal source As Object,ByVal e As system.Web.UI.WebControls. DataGridCommandEventArgs)Handles DataGrid1.EditCommand

Me.DataGrid1.ShowFooter=False

Me.DataGrid1.EditItemIndex=e.Item.ItemIndex

binddata()

End Sub

Private Sub doCancel(ByVal source As Object,ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)Handles DataGrid1.CancelCommand

Me.DataGrid1.ShowFooter=True

Me.DataGrid1.EditItemIndex=—1

binddata()

End Sub

③为“删除”和“添加”按钮增加事件。

Private Sub doInsertDel ( ByVal source As Object,ByVal e As System.Web.UI.WebControls. DataGridCommandEventArgs)Handles DataGrid1.ItemCommand

If e.CommandName=”Insert”Then

Dim storeid As String

Dim txtstoreid As TextBox

txtstoreid=e.Item.FindControl(”add_storID”)

storeid=txtstoreid.Text

Dim storename As String

Dim txtstorename As TextBox

txtstorename=e.Item.FindControl(”add_storname”)

storename=txtstorename.Text

Dim sql As SqlHelper

Dim ss As String=”insert into stores(stor_id,stor_name)values("”&;storeid&;”","”&;storename&;”")”

sql.ExecuteNonQuery(SqlHelper.ConnectionString,CommandType.Text,ss)

Me.DataGrid1.EditItemIndex=—1

binddata()

End If

If e.CommandName=”Delete”Then

Dim strkey As String=CType(e.Item.Cells(0).Controls(1),Label).Text

Dim sql1 As SqlHelper

Dim sqldel As String=”delete stores where stor_id="”&;strkey&;”"”

sql1.ExecuteNonQuery(SqlHelper.ConnectionString,CommandType.Text,sqldel)

binddata()

End If

End Sub

④增加分页和排序功能。

分页我们已经介绍过了。为了增加排序功能,先要进入DataGrid的“属性生成器”,然后选中第一个模板列,在下面的“排序表达式”中写入列名Stor_id,在“页眉文本”输入StoreID,作为DataGrid中的列标题。程序运行的时候,单击这个列标题,就会根据列Stor_id进行排序了。

增加事件:

Private Sub DataGrid1_SortCommand(ByVal source As Object,ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles DataGrid1.SortCommand

Dim sql As SqlHelper

sql.IniDa(SqlHelper.ConnectionString,”select*from stores”,da,dt)

Me.DataGrid1.DataSource=dt.DefaultView

dt.DefaultView.Sort=e.SortExpression

Me.DataGrid1.DataBind()

End Sub

这个例子中,是用模板列进行编辑,然后在FooterTemplate部分进行添加操作,结构紧凑,操作方便。

8.如何使用CSS

1)什么是样式表

样式表是HTML文档中元素样式定义的结合。

2)样式的实现

①CSS样式可以通过内联方式放在单个HTML元素内。

<; h1 style=”FONT—WEIGHT:bold;TEXT—TRANSFORM:uppercase;COLOR:green;TEXTDECORATION:

underline”>; hello

②也可以在Web页head部分的

③从单独CSS样式表文件中导入。

3)样式表的优先级

既然CSS有很多种实现方式,那么也就涉及不同实现方式的优先。

级别:以上面为准,优先级为:i>;ii>;iii。也就是说,优先级准则为:局部大于全局。

4)为页面元素配置CSS样式的具体CLASS

例如,把页面的一个Button的属性CssClass设置为button(在样式表里面定义的一个样式规则),就可以使页面中的Button控件都遵从一样的样式。

9.如何上传图片和文件

首先在前台页面需要加控件的地方,在其html代码里加上

在其后台.vb文件加上以下代码:

Private Sub UploadFile()

’判断是否选择了上传的文件

If FileUp.PostedFile.FileName<;>;””Then

Dim filename As String=FileUp.PostedFile.FileName

获取上传文件的扩展名

Dim index As Integer=filename.LastIndexOf(”.”)

Dim fileextendname As String=filename.Substring(index)

’给上传的文件重新命名

Dim newfilename As String=FileUp.PostedFile.ContentLength.ToString_&;fileextend1

name

’把选择的文件上传到服务器上相虚拟目录下Image文件架里

FileUp.PostedFile.SaveAs(Server.MapPath(”Image”&;newfilename))

End If

End Sub

10.如何使用树形控件

为了实现树形结构,需要用到树形控件Treeview,该控件可以从微软网站下载。这里使用嵌套循环的思路来完成,这也是最简单的思路,其缺点是代码的执行效率比较低下。对于这种类型的表,创建树还有很多改进的算法,可以使用递归,对于顺序表,还有更为简洁的算法。

如果只创建三层的树,可以参见以下代码:

Public Sub InitialTree()

Dim level1,level2,level3 As Int16

Dim dt1,dt2,dt3 As DataTable

’获取所有根节点

dt1=GetData(”select*from category where ParentCategoryID="0"”)

’第一层次的循环

For level1=0 To dt1.Rows.Count—1

Dim node1 As New Microsoft.Web.UI.WebControls.TreeNode

node1.Text=dt1.Rows(level1).Item(”CategoryName”)

’获取某一根节点的所有子节点

dt2=GetData(”select*from category where ParentCategoryID="”+dt1.Rows(level1).Item(”categoryid”)+”"”)

’第二层次的循环

For level2=0 To dt2.Rows.Count—1

Dim node2 As New Microsoft.Web.UI.WebControls.TreeNode

node2.Text=dt2.Rows(level2).Item(”CategoryName”)

’获取第二层节点的所有子节点

dt3=GetData(”select* from category where ParentCategoryID="”+dt2.Rows(level2).Item(”categoryid”)+”"”)

’第二层次的循环

For level3=0 To dt3.Rows.Count—1

Dim node3 As New Microsoft.Web.UI.WebControls.TreeNode