3

我正在使用 Objectdatasource 来填充网格视图。在 gridview 中,其中一列在数据库中具有加密值。我想在 gridview 中加载列的解密值。

这是gridview和objectdatasource

<asp:GridView ID="GridView1" SkinID="GridView" runat="server" Width="100%" AutoGenerateColumns="False"
                            AllowPaging="True" PageSize="10" DataSourceID="ObjectDataSource1" AllowSorting="True"
                            AlternatingRowStyle-BackColor="Silver" HeaderStyle-BackColor="#666666" HeaderStyle-ForeColor="White">
                            <Columns>
                                <asp:HyperLinkField DataNavigateUrlFields="Taxi_Id" HeaderText="Edit" DataNavigateUrlFormatString="TaxiForm.aspx?ID={0}&E=1"
                                    Text="Edit" />
                                <asp:BoundField DataField="Taxi_Id" HeaderText="Taxi_Id" SortExpression="Driver_Id"
                                    Visible="False" />
                                <asp:BoundField DataField="MedallionNo" HeaderText="Medallion No" SortExpression="MeddallionNo" />  
                                <asp:BoundField DataField="Car_Brand_Name" HeaderText="Brand Name" SortExpression="Car_Brand_Name" />
                                <asp:BoundField DataField="Car_Model_Name" HeaderText="Model Name" SortExpression="Car_Model_Name" />
                                <asp:BoundField DataField="Car_Year" HeaderText="Year" SortExpression="Car_Year" />                                                              
                                <asp:BoundField DataField="Affiliation_Name" HeaderText="Affiliation" SortExpression="Affiliation_Name" />
                                <asp:BoundField DataField="Mileage" HeaderText="Mileage" SortExpression="Mileage" />
                                <asp:BoundField DataField="Taxi_Company_Garage_Name" HeaderText="PickUp Location" SortExpression="Taxi_Company_Garage_Name" />                                
                                <asp:HyperLinkField DataNavigateUrlFields="Taxi_Id" HeaderText="Shift Rate" DataNavigateUrlFormatString="TaxiShiftRateList.aspx?ID={0}&E=1" Text="View Shift Rate" />
                            </Columns>
                        </asp:GridView>


<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
                            SelectMethod="Owner_GetAll" TypeName="TMS.TaxiObj" >
                            <DeleteParameters>
                                <asp:Parameter Name="Driver_Id" Type="Int32" />
                            </DeleteParameters>
                            <SelectParameters>
                                <asp:ControlParameter ControlID="txtMedallionNo" Name="MedallionNo" PropertyName="Text"
                                    Type="String" DefaultValue="" />
                                <asp:ControlParameter ControlID="ddlBrand" Name="Car_Brand_Id" PropertyName="Text"
                                    Type="Int32" DefaultValue="-1" />
                                <asp:ControlParameter ControlID="ddlModel" Name="Car_Model_Id" PropertyName="Text"
                                    Type="Int32" DefaultValue="-1" />
                                <asp:ControlParameter ControlID="ddlAffiliation" Name="Affiliation_Id" PropertyName="Text"
                                    Type="Int32" DefaultValue="-1" />
                            </SelectParameters>
                        </asp:ObjectDataSource>
4

2 回答 2

0

将您的列转换为带有标签的模板列。

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label runat="server" Id="lbl"></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

注册到GridView OnRowDataBoundEvent. 在事件处理程序中解密该值并相应地设置标签文本。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    Label lbl = e.Row.FindControl("lbl") as Label;
    if (lbl != null) {
        var encrypted = DataBinder.Eval(e.Row.DataItem, "EncryptedColumn");
        string decrypted = Decrypt(encrypted);
        lbl.Text = decrypted;
    }
}
于 2013-04-24T15:36:49.503 回答
0
<asp:TemplateField HeaderText="SomeField" SortExpression="SomeField">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Decrypt(Eval("SomeField").ToString()) %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>
于 2013-04-24T15:38:05.990 回答