Objectdatasource in asp.net
本文关键字:net asp in Objectdatasource | 更新日期: 2023-09-27 18:10:47
我使用Objectdatasource填充gridview。在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>
<asp:TemplateField HeaderText="SomeField" SortExpression="SomeField">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Decrypt(Eval("SomeField").ToString()) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
将您的列转换为模板列。
<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;
}
}