VB.NET是微软asp之后、ASP.NET之前的Web开发技术。
前台页面的拓展名是.aspx,后端扩展名是.vb,前台aspx页面继承自.aspx.vb,aspx页面基本上和asp.net中aspx页面一模一样,主要就是服务器控件都是一样的,Grid的Eval绑定表达式都是一样的。
后端的VB和C#语法还是有一定区别的, 写惯了C#,觉得vb语法不优雅
1. 基本语法:
Dim sort As String Protected Sub BindLines() dropLines.DataSource = viewCurrentCountsBL.GetLines() dropLines.DataTextField = "DESCRIPTION" dropLines.DataValueField = "LINE_ID" dropLines.DataBind() End Sub Protected Function GetIco(itemType As String) As String If itemType = "1" Then Return "<img src='/images/tool.png'/>" ElseIf itemType = "2" Then Return "<img src='/images/Kits.png'/>" Else Return "" End If End Function Protected Sub chkHideTools_CheckedChanged(sender As Object, e As EventArgs) Handles chkHideTools.CheckedChanged BindGird() End Sub
其中,跟C#中一些关键字差异的地方:Dim申明值类型、类变量,As 类型。Sub/End Sub,If/End If 相当于C#中If{},void Fun(){}
C#中a!=null, vb中a Is Nothing
C# !布尔值变量、表达式写法位!flag、!IsValid(), vb中Not flag、 not IsValid()
2. VB.NET中的GridView Sorting
2.1.利用table.DefaultView.Sort='SortExpression sortDirection'
Protected Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting Dim table = Session("ViewCurrentCountGridData") Dim sortDirection As String Dim sort As String If table Is Nothing Then If Request.QueryString("operationIds") IsNot Nothing AndAlso Not String.IsNullOrEmpty(Request.QueryString("operationIds").ToString()) Then Dim operationIds As New System.Collections.ObjectModel.Collection(Of Long) table = viewCurrentCountsBL.GetItemsForOps(operationIds, chkHideTools.Checked) Session("ViewCurrentCountGridData") = table Else table = viewCurrentCountsBL.GetItems(Long.Parse(dropLines.SelectedValue), 0, chkHideTools.Checked) Session("ViewCurrentCountGridData") = table End If End If If String.IsNullOrEmpty(e.SortExpression) Then sort = "last_update desc" Else If ViewState("SortDirection") IsNot Nothing AndAlso ViewState("SortDirection").ToString() = "asc" Then sortDirection = "desc" Else sortDirection = "asc" End If sort = e.SortExpression + " " + sortDirection ViewState("SortDirection") = sortDirection End If table.DefaultView.Sort = sort GridView1.DataSource = table.DefaultView GridView1.DataBind() End Sub
2.2 用DataTable.Select(filterExpression, sortStr) ,下面的4中有示例代码。
3. Decimal取14位:
Math.Round(Decimal.Parse(hours.ToString()),14)
4. 实现下图效果:
如果是按照Hours Remaining排序的时候,根据Hours Remaining值的范围2以下,2-4,4-6,6-8,8-10,10以上,"分组"显示,显示组标题。
4.1. 后台计算得出规律,生成json字符串,在前台解析,通过JS添加tr
Protected Sub GridView1_Sorting(sender As Object, e As GridViewSortEventArgs) Handles GridView1.Sorting Dim table = Session("ViewCurrentCountGridData") Dim sortDirection As String Dim sort As String Dim filter = String.Format("hours<={0}", hoursUnder) If table Is Nothing Then table = GetData() Session("ViewCurrentCountGridData") = table End If If String.IsNullOrEmpty(e.SortExpression) Then sort = "last_update desc" Else If ViewState("SortDirection") IsNot Nothing AndAlso ViewState("SortDirection").ToString() = "asc" Then sortDirection = "desc" Else sortDirection = "asc" End If sort = e.SortExpression + " " + sortDirection ViewState("SortField") = e.SortExpression ViewState("SortDirection") = sortDirection End If GridView1.DataSource = table.Select(filter, sort) GridView1.DataBind() If e.SortExpression.ToLower() = "hours" Then SetUnderHours(table) End If End Sub Protected Sub SetUnderHours(table As DataTable) Dim listHours = ( From item In table.AsEnumerable() Where Not item.IsNull("hours") Select item.Field(Of Decimal)("hours") ).ToList().Where(Function(m) m <= hoursUnder).ToList() Dim query2 = From el In listHours Group el By key = GetUnderKey(el) Into g = Group _ Select New With { _ .Key = key, _ .min = g.Min(), .max = g.Max() } Dim list = query2.ToList() If list IsNot Nothing Then json = "[" For i As Integer = 0 To list.Count - 1 If i = list.Count - 1 Then json += "{'underKey':" + list(i).Key.ToString() + ",'min':" + list(i).min.ToString() + ",'max':" + list(i).max.ToString() + "}" Else json += "{'underKey':" + list(i).Key.ToString() + ",'min':" + list(i).min.ToString() + ",'max':" + list(i).max.ToString() + "}," End If Next json += "]" End If End Sub
Public Function GetUnderKey(hours As Decimal) As Integer If hours <= 0 Then Return -1 ElseIf 0 < hours And hours <= 2 Then Return 2 ElseIf 2 < hours And hours <= 4 Then Return 4 ElseIf 4 < hours And hours <= 6 Then Return 6 ElseIf 6 < hours And hours <= 8 Then Return 8 ElseIf 8 < hours And hours <= 10 Then Return 10 ElseIf hours > 10 Then Return 11 Else Return -1 End If End Function
<script type="text/javascript"> $(function () { var jsonStr = eval("<%=json%>"); var sortField = "<%=ViewState("SortField")%>"; var sortDirection="<%=ViewState("SortDirection")%>"; if (sortField.toLowerCase() == "hours") { $.each(jsonStr, function (i, item) { if (sortDirection == "desc") { var maxRow = $("#<%=GridView1.ClientID %> tr input[data-field='hours'][value='" + item.max.toFixed(14) + "']").eq(0).parent().parent(); var title = item.underKey == 11 ? "Over 10 Hours" : "Under " + item.underKey + " Hours"; maxRow.before($("<tr><td style='background-color:blue;color:white'>" + title + " </td><td colspan='5'></td></tr>")); } else { var minRow = $("#<%=GridView1.ClientID %> tr input[data-field='hours'][value='" + item.min.toFixed(14) + "']").eq(0).parent().parent(); var title = item.underKey == 11 ? "Over 10 Hours" : "Under " + item.underKey + " Hours"; minRow.before($("<tr><td style='background-color:blue;color:white'>" + title + " </td><td colspan='5'></td></tr>")); } }); } }); </script>
其中的难点:1.思路 2.vb的linq语法,特别根据列值,得出计算值再分组,不太好弄。
4.2 用C# linq 实现的group,返回json的代码如下:
4.3 在服务端插入group row,代码如下: