使用loginview隐藏内容占位符

时间:2014-05-27 13:30:13

标签: html asp.net visual-studio-2012

我试图以这种方式隐藏母版页中的内容:

 <asp:LoginView ID="LoginView1" runat="server">

            <AnonymousTemplate>
                <h1 style="color:white"> You must be logged in to view the content</h1>
            </AnonymousTemplate>

            <LoggedInTemplate>
                <div>
                    <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
                    </asp:ContentPlaceHolder>
                </div>
            </LoggedInTemplate>
        </asp:LoginView>

但是当我启动它时它会给我一个错误:我在内容占位符中的一个页面中有文字 它表示对象的引用为null。 我不明白为什么。

这是给我错误的部分:

  Literal.Text = string.Format(@"
                                                      <table class='zebra'   style=' border-collapse: collapse;border-spacing: 0;width:100%;-webkit-box-shadow:  0px 2px 1px 5px rgba(242, 242, 242, 0.1); box-shadow:  0px 2px 1px 5px rgba(242, 242, 242, 0.1);' >
                                                        <thead>
                                                            <tr>
                                                                <th ></th>
                                                                <th >Team</th>
                                                                <th>Played</th>
                                                                <th >Won</th>
                                                                <th>Drawn</th>
                                                                <th >Lost</th>
                                                                <th>For</th>
                                                                <th>Against</th>
                                                                <th>Points</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                         {0}
                                                        </tbody>
                                                    </table>", sb);
        }

sb是StringBuilder

1 个答案:

答案 0 :(得分:0)

您无法在LoginView中引用项目。您需要在LoginView控件上使用FindControl()方法来获取它们。

由于您引用的Literal对象位于LoginView内部的ContentPlaceHolder中,因此当您尝试引用它时会抛出错误。

以下是查找控件的示例:

//Finding a TextBox
Literal lit = LoginView1.FindControl("Literal") as Literal
if (lit != null)
{
     lit.Text = "Value";
}

LoginView是一个容器控件。因此,必须通过FindControl()方法访问LoginView中包含的任何控件。

相关问题