【发布时间】:2015-12-09 01:11:08
【问题描述】:
对于一个用户,我需要显示一个不同的 gridview,所以我的想法是创建 2 个网格并将它们都隐藏起来,然后在 Page_Load() 上根据用户 ID 显示适当的 gridview。这可行,但由于 2 个网格非常相似,因此非常重复。他们是实现这一目标的更好方法还是语法上更合适的方法?
更多信息 - 适合所有人的网格有 16 个字段
YouOnly 网格共享相同的 16 个字段,但唯一的区别是另外 4 个字段
HTML(只是数据采样)
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div id="TextTopOfPage" runat="server">This is the top of the page</div>
<br />
<div id="Alldiv" runat="server">
<asp:GridView ID="ForAll" runat="server" visible="false"/>
</div>
<div id="YouOnlyDiv" runat="server">
<asp:GridView ID="YouOnly" runat="server" Visible="false" />
</div>
</form>
</body>
</html>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (userid == "eac11") { this.YouOnly.Visible = true; }
if (userid != "eac11") { this.ForAll.Visible = true; }
}
【问题讨论】:
-
能否根据用户 ID 过滤用于绑定到 Grid 的数据?
-
@GrantWinney - 网格的实际语法是显示 16 个字段,唯一的区别是如果用户 ID 是 eac11,它会显示 20 个字段(与上面的 16 个完全相同,另外还有 4 个)
-
@rs - 我从来没有这样做过。我会谷歌看看我是否可以。
标签: c# asp.net gridview webforms