我希望能够根据用户角色显示/隐藏“ div标签”。
有些人会看到某些东西,而其他人则看不到。在中继器之外,这很容易。显然d1.Visible = false;
不起作用。
我搜索了包括Stack在内的网络,但无济于事。哪个方向是合适的问题更多。
代码-内联-
protected void Eng_SelectedIndexChanged(object sender, EventArgs e)
{
sqlInv = "Select Item1, Item2, Item3, From Table";
IDataReader reader6 = DatabaseHelper.GetReader(string.Empty, sqlInv);
Repeater1.DataSource = reader6;
Repeater1.DataBind();
reader6.Close();
}
html
asp:Repeater runat="server" id="Repeater1">
itemtemplate>
div>
div id="d1" runat="server"><%# DataBinder.Eval(Container.DataItem, "Item1") %>/div>
<div id="d2" runat="server"><%# DataBinder.Eval(Container.DataItem, "Item2") %>/div>
div id="d3" runat="server"><%# DataBinder.Eval(Container.DataItem, "Item3") %>/div>
/itemtemplate>
/asp:Repeater>
/div>
答案 0 :(得分:0)
<asp:Repeater runat="server" id="Repeater1" OnItemDataBound="Repeater1_ItemDataBound">
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType.Equals(ListItemType.AlternatingItem) || e.Item.ItemType.Equals(ListItemType.Item))
{
//You can hide/show disable/enable your div
Control div1 = e.Item.FindControl("div1");
//get role here and check
// if role != admin
div1.Visible = false;
}
}
答案 1 :(得分:0)
感谢您的回复和指导。在您回复之前,我决定尝试一种非常相似的方法。如下: 受保护的void Repeater1_ItemDataBound(object sender,RepeaterItemEventArgs e)
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Show or hid div here
System.Web.UI.HtmlControls.HtmlContainerControl div1 = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("div1");
System.Web.UI.HtmlControls.HtmlContainerControl div2 = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("div2");
System.Web.UI.HtmlControls.HtmlContainerControl div3 = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("div3");
Also changed the test for the reader.
the reader is checking the DB for a RoleId but the problem was when the reader
为空或为空,页面被炸毁。
I changed to test if the reader had a record or not.
if (reader6a.Read())
{
div1.Visible = true;
div2.Visible = false;
div3.Visible = false;
}
else
{
and so on
}