简体   繁体   English

asp.net中的Objectdatasource

[英]Objectdatasource in asp.net

I'm using Objectdatasource to populate a gridview. 我正在使用Objectdatasource来填充gridview。 In the gridview one of the column is having encrypted values in database. 在gridview中,其中一列在数据库中具有加密值。 I want to load the decrypted values of the column in the gridview. 我想在gridview中加载列的解密值。

Here are the gridview and the objectdatasource 这是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>

Convert your column in a template column with a label. 使用标签转换模板列中的列。

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

The register to the GridView OnRowDataBoundEvent . GridView OnRowDataBoundEvent In the event handler decrypt the value and set the label text accordingly. 在事件处理程序中解密该值并相应地设置标签文本。

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;
    }
}
<asp:TemplateField HeaderText="SomeField" SortExpression="SomeField">
    <ItemTemplate>
        <asp:Label ID="Label1" runat="server" Text='<%# Decrypt(Eval("SomeField").ToString()) %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM