我有一个在GridView
中的BoundField
。
<asp:BoundField DataField="ReportId" HeaderText="RId" Visible="false" />
但是当我尝试获取该字段中的文本时,它返回为空。
protected void gvwReports_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewSchedule")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvwReports.Rows[index];
string s = row.Cells[0].Text;
}
}
但是,如果我将BoundField
的.Visible
属性更改为true
,它会返回正确的值。
Visible="false"
不仅会在客户端上“隐藏”该字段,甚至不会将数据发送到客户端。因此,无法从客户端获取它。因此,不要设置Visible="false"
,而是使用一个CssClass
在客户端上隐藏它,但数据仍然存在。然后,在 postback 时,数据将再次可用于服务器端。就像 COLD TOLD 建议的那样,但没有拼写错误 :), 并记得设置标题的可见性。 - hmqcnoesy