如何在 Web 窗体中访问 GridView Templete 字段的属性

How to access properties of GridView Templete Field in Web Forms

我在 WebFormsGridView 上有 .NET 项目。 GridView 填充了来自我的数据库的数据。数据库有名为 password 的列,但在 GridView 上,此列设置为 Visible="False"。当我单击命令按钮“编辑”时,所有框都准备好填充新数据,但列密码是 不可见

我的问题是:当我单击“编辑”时,如何(或我能否)使带有密码的列可见并准备好填充带有新密码的文本框,但我也不知道' 当然要显示其他密码。摘要 我可以以某种方式访问​​此 TempleteField/ItemTemplete 的属性吗?

无需设置Visible="false"属性。只需欺骗 GridView 在正常显示模式下隐藏列并在编辑模式下显示列。


假设Password列为第三列,处理GridView的RowDataBound事件隐藏该列:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Just return in case GridView is rendering in Edit Mode
            if (GridView1.EditIndex >= 0)
                return;

            // In case GridView is rendering in Normal state
            // Hide the Columns you don't want to display
            if ((e.Row.RowState == DataControlRowState.Normal ||
                 e.Row.RowState == DataControlRowState.Alternate) &&
            ( e.Row.RowType == DataControlRowType.DataRow || 
              e.Row.RowType == DataControlRowType.Header))
            {
                // Hide the Password Column
                e.Row.Cells[2].Visible = false;
            }
        }

如果您使用 TemplateField 作为密码列,您可以使用 GridView 的 DataBound 事件隐藏它:

protected void GridView1_DataBound(object sender, EventArgs e)
{
       if (GridView1.EditIndex >= 0)
            return;
       GridView1.Columns[2].Visible = false;            
}

现在,当 GridView 进入编辑模式时(点击编辑按钮等),它将在编辑模式下显示 Password 列。