简体   繁体   English

在gridview中设置标签格式以隐藏社会安全号码的前5位数字

[英]Format a label in a gridview to hide first 5 digits of a social security number

I have a data-bound label inside a template field of a griview that displays Social Security Numbers. 我在显示社会保险号的griview的模板字段内有一个数据绑定标签。 It currently shows the SSN's as such, 123-45-6789. 当前显示的是SSN,即123-45-6789。 I need to hide the first 5 digits so it show's like ###-##-6789. 我需要隐藏前5位数字,使其显示为###-##-6789。 Does anyone have an example of how I might accomplish this. 有没有人举我如何做到这一点的例子。

Here is the mark-up for the template fied: 这是模板字段的标记:

    <asp:TemplateField HeaderText="Social Security Number" SortExpression="SSN">
         <ItemTemplate>
              <asp:Label ID="lblSSN" runat="server" Text='<%# Bind("SSN") %>'></asp:Label>
         </ItemTemplate>
    </asp:TemplateField>

Code block that adds decrypted SSN to grid: 将解密的SSN添加到网格的代码块:

    table.Columns.Add("SSN");
            foreach (DataRow row in table.Rows)
            {
                row["SSN"] = DecryptSSN(row["EncryptedSSN"].ToString());                   
                row.AcceptChanges();
            }

I'm new to programming so any help would be greatly appreciated. 我是编程新手,所以将不胜感激。

Here is a helper method. 这是一个辅助方法。

public static string GetMaskedNumber(string number)
{
    if (String.IsNullOrEmpty(number))
        return string.Empty;

    if (number.Length <= 5)
        return number;

    string last5 = number.Substring(number.Length - 5, 5);
    var maskedChars = new StringBuilder();
    for (int i = 0; i < number.Length - 5; i++)
    {
        maskedChars.Append(number[i] == '-' ? "-" : "#");
    }
    return maskedChars + last5;
}

Result 结果

123-123-1234 to ###-##-1234 123-123-1234至###-##-1234

Use this and replace row["SSN"] the code below you can use to test it's functionality so something like this would work if the value were "123-45-6789" 使用此代码并替换row["SSN"]下面的代码即可用来测试其功能,因此,如果值是"123-45-6789"则类似的代码就可以工作

foreach (DataRow row in table.Rows)
{
    var newMaskedSSN = DecryptSSN(row["EncryptedSSN"].ToString().Replace("-", string.Empty)); 
    if (newMaskedSSN.Length > 4)
    {
        Console.WriteLine(
            string.Concat(
               "".PadLeft(9, '*'), 
        newMaskedSSN.Substring(newMaskedSSN.Length - 4)));
        row["SSN"] = newMaskedSSN; 
        row.AcceptChanges();
    }
}

results: *****6789

这个怎么样?

  select '###-##-'+right(ssn_coumn,4) as ssn  from your_table 

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

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