我想在gridview中显示电子邮件,解密密码和加密密码
<asp:GridView ID="gvUsers" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Email" HeaderText="email" />
<asp:BoundField DataField="Password" HeaderText=" encrypted password" />
<asp:BoundField DataField="Password" HeaderText="decrypted password" />
</Columns>
</asp:GridView>
这是我绑定gridview的代码
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Memberships"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
DataTable dt = new DataTable();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
gvUsers.DataSource = dt;
gvUsers.DataBind();
}
}
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Text = Decrypt(e.Row.Cells[2].Text);
}
}
private string Decrypt(string cipherText)
{
string EncryptionKey = "MAKV2SPBNI99212";
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
问题是,当我调用OnRowDataBound时,页面未运行且出现错误“无法复制文件pageName.dll”,因为找不到它。为什么会这样?如果我从gridview页面运行排除OnRowDataBound事件没有任何错误。但是对于密码解密,我必须调用OnRowDataBound。帮助
答案 0 :(得分:0)
您的活动名称错误
替换下面的
<asp:GridView ID="gvUsers" runat="server"
AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
使用
<asp:GridView ID="gvUsers" runat="server"
AutoGenerateColumns="false" OnRowDataBound="gvUsers_RowDataBound">
希望这能解决你的问题。