简体   繁体   English

Visible ='<%#UserCanEdit%>'–如果在Page_Load中设置为true,则控件不可见

[英]Visible='<%# UserCanEdit %>' – if set to true inside Page_Load, then control isn't visible


User control defines a property named UserCanEdit : 用户控件定义了一个名为UserCanEdit的属性:

private bool _userCanEdit=false;
public bool UserCanEdit
{
    get { return _userCanEdit; }
    set { _userCanEdit = value; }
}


This User Control also contains the following GridView : 此用户控件还包含以下GridView

    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Label ID="C" runat="server" Visible='<%# UserCanEdit %>' Text="Visibility"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>


For control C to be visible, UserCanEdit needs to be set to true . 为了使控件C可见,需要将UserCanEdit设置为true If I set it inside Page_Init() , then C is indeed visible. 如果将其设置在Page_Init()中 ,则C确实可见。 But if I set it inside Page_Load() , then C is not visible: 但是,如果我在Page_Load()中设置它,那么C是不可见的:

protected void Page_Load(object sender, EventArgs e)
{
    this.UserCanEdit = (this.Page.User.Identity.IsAuthenticated &&
       (this.Page.User.IsInRole("Administrators") ||
        this.Page.User.IsInRole("Editors")));

    GridView1.DataBind();
}


So why isn't C visible if UserCanEdit is set inside Page_Load()? 那么,如果在Page_Load()中设置UserCanEdit,为什么C不可见? As far as I know, single-value binding expression <%#%> is evaluated only when GridView.DataBind() is called, which happens after UserCanEdit is set to true ?! 据我所知,仅当调用GridView.DataBind()时才会计算单值绑定表达式<%#%> ,这是在UserCanEdit设置为true吗?


cheers 干杯

My guess is because the controls are being defined before you have a value for UserCanEdit yet. 我的猜测是,因为尚未定义UserCanEdit的值就已定义了控件。 Wouldn't the controls be loaded before the Page_Load() in the Page Initialization step? 是否在页面初始化步骤的Page_Load()之前加载控件?

http://msdn.microsoft.com/en-us/library/ms178472.aspx http://msdn.microsoft.com/en-us/library/ms178472.aspx

I guess issue over here is UserCanEdit is not part your datasource for gridview. 我想这里的问题是UserCanEdit不是您的gridview数据源的一部分。 How can you Bind based on Property which is not part of your source. 如何基于不属于您来源的媒体资源进行绑定。 I guess what you are trying to do is you want to hide a column based on some user credential. 我猜您正在尝试做的是要基于某些用户凭据隐藏列。 Possible sulution would be user OnRowDataBound event. 可能的解决方法是用户OnRowDataBound事件。 And inside that event user something like this 在该事件用户内部,像这样

if(Condition) if(条件)

((Label)e.Row.FindControl("C")).visible = true;

else 其他

((Label)e.Row.FindControl("C")).visible = false;

Now you can set this codition inside actual Page_Load(Your web page) event. 现在,您可以在实际的Page_Load(您的网页)事件中设置此条件。

Note: Condition is actully public property similar to your UserCanEdit 注意:条件是与您的UserCanEdit相似的强制性公共属性

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

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