我有一个 GridView BoundField
:
<asp:BoundField HeaderText="Secret" DataField="encrypted" DataFormatString="***"/>
我只想在用户编辑行时解密此字段。这样做的合乎逻辑的地方似乎在RowDataBound()
. 我尝试使用e.Rows.Cells
,但在编辑时它是空的(否则会是'***'
)。
我可以使用 获取基础值DataRowView
,但我不知道在编辑时如何在 TextBox 中获取解密数据。
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState.HasFlag(DataControlRowState.Edit))
{
// When in Normal state, e.Row.Cells[0].Text is '***'
// When in Edit state, e.Row.Cells[0].Text is empty.
string cellValue = e.Row.Cells[0].Text; // Always empty
// Get the encrypted field
DataRowView rowView = (DataRowView)e.Row.DataItem;
string decrypted = Decrypt(rowView["encrypted"].ToString());
// This doesn't work - how to get this value in the edit box?
e.Row.Cells[0].Text = decrypted;
}
}
}
看起来我必须访问显示的编辑控件,但是如何?