繁体   English   中英

如何在编辑时更改GridView单元格的单元格值?

[英]How to change the cell value of a GridView cell WHILE editing?

我有一个带有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;
        }
    }
}

看来我必须访问显示的编辑控件,但是如何?

使用BoundField,没有找到详细的方法来找到编辑控件。 您可能会在Cell中找到它作为第一个控件,但是为了将来证明您的解决方案,我建议使用templatefield:

<asp:TemplateField  HeaderText = "Secret">

    <ItemTemplate>
        *****
    </ItemTemplate>
    <EditItemTemplate>
        <asp:TextBox ID="txtSecret" runat="server"

            Text='<%# Decrypt(Eval("encrypted").ToString()) %>'></asp:TextBox>

    </EditItemTemplate> 
</asp:TemplateField>

您的Decrypt方法必须在类中公开。 甚至不需要OnRowDataBound

暂无
暂无

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

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