学无止境

-------至弱即为至强!

导航

ASP.NET程序编写注意

Posted on 2006-05-25 11:30  赵国亮  阅读(1558)  评论(5编辑  收藏  举报

1.各个输入文本框添加是否为空的判断,不可为空;

2.注意尽量符合命名规范;

3.尽量做好注释,方便程序阅读和以后的修改;

4.做好上传文件大小的限制;

5.在编写前台代码时加上注释头文件,以方便二次修改;

6.注意每个页面的title

7.注意前台显示字符串长度;

8.需要输出显示时间的存储过程请将时间如下输出:

CONVERT(varchar(10),AddTime,120) AS AddTime

9.需要在前台显示标题的存储过程,如果可能出现标题过长,导致页面变形的情况的话,可用如下存储过程输出文章标题

case

when len(NewsTitle)>20 then left(NewsTitle,20)+'...'

when len(NewsTitle)<21 then NewsTitle

end

AS NewsTitle

或在前台输出时控制字数多少。

10.注意输入标题的长度限制

11.注意表格的强制换行

12.写类似添加或修改的存储过程时注意被添加或修改的数据是否在表中已存在,如果已存在,则提示已有该数据,通过存储过程返回值来判断。

13.为了维护方便起见,能在前台写的东西不要放到程序里去生成。比如链接、表格等...

14.进行字符()替换,按原样显示

15.DataGrid选定列特殊样式显示

If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then

       e.Item.Attributes.Add("onmouseover", "this.style.backgroundColor='#E4EDF9'")

       e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor='#F1F3F5'")

End If

16.用以下样式显示分页效果

/ 本页 /  首页 上一页 下一页 尾页

前台代码如下:

<asp:panel id="pnlPage" runat="server">

<asp:Label id="lblPage" runat="server"></asp:Label>

<asp:linkbutton id="lbtnFirst" onclick="LBtnNavigation_Click" runat="server" CommandName="First">首页</asp:linkbutton>

<asp:linkbutton id="lbtnPrev" onclick="LBtnNavigation_Click" runat="server" CommandName="Prev">上一页</asp:linkbutton>&nbsp;

<asp:linkbutton id="lbtnNext" onclick="LBtnNavigation_Click" runat="server" CommandName="Next">下一页</asp:linkbutton>&nbsp;

<asp:linkbutton id="lbtnLast" onclick="LBtnNavigation_Click" runat="server" CommandName="Last">尾页</asp:linkbutton>&nbsp;

      转到

<asp:TextBox id="txtToPage" runat="server" Width="25px" Height="20px" MaxLength="9"></asp:TextBox>

<asp:LinkButton id="lbnGO" runat="server">GO</asp:LinkButton>&nbsp;&nbsp;

<asp:dropdownlist id="dlstPage" runat="server" AutoPostBack="True">

<asp:ListItem Value="10">每页显示10</asp:ListItem>

<asp:ListItem Value="20" Selected="True">每页显示20</asp:ListItem>

<asp:ListItem Value="50">每页显示50</asp:ListItem>

<asp:ListItem Value="100">每页显示100</asp:ListItem>

</asp:dropdownlist></asp:panel>

后台代码如下:

        Dim pageCount As Integer '总页数

        Dim recordCount As Integer '总条数

    

#Region " 绑定分页 "

        Protected Sub BindData(ByVal DsetData As System.Data.DataSet)

            '获取当前的总记录条数

            recordCount = DsetData.Tables(0).Rows.Count

            '获取当前的页数

            pageCount = CInt(Math.Ceiling(recordCount * 1.0 / dgrdRegister.PageSize))

            '避免纪录从有到无时,并且已经进行过反页的情况下CurrentPageIndex > PageCount出错

            If recordCount = 0 Then

                dgrdRegister.CurrentPageIndex = 0

            ElseIf dgrdRegister.CurrentPageIndex >= pageCount Then

                dgrdRegister.CurrentPageIndex = pageCount - 1

            End If

            NavigationStateChange()

        End Sub

        '初始化页码按钮

        Protected Sub NavigationStateChange()

            If recordCount <= dgrdRegister.PageSize Then '( RecordCount <= PageSize )//小于等于一页

                lbtnFirst.Enabled = False

                lbtnPrev.Enabled = False

                lbtnNext.Enabled = False

                lbtnLast.Enabled = False

            Else '有多页

                If dgrdRegister.CurrentPageIndex = 0 Then  '当前为第一页

                    lbtnFirst.Enabled = False

                    lbtnPrev.Enabled = False

                    lbtnNext.Enabled = True

                    lbtnLast.Enabled = True

                ElseIf dgrdRegister.CurrentPageIndex = pageCount - 1 Then '当前为最后页

                    lbtnFirst.Enabled = True

                    lbtnPrev.Enabled = True

                    lbtnNext.Enabled = False

                    lbtnLast.Enabled = False

                Else '中间页

                    lbtnFirst.Enabled = True

                    lbtnPrev.Enabled = True

                    lbtnNext.Enabled = True

                    lbtnLast.Enabled = True

                End If

            End If

 

            Dim intPageCount As Integer

            Dim intPageIndex As Integer

            Dim intPageSize As Integer

            Dim intRecordCount As Integer

 

            If recordCount = 0 Then '当没有纪录时DataGrid.PageCount会显示1

                intPageCount = 0

                intPageIndex = 0

                intPageSize = 0

                intRecordCount = 0

            Else

                intPageCount = pageCount

                intPageIndex = dgrdRegister.CurrentPageIndex + 1 '在有页数的情况下前台显示页数加1

                If intPageIndex = pageCount Then

                    intPageSize = recordCount - (pageCount - 1) * dgrdRegister.PageSize

                Else

                    intPageSize = dgrdRegister.PageSize

                End If

                intRecordCount = recordCount

            End If

 

            lblPage.Text = "" & intPageIndex & "/" & intPageCount & " 本页" & intPageSize & "/" & intRecordCount & ""

 

        End Sub

        '翻页

        Protected Sub LBtnNavigation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

 

            Dim btn As LinkButton = CType(sender, LinkButton)

            Select Case btn.CommandName

                Case "First"

                    dgrdRegister.CurrentPageIndex = 0

                Case "Prev" 'if( PageIndex > 0 )

                    dgrdRegister.CurrentPageIndex = dgrdRegister.CurrentPageIndex - 1

                Case "Next" 'if( PageIndex < PageCount -1)

                    dgrdRegister.CurrentPageIndex = dgrdRegister.CurrentPageIndex + 1

                Case "Last"

                    dgrdRegister.CurrentPageIndex = dgrdRegister.PageCount - 1

            End Select

            BindData()

        End Sub

        '分页

        Private Sub dlstPage_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dlstPage.SelectedIndexChanged

            dgrdRegister.PageSize = CInt(dlstPage.SelectedValue)

            txtToPage.Text = ""

            dgrdRegister.CurrentPageIndex = 0

            BindData()

        End Sub

        '跳转页

        Private Sub lbnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbnGO.Click

            Dim ToPage As Integer

            If txtToPage.Text.Trim <> "" Then

                If IsNumeric(txtToPage.Text) Then

                    ToPage = CInt(txtToPage.Text.Trim)

                    If ToPage <= pageCount Then

                        Try

                            dgrdRegister.CurrentPageIndex = ToPage - 1

                            dgrdRegister.BindData()

                        Catch ex As Exception

                            Response.Write("<script language='javascript'>alert('输入的页数不能小于等于0');history.back();</script>")

                            Response.End()

                        End Try

                    Else

                        Response.Write("<script language='javascript'>alert('您输入的页码不能大于页数');history.back();</script>")

                        Response.End()

                    End If

                Else

                    Response.Write("<script language='javascript'>alert('输入的页码数为必须数字');history.back();</script>")

                    Response.End()

                End If

            End If

        End Sub

#End Region

注释头样式:

'***********************************************************************

' Module:  Disease.vb

' Author:  赵国亮

' Modified: 2005613 16:22:00

' Purpose: Definition of the Class
' Comment: Web层数据层前台显示。

'***********************************************************************

 

DataAccess层样式:

        '----------------------------------------------------------------

        ' Name:        UpdateKeyKnowledge(ByVal KnowledgeID As Integer, ByVal objKnowledge As Sanitation.DataAccess.Key.Key)

        ' Description: 根据SeriaNo修改指定的知识,成功返回1,失败返回0

        ' Parameters:

        '    [ByVal] objKeyKnowledge -

        ' Return:      Integer

        '----------------------------------------------------------------

        Public Function UpdateKeyKnowledge(ByVal KnowledgeID As Integer, ByVal objKnowledge As Sanitation.DataAccess.Key.Key) As Integer

            ' TODO : 调用存储过程:p_Update

 

            '建立连接

            Dim conn As SqlConnection = New SqlConnection(ConnectionString)

            Dim cmd As SqlCommand = New SqlCommand("p_Update", conn)

 

            cmd.CommandType = CommandType.StoredProcedure

 

            '传递参数

            Dim param As SqlParameter

 

            '------------------------------------Knowledge表进行修改操作---------------------------------

 

            param = cmd.Parameters.Add("@KeyKnowledgeID", SqlDbType.Int)

            param.Value = KnowledgeID                                        ' 编号

 

            param = cmd.Parameters.Add("@KeyKnowledgeTitle", SqlDbType.VarChar)

            param.Value = objKnowledge.KnowledgeTitle                     ' 标题

 

            param = cmd.Parameters.Add("@KeyKnowledgeContent", SqlDbType.Text)

            param.Value = objKnowledge.KnowledgeContent                   ' 内容

 

            Dim cmm As Integer

 

            '打开连接

            conn.Open()

            cmm = cmd.ExecuteNonQuery

 

            '关闭连接

            conn.Close()

 

            '返回函数值

            If cmm > 0 Then

                Return 1

            Else

                Return 0

            End If

 

        End Function

 

页面注意

1.添加,删除,返回等按钮使用圆角图片按钮;

2.各个DataGrid外表格的宽度为95%,居中;

3.各个DataGrid头部加入正确背景;

4.文本显示要注意添加强制换行;

 

后台DataGrid中背景色

DataGrid #FFFFFF

页眉 #E4EDF9

普通项 #F1F3F5

 

测试注意问题

1.越权;

2.页面样式;

3.按钮对应功能;

4.每个页面的title

5.上传文件大小的限制;

6.各个输入文本框添加是否为空的判断;

7.空数据情况下页面显示是否正常,功能是否正常;

8.少数据情况下页面显示是否正常,功能是否正常;

9.多数据情况下页面显示是否正常,功能是否正常;

10.不同用户登陆后所见功能及操作是否正常;

11.功能是否全面;

每个页面都要做详细测试,并多多注意!

12.所有页面中有文本添加的地方,在前台是否按原样显示。

13.权限控制

用例1

用例名称

无登录权限测试

操作步骤

1  登录后打开要测试的页面,右键选择页面的属性,复制页面的地址。

2  退出登录(清空cookies),在浏览器地址栏粘贴页面地址打开页面。

应当返回结果

弹出对话框提示无权限,并终止页面的显示。

用例2

用例名称

有登录权限测试

操作步骤

1  登录后打开要测试的页面,右键选择页面的属性,复制页面的地址。

2  以没有权限的用户登录,在浏览器地址栏粘贴页面地址打开页面。

应当返回结果

弹出对话框提示无权限,并终止页面的显示。

14. 特殊字符

用例1

用例名称

输入特殊字符

操作步骤

在标题或内容中输入特殊字符:

1  空格

2  </td></tr></table>

3  <span onclick="alert('test');">test</span>

应当返回结果

进行字符()替换,按原样显示。或去掉标题中的空格(trim)

用例2

用例名称

输入连续英文字符

操作步骤

在标题或内容中输入特殊字符:

如:ttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt

应当返回结果

<table style="table-layout:fixed; word-break:break-all; word-wrap:break-word;">

<tr><td>test

</td></tr>

</table>

控制,不使表格变形!