2016-05-02 11 views
0

私のデータベースのデータは暗号化されています。私はGridViewの上に印刷するとき、それはこのように書きます:ASP暗号化データのソート

私のマーク上に:これは私の選択方法

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

​​

:私のコードで

<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> 

問題は、私が列を並べ替えると、暗号化されたデータが暗号化されずにソートされます。暗号化されたデータをソートする方法です私のgridviewに表示されますか?

ソートする前にフィールドを復号化する方法はありますか?ありがとうございました!

+3

GridViewにバインドする前に復号化してみませんか? – mason

+0

gridviewのデータソースとして何を使用していますか? – DVK

+0

こんにちは@mason、私は私のポストを更新しました、あなたがGridviewにバインドする前に復号化についてあなたのアイデアを投稿する場合は本当に感謝します。私はselect allを使用して返す方法を知っていますが、フィールドを1つずつ選択して最初に解読する方法はわかりません。ありがとうございました! – Katherine

答えて

1

イベントで単純に復号化し、手動でソートを実行します。

グリッドビューには、実際には、GridView.Sortingイベントが含まれている必要があります。目的:

ハイパーリンクが列をソートする際のソートイベントが発生するが クリックされたが、GridViewコントロールが並べ替え操作を処理する前に。 これにより、 イベントが発生するたびに並べ替え操作のキャンセルなど、 カスタムルーチンを実行するイベント処理メソッドを提供できます。 A 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; 
    } 
+0

ありがとうございました!私はこれを試してみよう! – Katherine

0

ストアリストにクエリ。格納中に必要な値を復号化します。次に、リストを

list.AsQueryable(); 

として返し、gridviewと互換性があるようにします。