拉出我的头发,一些帮助会很棒。
我在ASP中加载一个GridView,动态填充SQL语句的结果。此页面用于一般查询,因此从查询结果中提取标题名称,而不是模板。
我想要做的是调用一个标题,即SSN(敏感数据列)我想通过这个特定列中的每个单元格并屏蔽该字段。示例:“### - ## - ####”。在页面显示之前,所有更改都会保留在每次更改页面以及重新绑定GridView时。
我查看了一些事件,如GridView1_RowCreated,GridView1_OnDataBound
但是,每当我搜索header.text时,它总是空的!我可以改变并设置它,但它从未在这些事件中填充。这让我相信我在错误的地方做这个更新。
即:
Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim header As String
' DataControlRowType.DataRow - also tried this with checking HeaderText too.
If e.Row.RowType = DataControlRowType.Header Then
For columnIndex As Integer = 0 To e.Row.Cells.Count - 1 Step 1
header = e.Row.Cells(columnIndex).Text
Response.Write(header) ' Empty
Response.Write("Cell") ' Will Display this for each header cell.
header = String.Empty
Next
End If
End Sub
我是否需要在Page_Load或PreRender上执行此操作?任何想法/例子都会很棒 谢谢你的帮助。
答案 0 :(得分:2)
如果您可以通过其标题文本确定特殊列,则可以找到列索引,然后在找到特定列时更改RowDataBound事件中单元格的文本。
这在我的测试中有效:
<强> ASPX:强>
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="GridView1" />
</div>
</form>
</body>
</html>
代码整理:
Partial Public Class _Default
Inherits Page
Private _specialColumnName As String = "DEF"
Private _specialColumnIndex As Integer = -1
Private ReadOnly Property Data() As DataTable
Get
If Session("Default.Data") Is Nothing Then
Dim value = New DataTable()
Using connection = New SqlClient.SqlConnection("your_connection_string")
Using command = connection.CreateCommand()
command.CommandText = "SELECT * FROM your_table"
Using adapter = New SqlClient.SqlDataAdapter(command)
adapter.Fill(value)
End Using
End Using
End Using
'value.Columns.Add("ABC", GetType(String))
'value.Columns.Add("DEF", GetType(Integer))
'value.Columns.Add("GHI", GetType(Boolean))
'value.Rows.Add("A", 1, True)
'value.Rows.Add("B", 2, False)
'value.Rows.Add("C", 3, False)
Session("Default.Data") = value
End If
Return CType(Session("Default.Data"), DataTable)
End Get
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
GridView1.DataSource = Data
GridView1.DataBind()
End Sub
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.Header Then
For index As Integer = 0 To e.Row.Cells.Count - 1
If e.Row.Cells(index).Text = _specialColumnName Then
_specialColumnIndex = index
Return
End If
Next
ElseIf _specialColumnIndex > -1 AndAlso e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Cells(_specialColumnIndex).Text = "###"
End If
End Sub
End Class