在Gridview中加密DataNavigateUrlFormatString的查询字符串

时间:2018-08-28 05:52:02

标签: c# asp.net encryption query-string

我在这里想要加密使用asp.net Gridview形成的查询字符串。

下面是我的代码

<asp:GridView ID="gvCreatedCRList" runat="server" AutoGenerateColumns="False" CssClass="table table-bordered table-hover"
                ShowHeaderWhenEmpty="True" PageSize="10" AllowPaging="true" EmptyDataText="No data to display.">
                <Columns>
                    <asp:HyperLinkField DataTextField="CHANGEREQUESTNUMBER" ItemStyle-CssClass="GridRow"
                        HeaderText="Change Request No" DataNavigateUrlFields="CHANGEREQUESTID" DataNavigateUrlFormatString="ChangeRequestDetails.aspx?ID={0}"
                        Text="CR No" Target="_blank" ItemStyle-Width="11%" />
                    <asp:BoundField DataField="DESCRIPTION" ItemStyle-CssClass="GridRow" HeaderText="Description"
                        ReadOnly="True" ItemStyle-Width="15%" />
                    <asp:BoundField DataField="STATENAME" ItemStyle-CssClass="GridRow" HeaderText="State"
                        ReadOnly="True" SortExpression="State" ItemStyle-Width="12%" />
                    <asp:BoundField DataField="CITYNAME" ItemStyle-CssClass="GridRow" HeaderText="City"
                        ReadOnly="True" SortExpression="City" ItemStyle-Width="7%" />
                    <asp:BoundField DataField="CATEGORY" ItemStyle-CssClass="GridRow" HeaderText="Category"
                        ReadOnly="True" SortExpression="Category" ItemStyle-Width="7%" />
                    <asp:BoundField DataField="CHANGETYPE" ItemStyle-CssClass="GridRow" HeaderText="Type"
                        ReadOnly="True" SortExpression="Type" ItemStyle-Width="15%" />
                    <asp:BoundField DataField="OPENCLOSED" ItemStyle-CssClass="GridRow" HeaderText="Open/ Closed"
                        ReadOnly="True" ItemStyle-Width="4%" />
                    <asp:BoundField DataField="STATUS" ItemStyle-CssClass="GridRow" HeaderText="Detailed Status"
                        ReadOnly="True" ItemStyle-Width="15%" />
                    <asp:BoundField DataField="CREATEDON" ItemStyle-CssClass="GridRow" HeaderText="Creation Date"
                        ReadOnly="True" DataFormatString="{0:dd-MM-yyyy}" ItemStyle-Width="7%" />
                    <asp:BoundField DataField="LASTMODIFIEDON" ItemStyle-CssClass="GridRow" HeaderText="Last Modified Date"
                        ReadOnly="True" DataFormatString="{0:dd-MM-yyyy}" ItemStyle-Width="7%" />
                </Columns>
                <HeaderStyle BackColor="#C3C1C1" />
                <RowStyle HorizontalAlign="Left" />
                <PagerStyle CssClass="pagergrid" />
            </asp:GridView>

我想加密DataNavigateUrlFormatString="ChangeRequestDetails.aspx?ID={0}"

1 个答案:

答案 0 :(得分:1)

首先,请确保您已经创建了一个返回string的方法以用于加密/解密。该方法应该可以从页面标记中的数据绑定语法访问:

public string ParseRequest(string id)
{
    // perform encryption/decryption here
}

请注意,HyperLinkField不支持与加密/解密过程所需的Eval()方法绑定(因为它将抛出 HyperLinkField没有DataBinding事件消息),则需要使用TemplateField并将HyperLink控件放入其中。然后使用数据绑定在NavigateUrl内部调用加密/解密方法:

<asp:GridView ID="gvCreatedCRList" runat="server" AutoGenerateColumns="False" CssClass="table table-bordered table-hover"
    ShowHeaderWhenEmpty="True" PageSize="10" AllowPaging="true" EmptyDataText="No data to display.">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:HyperLink ID="CRNo" runat="server" 
                 NavigateUrl='<%# this.ResolveUrl(
                 string.Format("ChangeRequestDetails.aspx?ID={0}", ParseRequest(Eval("CHANGEREQUESTID").ToString()))) %>' 
                 Text="CR No" Target="_blank" ...>
                </asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>

        <%-- other BoundField columns --%>

    </Columns>

    <%-- other settings --%>
</asp:GridView>

或者如果您想与查询字符串一起加密整个URL,请使用这种方式:

<asp:HyperLink ID="CRNo" runat="server" 
     NavigateUrl='<%# this.ResolveUrl(ParseRequest(Eval("REQUESTURL").ToString())) %>' Text="CR No" Target="_blank" ...>
</asp:HyperLink>