无法让列按字母顺序显示

发布于 2024-12-13 12:30:47 字数 8540 浏览 0 评论 0原文

代码如下:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

Dim SQLConn As SqlConnection = New SqlConnection()
Dim strSQL As String
Dim strFilter As String
Dim objCmd 
Dim objReader As SqlDataReader
Dim DBComm As SqlCommand
Dim DBAdapt As SqlDataAdapter

Sub DBConnect
  SQLConn.ConnectionString = ConfigurationSettings.AppSettings("CString")
  SQLConn.Open()
End Sub

Sub DBDisconnect
  SQLConn.close()
End Sub

Sub Page_Load
  Dim ThisFilter As Object = ViewState("vsFilter")
    If Not (ThisFilter Is Nothing) Then
        strFilter = CStr(ThisFilter)
    Else
        strFilter = "All"
    End If
    If Not Page.IsPostBack Then
        BindData(True)
    End If
End Sub

Sub BindData(ByVal GetFresh As Boolean)
    Call DBConnect()

    Dim DTable As DataTable = Nothing
    If ViewState("vsSortData") Is Nothing Or GetFresh Then
        If Session("Agency_Name") = "Main Company" Then
            strSQL = "SELECT LOGIN.Login_ID, LOGIN.Login_Last_Name + ', ' + LOGIN.Login_First_Name + ' ' + CASE WHEN Login_Middle_Name IS NULL THEN '' ELSE CONVERT(varchar, Login_Middle_Name) END AS FullName, AGENCY.Agency_Name, '(' + Substring(Login_Phone, 0, 4) + ') ' + Substring(Login_Phone, 4, 3) + '-' + Substring(Login_Phone, 7, 4) as Phone, LOGIN.Login_Email, CASE WHEN Login_Type = 'U' THEN 'User' WHEN Login_Type = 'I' THEN 'ISC' ELSE CONVERT(varchar, Login_Type) END AS LoginType, CASE WHEN Login_Account_Active = 'T' THEN 'Yes' WHEN Login_Account_Active = 'F' THEN 'No' ELSE CONVERT(varchar, Login_Account_Active) END AS AcctActive FROM LOGIN INNER JOIN AGENCY ON LOGIN.Login_Agency_ID = AGENCY.Agency_ID WHERE LOGIN.Login_Deleted = 'F'"
        Else
            strSQL = "SELECT LOGIN.Login_ID, LOGIN.Login_Last_Name + ', ' + LOGIN.Login_First_Name + ' ' + CASE WHEN Login_Middle_Name IS NULL THEN '' ELSE CONVERT(varchar, Login_Middle_Name) END AS FullName, AGENCY.Agency_Name, '(' + Substring(Login_Phone, 0, 4) + ') ' + Substring(Login_Phone, 4, 3) + '-' + Substring(Login_Phone, 7, 4) as Phone, LOGIN.Login_Email, CASE WHEN Login_Type = 'U' THEN 'User' WHEN Login_Type = 'I' THEN 'ISC' ELSE CONVERT(varchar, Login_Type) END AS LoginType, CASE WHEN Login_Account_Active = 'T' THEN 'Yes' WHEN Login_Account_Active = 'F' THEN 'No' ELSE CONVERT(varchar, Login_Account_Active) END AS AcctActive FROM LOGIN INNER JOIN AGENCY ON LOGIN.Login_Agency_ID = AGENCY.Agency_ID WHERE LOGIN.Login_Deleted = 'F' AND AGENCY.Agency_Name='" & Session("Agency_Name") & "'"
        End If
        DBComm = New SqlCommand(strSQL, SQLConn)
        DBAdapt = New SqlDataAdapter(DBComm)
        Dim DSet As New DataSet()
        Try
            DBAdapt.Fill(DSet)
            DTable = DSet.Tables(0)
        Catch EXC As SqlException
            Me.lblMsg2.Text = EXC.Message
            Return
        Finally
            SQLConn.Close()
        End Try
        ViewState("vsSortData") = DTable
    Else
        DTable = CType(ViewState("vsSortData"), DataTable)
    End If
    If strFilter = "All" Then
        DTable.DefaultView.RowFilter = String.Empty
    Else
        DTable.DefaultView.RowFilter = "FullName LIKE '" & strFilter & "%'"
    End If
    Me.datagrid.DataSource = DTable.DefaultView
    Me.datagrid.DataBind()
    BuildAlphaPager()

    Call DBDisconnect()
End Sub

Sub BuildAlphaPager()
    Dim DTable As DataTable
    If ViewState(("strLetter")) Is Nothing Then
        Dim arrLetters As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "All"}
        DTable = New DataTable()
        DTable.Columns.Add(New DataColumn("Letter", GetType(String)))
        Dim i As Integer
        For i = 0 To arrLetters.Length - 1
            Dim DRow As DataRow = DTable.NewRow()
            DRow(0) = arrLetters(i)
            DTable.Rows.Add(DRow)
        Next i
        ViewState("strLetter") = DTable
    Else
        DTable = CType(ViewState("strLetter"), DataTable)
    End If
    Me.rptLetters.DataSource = DTable.DefaultView
    Me.rptLetters.DataBind()
End Sub

Protected Sub rptLetters_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If (e.Item.ItemType = ListItemType.Header) Then
    ElseIf (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim lnkAlpha As LinkButton = CType(e.Item.FindControl("lnkAlpha"), LinkButton)
        lnkAlpha.Text = DataBinder.Eval(e.Item.DataItem, "Letter")
        lnkAlpha.CommandName = "Filter"
        lnkAlpha.CommandArgument = DataBinder.Eval(e.Item.DataItem, "Letter")
        Dim DRView As DataRowView = CType(e.Item.DataItem, DataRowView)
        If CStr(DRView(0)) = strFilter Then
            lnkAlpha.Enabled = False
        End If
    ElseIf (e.Item.ItemType = ListItemType.Footer) Then
    End If
End Sub

Protected Sub rptLetters_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
    If e.CommandName = "Filter" Then
        strFilter = CStr(e.CommandArgument)
        ViewState("vsFilter") = strFilter
        BindData(False)
    End If
End Sub

</script>

<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
<form runat="server">

<font size="1" color="#FF0000"><strong><asp:label id="lblMsg2" runat="server" /></strong></font>

<div align="center"><h3>
<asp:Repeater ID="rptLetters" runat="server" OnItemDataBound="rptLetters_ItemDataBound" OnItemCommand="rptLetters_ItemCommand">
    <ItemTemplate>
        <asp:LinkButton ID="lnkAlpha" runat="server" />
    </ItemTemplate>
</asp:Repeater>
</h3></div>

<asp:DataGrid ID="datagrid" runat="server" AutoGenerateColumns="False" AllowSorting="true" width="725px" BackColor="Gray" BorderWidth="0" BorderStyle="None" BorderColor="#DEBA84" Font-Size="XX-Small" CellPadding="3" CellSpacing="1">
    <HeaderStyle font-size="XX-Small" font-names="Verdana" font-bold="True" horizontalalign="Center" forecolor="White" bordercolor="White" backcolor="#1E3769" />
    <AlternatingItemStyle BackColor = "Gray" CssClass = "row" />
    <ItemStyle CssClass = "row" />
    <Columns>
  <asp:TemplateColumn ItemStyle-Width="200px" ItemStyle-HorizontalAlign="center" HeaderText="Full Name" SortExpression="Login_Last_Name">
    <ItemTemplate> <a style="color: #000000;" href="mailto:<%# Container.DataItem("Login_Email")%>"> 
    <%# Container.DataItem("FullName")%> </a> </ItemTemplate>
  </asp:TemplateColumn>
  <asp:BoundColumn ItemStyle-Width="160px" ItemStyle-HorizontalAlign="center" DataField="Agency_Name" HeaderText="Agency Name" SortExpression="Agency_Name"></asp:BoundColumn>
  <asp:BoundColumn ItemStyle-Width="110px" ItemStyle-HorizontalAlign="center" DataField="Phone" HeaderText="Phone Number" SortExpression="Login_Phone"></asp:BoundColumn>
  <asp:BoundColumn ItemStyle-Width="80px" ItemStyle-HorizontalAlign="center" DataField="LoginType" HeaderText="User Type" SortExpression="Login_Type"></asp:BoundColumn>
  <asp:BoundColumn ItemStyle-Width="115px" ItemStyle-HorizontalAlign="center" DataField="AcctActive" HeaderText="Account Active" SortExpression="Login_Account_Active"></asp:BoundColumn>
  <asp:TemplateColumn ItemStyle-Width="40px" ItemStyle-HorizontalAlign="center" HeaderText="Details">
    <ItemTemplate> 
    <a style="color: #000000;" href="javascript:openDetail('userDetail.aspx?id=<%# Container.DataItem("Login_ID")%>');">
    Details </a> </ItemTemplate>
  </asp:TemplateColumn>
  <asp:TemplateColumn ItemStyle-Width="25px" ItemStyle-HorizontalAlign="center" HeaderText="Edit">
    <ItemTemplate> <a style="color: #000000;" href="edAccounts.aspx?f=e&id=<%# Container.DataItem("Login_ID")%>"> 
    Edit </a> </ItemTemplate>
  </asp:TemplateColumn>
</Columns>
</asp:DataGrid>

</form>
</body>
</html>

我只希望全名列(第一列,FullName)默认按字母顺序排列,然后如果他们单击特定列,则该列应按字母顺序排列。这曾经有效,直到我应用上述解决方案根据名称中的字母进行分页。

我尝试修改 SQL 查询并包括“ORDER BY FullName”和“ORDER BY LOGIN.Login_Last_Name”以及许多其他内容,但默认情况下似乎无法按此顺序排序,单击时也不会进行任何其他列排序正如应该发生的那样。不知何故,字母分页不允许对这些字段进行进一步排序。

这是带有 VB 的 ASP.NET 2.0。帮助?

Here is the code:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

Dim SQLConn As SqlConnection = New SqlConnection()
Dim strSQL As String
Dim strFilter As String
Dim objCmd 
Dim objReader As SqlDataReader
Dim DBComm As SqlCommand
Dim DBAdapt As SqlDataAdapter

Sub DBConnect
  SQLConn.ConnectionString = ConfigurationSettings.AppSettings("CString")
  SQLConn.Open()
End Sub

Sub DBDisconnect
  SQLConn.close()
End Sub

Sub Page_Load
  Dim ThisFilter As Object = ViewState("vsFilter")
    If Not (ThisFilter Is Nothing) Then
        strFilter = CStr(ThisFilter)
    Else
        strFilter = "All"
    End If
    If Not Page.IsPostBack Then
        BindData(True)
    End If
End Sub

Sub BindData(ByVal GetFresh As Boolean)
    Call DBConnect()

    Dim DTable As DataTable = Nothing
    If ViewState("vsSortData") Is Nothing Or GetFresh Then
        If Session("Agency_Name") = "Main Company" Then
            strSQL = "SELECT LOGIN.Login_ID, LOGIN.Login_Last_Name + ', ' + LOGIN.Login_First_Name + ' ' + CASE WHEN Login_Middle_Name IS NULL THEN '' ELSE CONVERT(varchar, Login_Middle_Name) END AS FullName, AGENCY.Agency_Name, '(' + Substring(Login_Phone, 0, 4) + ') ' + Substring(Login_Phone, 4, 3) + '-' + Substring(Login_Phone, 7, 4) as Phone, LOGIN.Login_Email, CASE WHEN Login_Type = 'U' THEN 'User' WHEN Login_Type = 'I' THEN 'ISC' ELSE CONVERT(varchar, Login_Type) END AS LoginType, CASE WHEN Login_Account_Active = 'T' THEN 'Yes' WHEN Login_Account_Active = 'F' THEN 'No' ELSE CONVERT(varchar, Login_Account_Active) END AS AcctActive FROM LOGIN INNER JOIN AGENCY ON LOGIN.Login_Agency_ID = AGENCY.Agency_ID WHERE LOGIN.Login_Deleted = 'F'"
        Else
            strSQL = "SELECT LOGIN.Login_ID, LOGIN.Login_Last_Name + ', ' + LOGIN.Login_First_Name + ' ' + CASE WHEN Login_Middle_Name IS NULL THEN '' ELSE CONVERT(varchar, Login_Middle_Name) END AS FullName, AGENCY.Agency_Name, '(' + Substring(Login_Phone, 0, 4) + ') ' + Substring(Login_Phone, 4, 3) + '-' + Substring(Login_Phone, 7, 4) as Phone, LOGIN.Login_Email, CASE WHEN Login_Type = 'U' THEN 'User' WHEN Login_Type = 'I' THEN 'ISC' ELSE CONVERT(varchar, Login_Type) END AS LoginType, CASE WHEN Login_Account_Active = 'T' THEN 'Yes' WHEN Login_Account_Active = 'F' THEN 'No' ELSE CONVERT(varchar, Login_Account_Active) END AS AcctActive FROM LOGIN INNER JOIN AGENCY ON LOGIN.Login_Agency_ID = AGENCY.Agency_ID WHERE LOGIN.Login_Deleted = 'F' AND AGENCY.Agency_Name='" & Session("Agency_Name") & "'"
        End If
        DBComm = New SqlCommand(strSQL, SQLConn)
        DBAdapt = New SqlDataAdapter(DBComm)
        Dim DSet As New DataSet()
        Try
            DBAdapt.Fill(DSet)
            DTable = DSet.Tables(0)
        Catch EXC As SqlException
            Me.lblMsg2.Text = EXC.Message
            Return
        Finally
            SQLConn.Close()
        End Try
        ViewState("vsSortData") = DTable
    Else
        DTable = CType(ViewState("vsSortData"), DataTable)
    End If
    If strFilter = "All" Then
        DTable.DefaultView.RowFilter = String.Empty
    Else
        DTable.DefaultView.RowFilter = "FullName LIKE '" & strFilter & "%'"
    End If
    Me.datagrid.DataSource = DTable.DefaultView
    Me.datagrid.DataBind()
    BuildAlphaPager()

    Call DBDisconnect()
End Sub

Sub BuildAlphaPager()
    Dim DTable As DataTable
    If ViewState(("strLetter")) Is Nothing Then
        Dim arrLetters As String() = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "All"}
        DTable = New DataTable()
        DTable.Columns.Add(New DataColumn("Letter", GetType(String)))
        Dim i As Integer
        For i = 0 To arrLetters.Length - 1
            Dim DRow As DataRow = DTable.NewRow()
            DRow(0) = arrLetters(i)
            DTable.Rows.Add(DRow)
        Next i
        ViewState("strLetter") = DTable
    Else
        DTable = CType(ViewState("strLetter"), DataTable)
    End If
    Me.rptLetters.DataSource = DTable.DefaultView
    Me.rptLetters.DataBind()
End Sub

Protected Sub rptLetters_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
    If (e.Item.ItemType = ListItemType.Header) Then
    ElseIf (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
        Dim lnkAlpha As LinkButton = CType(e.Item.FindControl("lnkAlpha"), LinkButton)
        lnkAlpha.Text = DataBinder.Eval(e.Item.DataItem, "Letter")
        lnkAlpha.CommandName = "Filter"
        lnkAlpha.CommandArgument = DataBinder.Eval(e.Item.DataItem, "Letter")
        Dim DRView As DataRowView = CType(e.Item.DataItem, DataRowView)
        If CStr(DRView(0)) = strFilter Then
            lnkAlpha.Enabled = False
        End If
    ElseIf (e.Item.ItemType = ListItemType.Footer) Then
    End If
End Sub

Protected Sub rptLetters_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
    If e.CommandName = "Filter" Then
        strFilter = CStr(e.CommandArgument)
        ViewState("vsFilter") = strFilter
        BindData(False)
    End If
End Sub

</script>

<html>
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
<form runat="server">

<font size="1" color="#FF0000"><strong><asp:label id="lblMsg2" runat="server" /></strong></font>

<div align="center"><h3>
<asp:Repeater ID="rptLetters" runat="server" OnItemDataBound="rptLetters_ItemDataBound" OnItemCommand="rptLetters_ItemCommand">
    <ItemTemplate>
        <asp:LinkButton ID="lnkAlpha" runat="server" />
    </ItemTemplate>
</asp:Repeater>
</h3></div>

<asp:DataGrid ID="datagrid" runat="server" AutoGenerateColumns="False" AllowSorting="true" width="725px" BackColor="Gray" BorderWidth="0" BorderStyle="None" BorderColor="#DEBA84" Font-Size="XX-Small" CellPadding="3" CellSpacing="1">
    <HeaderStyle font-size="XX-Small" font-names="Verdana" font-bold="True" horizontalalign="Center" forecolor="White" bordercolor="White" backcolor="#1E3769" />
    <AlternatingItemStyle BackColor = "Gray" CssClass = "row" />
    <ItemStyle CssClass = "row" />
    <Columns>
  <asp:TemplateColumn ItemStyle-Width="200px" ItemStyle-HorizontalAlign="center" HeaderText="Full Name" SortExpression="Login_Last_Name">
    <ItemTemplate> <a style="color: #000000;" href="mailto:<%# Container.DataItem("Login_Email")%>"> 
    <%# Container.DataItem("FullName")%> </a> </ItemTemplate>
  </asp:TemplateColumn>
  <asp:BoundColumn ItemStyle-Width="160px" ItemStyle-HorizontalAlign="center" DataField="Agency_Name" HeaderText="Agency Name" SortExpression="Agency_Name"></asp:BoundColumn>
  <asp:BoundColumn ItemStyle-Width="110px" ItemStyle-HorizontalAlign="center" DataField="Phone" HeaderText="Phone Number" SortExpression="Login_Phone"></asp:BoundColumn>
  <asp:BoundColumn ItemStyle-Width="80px" ItemStyle-HorizontalAlign="center" DataField="LoginType" HeaderText="User Type" SortExpression="Login_Type"></asp:BoundColumn>
  <asp:BoundColumn ItemStyle-Width="115px" ItemStyle-HorizontalAlign="center" DataField="AcctActive" HeaderText="Account Active" SortExpression="Login_Account_Active"></asp:BoundColumn>
  <asp:TemplateColumn ItemStyle-Width="40px" ItemStyle-HorizontalAlign="center" HeaderText="Details">
    <ItemTemplate> 
    <a style="color: #000000;" href="javascript:openDetail('userDetail.aspx?id=<%# Container.DataItem("Login_ID")%>');">
    Details </a> </ItemTemplate>
  </asp:TemplateColumn>
  <asp:TemplateColumn ItemStyle-Width="25px" ItemStyle-HorizontalAlign="center" HeaderText="Edit">
    <ItemTemplate> <a style="color: #000000;" href="edAccounts.aspx?f=e&id=<%# Container.DataItem("Login_ID")%>"> 
    Edit </a> </ItemTemplate>
  </asp:TemplateColumn>
</Columns>
</asp:DataGrid>

</form>
</body>
</html>

I just want the Full Name column (first column, FullName) to be in alphabetical order by default, and then if they click on a specific column then that column should arrange itself alphabetically. This used to work until I applied the above solution to paginate based on the letter in the name.

I have tried modifying the SQL queries and including "ORDER BY FullName" and "ORDER BY LOGIN.Login_Last_Name" and many other things but can't seem to get it to order by this by default, nor does any other column sort when clicked on as should occur. Somehow the alphabetic pagination is not allowing these fields to be sorted any further.

This is ASP.NET 2.0 with VB. Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-12-20 12:30:47

我推荐这个示例,我作为个人注释,我建议使用以 ObjectDataSource 为例,因为它允许您将表示层(页面)与数据访问层(执行 sql 并检索数据的类)分开

I recommend this example and i as a personal note I would recommend using the example with the ObjectDataSource as it allows you to separate your presentation layer (pages) prom your data access layer (classes that execute sql and retrieve the data)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文