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

Encrypt Query string for DataNavigateUrlFormatString in Gridview

我在这里想要的是我想加密正在使用 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}"

首先,确保您已经创建了一个 returns string 用于 encryption/decryption 目的的方法。该方法应该可以从页面标记中的数据绑定语法访问:

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

请注意,HyperLinkField 不支持与 encryption/decryption 进程所需的 Eval() 方法绑定(因为它会抛出 HyperLinkField doesn't have DataBinding 事件 消息),您需要使用 TemplateField 并将 HyperLink 控件放入其中。然后使用数据绑定在 NavigateUrl 中调用 encryption/decryption 方法:

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