【问题标题】:ASP Sorting Encrypted DataASP 对加密数据进行排序
【发布时间】:2016-08-27 08:38:58
【问题描述】:

我的数据库中的数据是加密的。当我在 gridview 上打印它时,它是这样的:

在我的标记上:

 <asp:GridView ID="GridView1" SelectMethod="GetParents" ... >
     <Columns>
      <asp:TemplateField HeaderText="LastName" SortExpression="LastName">
        <ItemTemplate>
            <asp:Label Text='<%# GetDecrypted((string)Eval("LastName"))%>'
                runat="server" />
        </ItemTemplate>
      </asp:TemplateField>
     </Columns>
</asp:GridView>

在我的代码上:

protected string GetDecrypted(string Decrypt)
{
  //returns a decrypted version
   return ENCRYPTION.Decrypt(Decrypt);
}

这是我的选择方法

public IQueryable<Parent> GetParents()
    {
        InfantRecordContext db = new InfantRecordContext();
        int id = int.Parse(Session["DoctorID"].ToString());
        return db.Parent.Where(p => p.DoctorID == id);
    }

问题是,当我对列进行排序时,它对加密数据而不是解密数据进行排序,有没有一种方法可以对我的 gridview 上显示的解密数据进行排序?

或者有什么方法可以在排序之前解密该字段?

【问题讨论】:

  • 为什么不在绑定到 GridView 之前解密它?
  • 您使用什么作为 gridview 的数据源?
  • 嗨@mason,我更新了我的帖子,如果你能在将它绑定到 Gridview 之前发布你的解密想法,我将不胜感激。我知道如何使用全选返回,但我不知道如何逐个选择字段并首先解密它们。谢谢!
  • @DVK 你好!我更新了我的帖子并添加了我用于选择方法的部分谢谢!
  • 我编辑了这个问题,以便更清楚地了解您具体需要做什么。

标签: c# asp.net sorting gridview encryption


【解决方案1】:

您只需在事件中解密,然后手动执行排序。

网格视图实际上应该包含:GridView.Sorting 事件。目的:

当对列进行排序的超链接出现时,会引发 Sorting 事件 单击,但在GridView 控件处理排序操作之前。 这使您能够提供一个事件处理方法来执行 自定义例程,例如取消排序操作,每当此 事件发生。 GridViewSortEventArgs 对象被传递给 事件处理方法,使您能够确定排序 列的表达式并指示选择操作 应该取消。要取消选择操作,请设置取消 GridViewSortEventArgs 对象的属性为 true。

代码示例如下:

  protected void TaskGridView_Sorting(object sender, GridViewSortEventArgs e)
  {

    //Retrieve the table from the session object.
    DataTable dt = Session["TaskTable"] as DataTable;

    if (dt != null)
    {

      //Sort the data.
      dt.DefaultView.Sort = e.SortExpression + " " + GetSortDirection(e.SortExpression);
      TaskGridView.DataSource = Session["TaskTable"];
      TaskGridView.DataBind();
    }

  }

  private string GetSortDirection(string column)
  {

    // By default, set the sort direction to ascending.
    string sortDirection = "ASC";

    // Retrieve the last column that was sorted.
    string sortExpression = ViewState["SortExpression"] as string;

    if (sortExpression != null)
    {
      // Check if the same column is being sorted.
      // Otherwise, the default value can be returned.
      if (sortExpression == column)
      {
        string lastDirection = ViewState["SortDirection"] as string;
        if ((lastDirection != null) && (lastDirection == "ASC"))
        {
          sortDirection = "DESC";
        }
      }
    }

    // Save new values in ViewState.
    ViewState["SortDirection"] = sortDirection;
    ViewState["SortExpression"] = column;

    return sortDirection;
  }

【讨论】:

    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    相关资源
    最近更新 更多